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