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