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