]> git.tuebingen.mpg.de Git - paraslash.git/blob - fd.c
openssl: Switch to evp API for sha1 and sha256.
[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 num_bytes Result pointer. Contains the number of bytes read from \a fd.
180  *
181  * This function tries to read up to sz bytes from fd, where sz is the sum of
182  * the lengths of all vectors in iov. Like \ref xwrite(), EAGAIN and EINTR are
183  * not considered error conditions. However, EOF is.
184  *
185  * \return Zero or a negative error code. If the underlying call to readv(2)
186  * returned zero (indicating an end of file condition) or failed for some
187  * reason other than EAGAIN or EINTR, a negative error code is returned.
188  *
189  * In any case, \a num_bytes contains the number of bytes that have been
190  * successfully read from \a fd (zero if the first readv() call failed with
191  * EAGAIN). Note that even if the function returns negative, some data might
192  * have been read before the error occurred. In this case \a num_bytes is
193  * positive.
194  *
195  * \sa \ref xwrite(), read(2), readv(2).
196  */
197 int readv_nonblock(int fd, struct iovec *iov, int iovcnt, size_t *num_bytes)
198 {
199         int ret, i, j;
200
201         *num_bytes = 0;
202         for (i = 0, j = 0; i < iovcnt;) {
203                 /* fix up the first iov */
204                 assert(j < iov[i].iov_len);
205                 iov[i].iov_base += j;
206                 iov[i].iov_len -= j;
207                 ret = readv(fd, iov + i, iovcnt - i);
208                 iov[i].iov_base -= j;
209                 iov[i].iov_len += j;
210
211                 if (ret == 0)
212                         return -E_EOF;
213                 if (ret < 0) {
214                         if (errno == EAGAIN || errno == EINTR)
215                                 return 0;
216                         return -ERRNO_TO_PARA_ERROR(errno);
217                 }
218                 *num_bytes += ret;
219                 while (ret > 0) {
220                         if (ret < iov[i].iov_len - j) {
221                                 j += ret;
222                                 break;
223                         }
224                         ret -= iov[i].iov_len - j;
225                         j = 0;
226                         if (++i >= iovcnt)
227                                 break;
228                 }
229         }
230         return 0;
231 }
232
233 /**
234  * Read from a non-blocking file descriptor into a single buffer.
235  *
236  * \param fd The file descriptor to read from.
237  * \param buf The buffer to read data to.
238  * \param sz The size of \a buf.
239  * \param num_bytes \see \ref readv_nonblock().
240  *
241  * This is a simple wrapper for readv_nonblock() which uses an iovec with a single
242  * buffer.
243  *
244  * \return The return value of the underlying call to readv_nonblock().
245  */
246 int read_nonblock(int fd, void *buf, size_t sz, size_t *num_bytes)
247 {
248         struct iovec iov = {.iov_base = buf, .iov_len = sz};
249         return readv_nonblock(fd, &iov, 1, num_bytes);
250 }
251
252 /**
253  * Read a buffer and check its content for a pattern.
254  *
255  * \param fd The file descriptor to receive from.
256  * \param pattern The expected pattern.
257  * \param bufsize The size of the internal buffer.
258  *
259  * This function tries to read at most \a bufsize bytes from the non-blocking
260  * file descriptor \a fd. If at least \p strlen(\a pattern) bytes have been
261  * received, the beginning of the received buffer is compared with \a pattern,
262  * ignoring case.
263  *
264  * \return Positive if \a pattern was received, negative on errors, zero if no data
265  * was available to read.
266  *
267  * \sa \ref read_nonblock(), \sa strncasecmp(3).
268  */
269 int read_pattern(int fd, const char *pattern, size_t bufsize)
270 {
271         size_t n, len;
272         char *buf = alloc(bufsize + 1);
273         int ret = read_nonblock(fd, buf, bufsize, &n);
274
275         buf[n] = '\0';
276         if (ret < 0)
277                 goto out;
278         ret = 0;
279         if (n == 0)
280                 goto out;
281         ret = -E_READ_PATTERN;
282         len = strlen(pattern);
283         if (n < len)
284                 goto out;
285         if (strncasecmp(buf, pattern, len) != 0)
286                 goto out;
287         ret = 1;
288 out:
289         if (ret < 0) {
290                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
291                 PARA_NOTICE_LOG("recvd %zu bytes: %s\n", n, buf);
292         }
293         free(buf);
294         return ret;
295 }
296
297 /**
298  * Check whether a file exists.
299  *
300  * \param fn The file name.
301  *
302  * \return True iff file exists.
303  */
304 bool file_exists(const char *fn)
305 {
306         struct stat statbuf;
307
308         return !stat(fn, &statbuf);
309 }
310
311 /**
312  * Set a file descriptor to blocking mode.
313  *
314  * \param fd The file descriptor.
315  *
316  * \return Standard.
317  */
318 __must_check int mark_fd_blocking(int fd)
319 {
320         int flags = fcntl(fd, F_GETFL);
321         if (flags < 0)
322                 return -ERRNO_TO_PARA_ERROR(errno);
323         flags = fcntl(fd, F_SETFL, ((long)flags) & ~O_NONBLOCK);
324         if (flags < 0)
325                 return -ERRNO_TO_PARA_ERROR(errno);
326         return 1;
327 }
328
329 /**
330  * Set a file descriptor to non-blocking mode.
331  *
332  * \param fd The file descriptor.
333  *
334  * \return Standard.
335  */
336 __must_check int mark_fd_nonblocking(int fd)
337 {
338         int flags = fcntl(fd, F_GETFL);
339         if (flags < 0)
340                 return -ERRNO_TO_PARA_ERROR(errno);
341         flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
342         if (flags < 0)
343                 return -ERRNO_TO_PARA_ERROR(errno);
344         return 1;
345 }
346
347 /**
348  * Paraslash's wrapper for mmap.
349  *
350  * \param length Number of bytes to mmap.
351  * \param prot Either PROT_NONE or the bitwise OR of one or more of
352  * PROT_EXEC PROT_READ PROT_WRITE.
353  * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
354  * \param fd The file to mmap from.
355  * \param map Result pointer.
356  *
357  * \return Standard.
358  *
359  * \sa mmap(2).
360  */
361 int para_mmap(size_t length, int prot, int flags, int fd, void *map)
362 {
363         void **m = map;
364
365         errno = EINVAL;
366         if (!length)
367                 goto err;
368         *m = mmap(NULL, length, prot, flags, fd, (off_t)0);
369         if (*m != MAP_FAILED)
370                 return 1;
371 err:
372         *m = NULL;
373         return -ERRNO_TO_PARA_ERROR(errno);
374 }
375
376 /**
377  * Wrapper for the open(2) system call.
378  *
379  * \param path The filename.
380  * \param flags The usual open(2) flags.
381  * \param mode Specifies the permissions to use.
382  *
383  * The mode parameter must be specified when O_CREAT is in the flags, and is
384  * ignored otherwise.
385  *
386  * \return The file descriptor on success, negative on errors.
387  *
388  * \sa open(2).
389  */
390 int para_open(const char *path, int flags, mode_t mode)
391 {
392         int ret = open(path, flags, mode);
393
394         if (ret >= 0)
395                 return ret;
396         return -ERRNO_TO_PARA_ERROR(errno);
397 }
398
399 /**
400  * Wrapper for chdir(2).
401  *
402  * \param path The specified directory.
403  *
404  * \return Standard.
405  */
406 int para_chdir(const char *path)
407 {
408         int ret = chdir(path);
409
410         if (ret >= 0)
411                 return 1;
412         return -ERRNO_TO_PARA_ERROR(errno);
413 }
414
415 /**
416  * Save the cwd and open a given directory.
417  *
418  * \param dirname Path to the directory to open.
419  * \param dir Result pointer.
420  * \param cwd File descriptor of the current working directory.
421  *
422  * \return Standard.
423  *
424  * Opening the current directory (".") and calling fchdir() to return is
425  * usually faster and more reliable than saving cwd in some buffer and calling
426  * chdir() afterwards.
427  *
428  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
429  * stored in \a cwd. If the function returns success, and \a cwd is not \p
430  * NULL, the caller must close this file descriptor (probably after calling
431  * fchdir(*cwd)).
432  *
433  * On errors, the function undos everything, so the caller needs neither close
434  * any files, nor change back to the original working directory.
435  *
436  * \sa getcwd(3).
437  *
438  */
439 static int para_opendir(const char *dirname, DIR **dir, int *cwd)
440 {
441         int ret;
442
443         *dir = NULL;
444         if (cwd) {
445                 ret = para_open(".", O_RDONLY, 0);
446                 if (ret < 0)
447                         return ret;
448                 *cwd = ret;
449         }
450         ret = para_chdir(dirname);
451         if (ret < 0)
452                 goto close_cwd;
453         *dir = opendir(".");
454         if (*dir)
455                 return 1;
456         ret = -ERRNO_TO_PARA_ERROR(errno);
457         /* Ignore return value of fchdir() and close(). We're busted anyway. */
458         if (cwd) {
459                 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
460         }
461 close_cwd:
462         if (cwd)
463                 close(*cwd);
464         return ret;
465 }
466
467 /**
468  * A wrapper for mkdir(2).
469  *
470  * \param path Name of the directory to create.
471  * \param mode The permissions to use.
472  *
473  * \return Standard.
474  */
475 int para_mkdir(const char *path, mode_t mode)
476 {
477         if (!mkdir(path, mode))
478                 return 1;
479         return -ERRNO_TO_PARA_ERROR(errno);
480 }
481
482 /**
483  * Open a file and map it into memory.
484  *
485  * \param path Name of the regular file to map.
486  * \param open_mode Either \p O_RDONLY or \p O_RDWR.
487  * \param map On success, the mapping is returned here.
488  * \param size size of the mapping.
489  * \param fd_ptr The file descriptor of the mapping.
490  *
491  * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
492  * open call is closed after mmap().  Otherwise the file is kept open and the
493  * file descriptor is returned in \a fd_ptr.
494  *
495  * \return Standard.
496  *
497  * \sa para_open(), mmap(2).
498  */
499 int mmap_full_file(const char *path, int open_mode, void **map,
500                 size_t *size, int *fd_ptr)
501 {
502         int fd, ret, mmap_prot, mmap_flags;
503         struct stat file_status;
504
505         if (open_mode == O_RDONLY) {
506                 mmap_prot = PROT_READ;
507                 mmap_flags = MAP_PRIVATE;
508         } else {
509                 mmap_prot = PROT_READ | PROT_WRITE;
510                 mmap_flags = MAP_SHARED;
511         }
512         ret = para_open(path, open_mode, 0);
513         if (ret < 0)
514                 return ret;
515         fd = ret;
516         if (fstat(fd, &file_status) < 0) {
517                 ret = -ERRNO_TO_PARA_ERROR(errno);
518                 goto out;
519         }
520         *size = file_status.st_size;
521         /*
522          * If the file is empty, *size is zero and mmap() would return EINVAL
523          * (Invalid argument). This error is common enough to spend an extra
524          * error code which explicitly states the problem.
525          */
526         ret = -E_EMPTY;
527         if (*size == 0)
528                 goto out;
529         /*
530          * If fd refers to a directory, mmap() returns ENODEV (No such device),
531          * at least on Linux. "Is a directory" seems to be more to the point.
532          */
533         ret = -ERRNO_TO_PARA_ERROR(EISDIR);
534         if (S_ISDIR(file_status.st_mode))
535                 goto out;
536
537         ret = para_mmap(*size, mmap_prot, mmap_flags, fd, map);
538 out:
539         if (ret < 0 || !fd_ptr)
540                 close(fd);
541         else
542                 *fd_ptr = fd;
543         return ret;
544 }
545
546 /**
547  * A wrapper for munmap(2).
548  *
549  * \param start The start address of the memory mapping.
550  * \param length The size of the mapping.
551  *
552  * \return Standard.
553  *
554  * \sa munmap(2), \ref mmap_full_file().
555  */
556 int para_munmap(void *start, size_t length)
557 {
558         int err;
559
560         if (!start)
561                 return 0;
562         if (munmap(start, length) >= 0)
563                 return 1;
564         err = errno;
565         PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
566                 strerror(err));
567         return -ERRNO_TO_PARA_ERROR(err);
568 }
569
570 /**
571  * Simple wrapper for poll(2).
572  *
573  * It calls poll(2) and starts over if the call was interrupted by a signal.
574  *
575  * \param fds See poll(2).
576  * \param nfds See poll(2).
577  * \param timeout See poll(2).
578  *
579  * \return The return value of the underlying poll() call on success, the
580  * negative paraslash error code on errors.
581  *
582  * All arguments are passed verbatim to poll(2).
583  */
584 int xpoll(struct pollfd *fds, nfds_t nfds, int timeout)
585 {
586         int ret;
587
588         do
589                 ret = poll(fds, nfds, timeout);
590         while (ret < 0 && errno == EINTR);
591         return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
592 }
593
594 /**
595  * Check a file descriptor for readability.
596  *
597  * \param fd The file descriptor.
598  *
599  * \return positive if fd is ready for reading, zero if it isn't, negative if
600  * an error occurred.
601  *
602  * \sa \ref write_ok().
603  */
604 int read_ok(int fd)
605 {
606         struct pollfd pfd = {.fd = fd, .events = POLLIN};
607         int ret = xpoll(&pfd, 1, 0);
608         return ret < 0? ret : pfd.revents & POLLIN;
609 }
610
611 /**
612  * Check a file descriptor for writability.
613  *
614  * \param fd The file descriptor.
615  *
616  * \return positive if fd is ready for writing, zero if it isn't, negative if
617  * an error occurred.
618  *
619  * \sa \ref read_ok().
620  */
621 int write_ok(int fd)
622 {
623         struct pollfd pfd = {.fd = fd, .events = POLLOUT};
624         int ret = xpoll(&pfd, 1, 0);
625         return ret < 0? ret : pfd.revents & POLLOUT;
626 }
627
628 /**
629  * Ensure that file descriptors 0, 1, and 2 are valid.
630  *
631  * Common approach that opens /dev/null until it gets a file descriptor greater
632  * than two.
633  */
634 void valid_fd_012(void)
635 {
636         while (1) {
637                 int fd = open("/dev/null", O_RDWR);
638                 if (fd < 0)
639                         exit(EXIT_FAILURE);
640                 if (fd > 2) {
641                         close(fd);
642                         break;
643                 }
644         }
645 }
646
647 /**
648  * Traverse the given directory recursively.
649  *
650  * \param dirname The directory to traverse.
651  * \param func The function to call for each entry.
652  * \param private_data Pointer to an arbitrary data structure.
653  *
654  * For each regular file under \a dirname, the supplied function \a func is
655  * called.  The full path of the regular file and the \a private_data pointer
656  * are passed to \a func. Directories for which the calling process has no
657  * permissions to change to are silently ignored.
658  *
659  * \return Standard.
660  */
661 int for_each_file_in_dir(const char *dirname,
662                 int (*func)(const char *, void *), void *private_data)
663 {
664         DIR *dir;
665         struct dirent *entry;
666         int cwd_fd, ret = para_opendir(dirname, &dir, &cwd_fd);
667
668         if (ret < 0)
669                 return ret == -ERRNO_TO_PARA_ERROR(EACCES)? 1 : ret;
670         /* scan cwd recursively */
671         while ((entry = readdir(dir))) {
672                 mode_t m;
673                 char *tmp;
674                 struct stat s;
675
676                 if (!strcmp(entry->d_name, "."))
677                         continue;
678                 if (!strcmp(entry->d_name, ".."))
679                         continue;
680                 if (lstat(entry->d_name, &s) == -1)
681                         continue;
682                 m = s.st_mode;
683                 if (!S_ISREG(m) && !S_ISDIR(m))
684                         continue;
685                 tmp = make_message("%s/%s", dirname, entry->d_name);
686                 if (!S_ISDIR(m)) {
687                         ret = func(tmp, private_data);
688                         free(tmp);
689                         if (ret < 0)
690                                 goto out;
691                         continue;
692                 }
693                 /* directory */
694                 ret = for_each_file_in_dir(tmp, func, private_data);
695                 free(tmp);
696                 if (ret < 0)
697                         goto out;
698         }
699         ret = 1;
700 out:
701         closedir(dir);
702         if (fchdir(cwd_fd) < 0 && ret >= 0)
703                 ret = -ERRNO_TO_PARA_ERROR(errno);
704         close(cwd_fd);
705         return ret;
706 }