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