2 * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fd.c Helper functions for file descriptor handling. */
10 #include <sys/types.h>
22 * Write a buffer to a file descriptor, re-write on short writes.
24 * \param fd The file descriptor.
25 * \param buf The buffer to be sent.
26 * \param len The length of \a buf.
30 int write_all(int fd, const char *buf, size_t len)
37 int ret = write(fd, buf + len, total - len);
39 return -ERRNO_TO_PARA_ERROR(errno);
46 * Send a buffer given by a format string.
48 * \param fd The file descriptor.
49 * \param fmt A format string.
53 __printf_2_3 int write_va_buffer(int fd, const char *fmt, ...)
58 PARA_VSPRINTF(fmt, msg);
59 ret = write_buffer(fd, msg);
65 * Write a buffer to a file descriptor, re-writing on short writes.
67 * \param fd The file descriptor.
68 * \param buf The buffer to write.
69 * \param len The number of bytes to write.
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.
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
83 int write_nonblock(int fd, const char *buf, size_t len)
87 while (written < len) {
88 ssize_t ret = write(fd, buf + written, len - written);
95 * The write() call was interrupted by a signal before
96 * any data was written. Try again.
99 if (errno == EAGAIN || errno == EWOULDBLOCK)
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.
107 return -ERRNO_TO_PARA_ERROR(errno);
113 * Read from a non-blocking file descriptor into multiple buffers.
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.
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
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.
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
137 * \sa \ref write_nonblock(), read(2), readv(2).
139 int readv_nonblock(int fd, struct iovec *iov, int iovcnt, fd_set *rfds,
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.
152 if (rfds && !FD_ISSET(fd, rfds))
155 for (i = 0, j = 0; i < iovcnt;) {
157 /* fix up the first iov */
158 assert(j < iov[i].iov_len);
159 iov[i].iov_base += j;
161 ret = readv(fd, iov + i, iovcnt - i);
162 iov[i].iov_base -= j;
170 return -ERRNO_TO_PARA_ERROR(errno);
174 if (ret < iov[i].iov_len - j) {
178 ret -= iov[i].iov_len - j;
188 * Read from a non-blocking file descriptor into a single buffer.
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().
196 * This is a simple wrapper for readv_nonblock() which uses an iovec with a single
199 * \return The return value of the underlying call to readv_nonblock().
201 int read_nonblock(int fd, void *buf, size_t sz, fd_set *rfds, size_t *num_bytes)
203 struct iovec iov = {.iov_base = buf, .iov_len = sz};
204 return readv_nonblock(fd, &iov, 1, rfds, num_bytes);
208 * Read a buffer and check its content for a pattern.
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().
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,
220 * \return Positive if \a pattern was received, negative on errors, zero if no data
221 * was available to read.
223 * \sa \ref read_nonblock(), \sa strncasecmp(3).
225 int read_pattern(int fd, const char *pattern, size_t bufsize, fd_set *rfds)
228 char *buf = para_malloc(bufsize + 1);
229 int ret = read_nonblock(fd, buf, bufsize, rfds, &n);
237 ret = -E_READ_PATTERN;
238 len = strlen(pattern);
241 if (strncasecmp(buf, pattern, len) != 0)
246 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
247 PARA_NOTICE_LOG("recvd %zu bytes: %s\n", n, buf);
254 * Check whether a file exists.
256 * \param fn The file name.
258 * \return Non-zero iff file exists.
260 int file_exists(const char *fn)
264 return !stat(fn, &statbuf);
268 * Paraslash's wrapper for select(2).
270 * It calls select(2) (with no exceptfds) and starts over if select() was
271 * interrupted by a signal.
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()
279 * \return The return value of the underlying select() call on success, the
280 * negative system error code on errors.
282 * All arguments are passed verbatim to select(2).
283 * \sa select(2) select_tut(2).
285 int para_select(int n, fd_set *readfds, fd_set *writefds,
286 struct timeval *timeout_tv)
290 ret = select(n, readfds, writefds, NULL, timeout_tv);
291 while (ret < 0 && errno == EINTR);
293 return -ERRNO_TO_PARA_ERROR(errno);
298 * Set a file descriptor to blocking mode.
300 * \param fd The file descriptor.
304 __must_check int mark_fd_blocking(int fd)
306 int flags = fcntl(fd, F_GETFL);
308 return -ERRNO_TO_PARA_ERROR(errno);
309 flags = fcntl(fd, F_SETFL, ((long)flags) & ~O_NONBLOCK);
311 return -ERRNO_TO_PARA_ERROR(errno);
316 * Set a file descriptor to non-blocking mode.
318 * \param fd The file descriptor.
322 __must_check int mark_fd_nonblocking(int fd)
324 int flags = fcntl(fd, F_GETFL);
326 return -ERRNO_TO_PARA_ERROR(errno);
327 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
329 return -ERRNO_TO_PARA_ERROR(errno);
334 * Set a file descriptor in a fd_set.
336 * \param fd The file descriptor to be set.
337 * \param fds The file descriptor set.
338 * \param max_fileno Highest-numbered file descriptor.
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.
345 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
347 assert(fd >= 0 && fd < FD_SETSIZE);
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);
358 *max_fileno = PARA_MAX(*max_fileno, fd);
362 * Paraslash's wrapper for fgets(3).
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.
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.
372 __must_check int para_fgets(char *line, int size, FILE *f)
375 if (fgets(line, size, f))
381 if (errno != EINTR) {
382 PARA_ERROR_LOG("%s\n", strerror(errno));
390 * Paraslash's wrapper for mmap.
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.
404 int para_mmap(size_t length, int prot, int flags, int fd, off_t offset,
412 *m = mmap(NULL, length, prot, flags, fd, offset);
413 if (*m != MAP_FAILED)
417 return -ERRNO_TO_PARA_ERROR(errno);
421 * Wrapper for the open(2) system call.
423 * \param path The filename.
424 * \param flags The usual open(2) flags.
425 * \param mode Specifies the permissions to use.
427 * The mode parameter must be specified when O_CREAT is in the flags, and is
430 * \return The file descriptor on success, negative on errors.
434 int para_open(const char *path, int flags, mode_t mode)
436 int ret = open(path, flags, mode);
440 return -ERRNO_TO_PARA_ERROR(errno);
444 * Wrapper for chdir(2).
446 * \param path The specified directory.
450 int para_chdir(const char *path)
452 int ret = chdir(path);
456 return -ERRNO_TO_PARA_ERROR(errno);
460 * Save the cwd and open a given directory.
462 * \param dirname Path to the directory to open.
463 * \param dir Result pointer.
464 * \param cwd File descriptor of the current working directory.
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.
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
477 * On errors, the function undos everything, so the caller needs neither close
478 * any files, nor change back to the original working directory.
483 static int para_opendir(const char *dirname, DIR **dir, int *cwd)
488 ret = para_open(".", O_RDONLY, 0);
493 ret = para_chdir(dirname);
499 ret = -ERRNO_TO_PARA_ERROR(errno);
500 /* Ignore return value of fchdir() and close(). We're busted anyway. */
502 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
511 * A wrapper for fchdir().
513 * \param fd An open file descriptor.
517 static int para_fchdir(int fd)
520 return -ERRNO_TO_PARA_ERROR(errno);
525 * A wrapper for mkdir(2).
527 * \param path Name of the directory to create.
528 * \param mode The permissions to use.
532 int para_mkdir(const char *path, mode_t mode)
534 if (!mkdir(path, mode))
536 return -ERRNO_TO_PARA_ERROR(errno);
540 * Open a file and map it into memory.
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.
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.
554 * \sa para_open(), mmap(2).
556 int mmap_full_file(const char *path, int open_mode, void **map,
557 size_t *size, int *fd_ptr)
559 int fd, ret, mmap_prot, mmap_flags;
560 struct stat file_status;
562 if (open_mode == O_RDONLY) {
563 mmap_prot = PROT_READ;
564 mmap_flags = MAP_PRIVATE;
566 mmap_prot = PROT_READ | PROT_WRITE;
567 mmap_flags = MAP_SHARED;
569 ret = para_open(path, open_mode, 0);
573 if (fstat(fd, &file_status) < 0) {
574 ret = -ERRNO_TO_PARA_ERROR(errno);
577 *size = file_status.st_size;
578 ret = para_mmap(*size, mmap_prot, mmap_flags, fd, 0, map);
580 if (ret < 0 || !fd_ptr)
588 * A wrapper for munmap(2).
590 * \param start The start address of the memory mapping.
591 * \param length The size of the mapping.
595 * \sa munmap(2), mmap_full_file().
597 int para_munmap(void *start, size_t length)
603 if (munmap(start, length) >= 0)
606 PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
608 return -ERRNO_TO_PARA_ERROR(err);
612 * Check a file descriptor for writability.
614 * \param fd The file descriptor.
616 * \return positive if fd is ready for writing, zero if it isn't, negative if
629 return para_select(fd + 1, NULL, &wfds, &tv);
633 * Ensure that file descriptors 0, 1, and 2 are valid.
635 * Common approach that opens /dev/null until it gets a file descriptor greater
638 * \sa okir's Black Hats Manual.
640 void valid_fd_012(void)
643 int fd = open("/dev/null", O_RDWR);
654 * Traverse the given directory recursively.
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.
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.
667 int for_each_file_in_dir(const char *dirname,
668 int (*func)(const char *, void *), void *private_data)
671 struct dirent *entry;
672 int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
675 return ret == -ERRNO_TO_PARA_ERROR(EACCES)? 1 : ret;
676 /* scan cwd recursively */
677 while ((entry = readdir(dir))) {
682 if (!strcmp(entry->d_name, "."))
684 if (!strcmp(entry->d_name, ".."))
686 if (lstat(entry->d_name, &s) == -1)
689 if (!S_ISREG(m) && !S_ISDIR(m))
691 tmp = make_message("%s/%s", dirname, entry->d_name);
693 ret = func(tmp, private_data);
700 ret = for_each_file_in_dir(tmp, func, private_data);
708 ret2 = para_fchdir(cwd_fd);
709 if (ret2 < 0 && ret >= 0)