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