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