Improve error diagnostics of mmap_full_file().
[paraslash.git] / fd.c
1 /*
2 * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file fd.c Helper functions for file descriptor handling. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/mman.h>
13 #include <fcntl.h>
14 #include <sys/uio.h>
15
16 #include "para.h"
17 #include "error.h"
18 #include "string.h"
19 #include "fd.h"
20
21 /**
22 * Write a buffer to a file descriptor, re-write on short writes.
23 *
24 * \param fd The file descriptor.
25 * \param buf The buffer to be sent.
26 * \param len The length of \a buf.
27 *
28 * \return Standard. In any case, the number of bytes that have been written is
29 * stored in \a len.
30 */
31 int write_all(int fd, const char *buf, size_t *len)
32 {
33 size_t total = *len;
34
35 assert(total);
36 *len = 0;
37 while (*len < total) {
38 int ret = write(fd, buf + *len, total - *len);
39 if (ret == -1)
40 return -ERRNO_TO_PARA_ERROR(errno);
41 *len += ret;
42 }
43 return 1;
44 }
45
46 /**
47 * Write a buffer to a non-blocking file descriptor.
48 *
49 * \param fd The file descriptor.
50 * \param buf the buffer to write.
51 * \param len the number of bytes of \a buf.
52 *
53 * EAGAIN is not considered an error condition. For example CCID3 has a
54 * sending wait queue which fills up and is emptied asynchronously. The EAGAIN
55 * case means that there is currently no space in the wait queue, but this can
56 * change at any moment.
57 *
58 * \return Negative on errors, number of bytes written else.
59 */
60 int write_nonblock(int fd, const char *buf, size_t len)
61 {
62 size_t written = 0;
63 int ret = 0;
64
65 while (written < len) {
66 size_t num = len - written;
67
68 ret = write(fd, buf + written, num);
69 if (ret < 0 && errno == EAGAIN)
70 return written;
71 if (ret < 0)
72 return -ERRNO_TO_PARA_ERROR(errno);
73 written += ret;
74 }
75 return written;
76 }
77
78 /**
79 * Read from a non-blocking file descriptor into multiple buffers.
80 *
81 * \param fd The file descriptor to read from.
82 * \param iov Scatter/gather array used in readv().
83 * \param iovcnt Number of elements in \a iov.
84 * \param rfds An optional fd set pointer.
85 * \param num_bytes Result pointer. Contains the number of bytes read from \a fd.
86 *
87 * If \a rfds is not \p NULL and the (non-blocking) file descriptor \a fd is
88 * not set in \a rfds, this function returns early without doing anything.
89 * Otherwise The function tries to read up to \a sz bytes from \a fd. As for
90 * write_nonblock(), EAGAIN is not considered an error condition. However, EOF
91 * is.
92 *
93 * \return Zero or a negative error code. If the underlying call to readv(2)
94 * returned zero (indicating an end of file condition) or failed for some
95 * reason other than \p EAGAIN, a negative return value is returned.
96 *
97 * In any case, \a num_bytes contains the number of bytes that have been
98 * successfully read from \a fd (zero if the first readv() call failed with
99 * EAGAIN). Note that even if the function returns negative, some data might
100 * have been read before the error occurred. In this case \a num_bytes is
101 * positive.
102 *
103 * \sa \ref write_nonblock(), read(2), readv(2).
104 */
105 int readv_nonblock(int fd, struct iovec *iov, int iovcnt, fd_set *rfds,
106 size_t *num_bytes)
107 {
108 int ret, i, j;
109
110 *num_bytes = 0;
111 /*
112 * Avoid a shortcoming of select(): Reads from a non-blocking fd might
113 * return EAGAIN even if FD_ISSET() returns true. However, FD_ISSET()
114 * returning false definitely means that no data can currently be read.
115 * This is the common case, so it is worth to avoid the overhead of the
116 * read() system call in this case.
117 */
118 if (rfds && !FD_ISSET(fd, rfds))
119 return 0;
120
121 for (i = 0, j = 0; i < iovcnt;) {
122
123 /* fix up the first iov */
124 assert(j < iov[i].iov_len);
125 iov[i].iov_base += j;
126 iov[i].iov_len -= j;
127 ret = readv(fd, iov + i, iovcnt - i);
128 iov[i].iov_base -= j;
129 iov[i].iov_len += j;
130
131 if (ret == 0)
132 return -E_EOF;
133 if (ret < 0) {
134 if (errno == EAGAIN)
135 return 0;
136 return -ERRNO_TO_PARA_ERROR(errno);
137 }
138 *num_bytes += ret;
139 while (ret > 0) {
140 if (ret < iov[i].iov_len - j) {
141 j += ret;
142 break;
143 }
144 ret -= iov[i].iov_len - j;
145 j = 0;
146 if (++i >= iovcnt)
147 break;
148 }
149 }
150 return 0;
151 }
152
153 /**
154 * Read from a non-blocking file descriptor into a single buffer.
155 *
156 * \param fd The file descriptor to read from.
157 * \param buf The buffer to read data to.
158 * \param sz The size of \a buf.
159 * \param rfds \see \ref readv_nonblock().
160 * \param num_bytes \see \ref readv_nonblock().
161 *
162 * This is a simple wrapper for readv_nonblock() which uses an iovec with a single
163 * buffer.
164 *
165 * \return The return value of the underlying call to readv_nonblock().
166 */
167 int read_nonblock(int fd, void *buf, size_t sz, fd_set *rfds, size_t *num_bytes)
168 {
169 struct iovec iov = {.iov_base = buf, .iov_len = sz};
170 return readv_nonblock(fd, &iov, 1, rfds, num_bytes);
171 }
172
173 /**
174 * Read a buffer and check its content for a pattern.
175 *
176 * \param fd The file descriptor to receive from.
177 * \param pattern The expected pattern.
178 * \param bufsize The size of the internal buffer.
179 * \param rfds Passed to read_nonblock().
180 *
181 * This function tries to read at most \a bufsize bytes from the non-blocking
182 * file descriptor \a fd. If at least \p strlen(\a pattern) bytes have been
183 * received, the beginning of the received buffer is compared with \a pattern,
184 * ignoring case.
185 *
186 * \return Positive if \a pattern was received, negative on errors, zero if no data
187 * was available to read.
188 *
189 * \sa \ref read_nonblock(), \sa strncasecmp(3).
190 */
191 int read_pattern(int fd, const char *pattern, size_t bufsize, fd_set *rfds)
192 {
193 size_t n, len;
194 char *buf = para_malloc(bufsize + 1);
195 int ret = read_nonblock(fd, buf, bufsize, rfds, &n);
196
197 buf[n] = '\0';
198 if (ret < 0)
199 goto out;
200 ret = 0;
201 if (n == 0)
202 goto out;
203 ret = -E_READ_PATTERN;
204 len = strlen(pattern);
205 if (n < len)
206 goto out;
207 if (strncasecmp(buf, pattern, len) != 0)
208 goto out;
209 ret = 1;
210 out:
211 if (ret < 0) {
212 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
213 PARA_NOTICE_LOG("recvd %zu bytes: %s\n", n, buf);
214 }
215 free(buf);
216 return ret;
217 }
218
219 /**
220 * Check whether a file exists.
221 *
222 * \param fn The file name.
223 *
224 * \return Non-zero iff file exists.
225 */
226 int file_exists(const char *fn)
227 {
228 struct stat statbuf;
229
230 return !stat(fn, &statbuf);
231 }
232
233 /**
234 * Paraslash's wrapper for select(2).
235 *
236 * It calls select(2) (with no exceptfds) and starts over if select() was
237 * interrupted by a signal.
238 *
239 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
240 * \param readfds fds that should be checked for readability.
241 * \param writefds fds that should be checked for writablility.
242 * \param timeout_tv upper bound on the amount of time elapsed before select()
243 * returns.
244 *
245 * \return The return value of the underlying select() call on success, the
246 * negative system error code on errors.
247 *
248 * All arguments are passed verbatim to select(2).
249 * \sa select(2) select_tut(2).
250 */
251 int para_select(int n, fd_set *readfds, fd_set *writefds,
252 struct timeval *timeout_tv)
253 {
254 int ret;
255 do
256 ret = select(n, readfds, writefds, NULL, timeout_tv);
257 while (ret < 0 && errno == EINTR);
258 if (ret < 0)
259 return -ERRNO_TO_PARA_ERROR(errno);
260 return ret;
261 }
262
263 /**
264 * Set a file descriptor to blocking mode.
265 *
266 * \param fd The file descriptor.
267 *
268 * \return Standard.
269 */
270 __must_check int mark_fd_blocking(int fd)
271 {
272 int flags = fcntl(fd, F_GETFL);
273 if (flags < 0)
274 return -ERRNO_TO_PARA_ERROR(errno);
275 flags = fcntl(fd, F_SETFL, ((long)flags) & ~O_NONBLOCK);
276 if (flags < 0)
277 return -ERRNO_TO_PARA_ERROR(errno);
278 return 1;
279 }
280
281 /**
282 * Set a file descriptor to non-blocking mode.
283 *
284 * \param fd The file descriptor.
285 *
286 * \return Standard.
287 */
288 __must_check int mark_fd_nonblocking(int fd)
289 {
290 int flags = fcntl(fd, F_GETFL);
291 if (flags < 0)
292 return -ERRNO_TO_PARA_ERROR(errno);
293 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
294 if (flags < 0)
295 return -ERRNO_TO_PARA_ERROR(errno);
296 return 1;
297 }
298
299 /**
300 * Set a file descriptor in a fd_set.
301 *
302 * \param fd The file descriptor to be set.
303 * \param fds The file descriptor set.
304 * \param max_fileno Highest-numbered file descriptor.
305 *
306 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
307 * return, \a max_fileno contains the maximum of the old_value and \a fd.
308 *
309 * \sa para_select.
310 */
311 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
312 {
313 assert(fd >= 0 && fd < FD_SETSIZE);
314 #if 0
315 {
316 int flags = fcntl(fd, F_GETFL);
317 if (!(flags & O_NONBLOCK)) {
318 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
319 exit(EXIT_FAILURE);
320 }
321 }
322 #endif
323 FD_SET(fd, fds);
324 *max_fileno = PARA_MAX(*max_fileno, fd);
325 }
326
327 /**
328 * Paraslash's wrapper for fgets(3).
329 *
330 * \param line Pointer to the buffer to store the line.
331 * \param size The size of the buffer given by \a line.
332 * \param f The stream to read from.
333 *
334 * \return Unlike the standard fgets() function, an integer value
335 * is returned. On success, this function returns 1. On errors, -E_FGETS
336 * is returned. A zero return value indicates an end of file condition.
337 */
338 __must_check int para_fgets(char *line, int size, FILE *f)
339 {
340 again:
341 if (fgets(line, size, f))
342 return 1;
343 if (feof(f))
344 return 0;
345 if (!ferror(f))
346 return -E_FGETS;
347 if (errno != EINTR) {
348 PARA_ERROR_LOG("%s\n", strerror(errno));
349 return -E_FGETS;
350 }
351 clearerr(f);
352 goto again;
353 }
354
355 /**
356 * Paraslash's wrapper for mmap.
357 *
358 * \param length Number of bytes to mmap.
359 * \param prot Either PROT_NONE or the bitwise OR of one or more of
360 * PROT_EXEC PROT_READ PROT_WRITE.
361 * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
362 * \param fd The file to mmap from.
363 * \param offset Mmap start.
364 * \param map Result pointer.
365 *
366 * \return Standard.
367 *
368 * \sa mmap(2).
369 */
370 int para_mmap(size_t length, int prot, int flags, int fd, off_t offset,
371 void *map)
372 {
373 void **m = map;
374
375 errno = EINVAL;
376 if (!length)
377 goto err;
378 *m = mmap(NULL, length, prot, flags, fd, offset);
379 if (*m != MAP_FAILED)
380 return 1;
381 err:
382 *m = NULL;
383 return -ERRNO_TO_PARA_ERROR(errno);
384 }
385
386 /**
387 * Wrapper for the open(2) system call.
388 *
389 * \param path The filename.
390 * \param flags The usual open(2) flags.
391 * \param mode Specifies the permissions to use.
392 *
393 * The mode parameter must be specified when O_CREAT is in the flags, and is
394 * ignored otherwise.
395 *
396 * \return The file descriptor on success, negative on errors.
397 *
398 * \sa open(2).
399 */
400 int para_open(const char *path, int flags, mode_t mode)
401 {
402 int ret = open(path, flags, mode);
403
404 if (ret >= 0)
405 return ret;
406 return -ERRNO_TO_PARA_ERROR(errno);
407 }
408
409 /**
410 * Wrapper for chdir(2).
411 *
412 * \param path The specified directory.
413 *
414 * \return Standard.
415 */
416 int para_chdir(const char *path)
417 {
418 int ret = chdir(path);
419
420 if (ret >= 0)
421 return 1;
422 return -ERRNO_TO_PARA_ERROR(errno);
423 }
424
425 /**
426 * Save the cwd and open a given directory.
427 *
428 * \param dirname Path to the directory to open.
429 * \param dir Result pointer.
430 * \param cwd File descriptor of the current working directory.
431 *
432 * \return Standard.
433 *
434 * Opening the current directory (".") and calling fchdir() to return is
435 * usually faster and more reliable than saving cwd in some buffer and calling
436 * chdir() afterwards.
437 *
438 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
439 * stored in \a cwd. If the function returns success, and \a cwd is not \p
440 * NULL, the caller must close this file descriptor (probably after calling
441 * fchdir(*cwd)).
442 *
443 * On errors, the function undos everything, so the caller needs neither close
444 * any files, nor change back to the original working directory.
445 *
446 * \sa getcwd(3).
447 *
448 */
449 static int para_opendir(const char *dirname, DIR **dir, int *cwd)
450 {
451 int ret;
452
453 if (cwd) {
454 ret = para_open(".", O_RDONLY, 0);
455 if (ret < 0)
456 return ret;
457 *cwd = ret;
458 }
459 ret = para_chdir(dirname);
460 if (ret < 0)
461 goto close_cwd;
462 *dir = opendir(".");
463 if (*dir)
464 return 1;
465 ret = -ERRNO_TO_PARA_ERROR(errno);
466 /* Ignore return value of fchdir() and close(). We're busted anyway. */
467 if (cwd) {
468 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
469 }
470 close_cwd:
471 if (cwd)
472 close(*cwd);
473 return ret;
474 }
475
476 /**
477 * A wrapper for fchdir().
478 *
479 * \param fd An open file descriptor.
480 *
481 * \return Standard.
482 */
483 static int para_fchdir(int fd)
484 {
485 if (fchdir(fd) < 0)
486 return -ERRNO_TO_PARA_ERROR(errno);
487 return 1;
488 }
489
490 /**
491 * A wrapper for mkdir(2).
492 *
493 * \param path Name of the directory to create.
494 * \param mode The permissions to use.
495 *
496 * \return Standard.
497 */
498 int para_mkdir(const char *path, mode_t mode)
499 {
500 if (!mkdir(path, mode))
501 return 1;
502 return -ERRNO_TO_PARA_ERROR(errno);
503 }
504
505 /**
506 * Open a file and map it into memory.
507 *
508 * \param path Name of the regular file to map.
509 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
510 * \param map On success, the mapping is returned here.
511 * \param size size of the mapping.
512 * \param fd_ptr The file descriptor of the mapping.
513 *
514 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
515 * open call is closed after mmap(). Otherwise the file is kept open and the
516 * file descriptor is returned in \a fd_ptr.
517 *
518 * \return Standard.
519 *
520 * \sa para_open(), mmap(2).
521 */
522 int mmap_full_file(const char *path, int open_mode, void **map,
523 size_t *size, int *fd_ptr)
524 {
525 int fd, ret, mmap_prot, mmap_flags;
526 struct stat file_status;
527
528 if (open_mode == O_RDONLY) {
529 mmap_prot = PROT_READ;
530 mmap_flags = MAP_PRIVATE;
531 } else {
532 mmap_prot = PROT_READ | PROT_WRITE;
533 mmap_flags = MAP_SHARED;
534 }
535 ret = para_open(path, open_mode, 0);
536 if (ret < 0)
537 return ret;
538 fd = ret;
539 if (fstat(fd, &file_status) < 0) {
540 ret = -ERRNO_TO_PARA_ERROR(errno);
541 goto out;
542 }
543 *size = file_status.st_size;
544 /*
545 * If the file is empty, *size is zero and mmap() would return EINVAL
546 * (Invalid argument). This error is common enough to spend an extra
547 * error code which explicitly states the problem.
548 */
549 ret = -E_EMPTY;
550 if (*size == 0)
551 goto out;
552 /*
553 * If fd refers to a directory, mmap() returns ENODEV (No such device),
554 * at least on Linux. "Is a directory" seems to be more to the point.
555 */
556 ret = -ERRNO_TO_PARA_ERROR(EISDIR);
557 if (S_ISDIR(file_status.st_mode))
558 goto out;
559
560 ret = para_mmap(*size, mmap_prot, mmap_flags, fd, 0, map);
561 out:
562 if (ret < 0 || !fd_ptr)
563 close(fd);
564 else
565 *fd_ptr = fd;
566 return ret;
567 }
568
569 /**
570 * A wrapper for munmap(2).
571 *
572 * \param start The start address of the memory mapping.
573 * \param length The size of the mapping.
574 *
575 * \return Standard.
576 *
577 * \sa munmap(2), mmap_full_file().
578 */
579 int para_munmap(void *start, size_t length)
580 {
581 int err;
582
583 if (!start)
584 return 0;
585 if (munmap(start, length) >= 0)
586 return 1;
587 err = errno;
588 PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
589 strerror(err));
590 return -ERRNO_TO_PARA_ERROR(err);
591 }
592
593 /**
594 * Check a file descriptor for writability.
595 *
596 * \param fd The file descriptor.
597 *
598 * \return positive if fd is ready for writing, zero if it isn't, negative if
599 * an error occurred.
600 */
601
602 int write_ok(int fd)
603 {
604 struct timeval tv;
605 fd_set wfds;
606
607 FD_ZERO(&wfds);
608 FD_SET(fd, &wfds);
609 tv.tv_sec = 0;
610 tv.tv_usec = 0;
611 return para_select(fd + 1, NULL, &wfds, &tv);
612 }
613
614 /**
615 * Ensure that file descriptors 0, 1, and 2 are valid.
616 *
617 * Common approach that opens /dev/null until it gets a file descriptor greater
618 * than two.
619 *
620 * \sa okir's Black Hats Manual.
621 */
622 void valid_fd_012(void)
623 {
624 while (1) {
625 int fd = open("/dev/null", O_RDWR);
626 if (fd < 0)
627 exit(EXIT_FAILURE);
628 if (fd > 2) {
629 close(fd);
630 break;
631 }
632 }
633 }
634
635 /**
636 * Traverse the given directory recursively.
637 *
638 * \param dirname The directory to traverse.
639 * \param func The function to call for each entry.
640 * \param private_data Pointer to an arbitrary data structure.
641 *
642 * For each regular file under \a dirname, the supplied function \a func is
643 * called. The full path of the regular file and the \a private_data pointer
644 * are passed to \a func. Directories for which the calling process has no
645 * permissions to change to are silently ignored.
646 *
647 * \return Standard.
648 */
649 int for_each_file_in_dir(const char *dirname,
650 int (*func)(const char *, void *), void *private_data)
651 {
652 DIR *dir;
653 struct dirent *entry;
654 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
655
656 if (ret < 0)
657 return ret == -ERRNO_TO_PARA_ERROR(EACCES)? 1 : ret;
658 /* scan cwd recursively */
659 while ((entry = readdir(dir))) {
660 mode_t m;
661 char *tmp;
662 struct stat s;
663
664 if (!strcmp(entry->d_name, "."))
665 continue;
666 if (!strcmp(entry->d_name, ".."))
667 continue;
668 if (lstat(entry->d_name, &s) == -1)
669 continue;
670 m = s.st_mode;
671 if (!S_ISREG(m) && !S_ISDIR(m))
672 continue;
673 tmp = make_message("%s/%s", dirname, entry->d_name);
674 if (!S_ISDIR(m)) {
675 ret = func(tmp, private_data);
676 free(tmp);
677 if (ret < 0)
678 goto out;
679 continue;
680 }
681 /* directory */
682 ret = for_each_file_in_dir(tmp, func, private_data);
683 free(tmp);
684 if (ret < 0)
685 goto out;
686 }
687 ret = 1;
688 out:
689 closedir(dir);
690 ret2 = para_fchdir(cwd_fd);
691 if (ret2 < 0 && ret >= 0)
692 ret = ret2;
693 close(cwd_fd);
694 return ret;
695 }