2 * Copyright (C) 2006-2008 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. */
13 #include <sys/select.h>
19 * Wrapper for the write system call.
21 * \param fd The file descriptor to write to.
22 * \param buf The buffer to write.
23 * \param size The length of \a buf in bytes.
25 * This function writes out the given buffer and retries if an interrupt
26 * occurred during the write.
28 * \return On success, the number of bytes written is returned, otherwise, the
29 * function returns \p -E_WRITE.
33 ssize_t para_write(int fd, const void *buf, size_t size)
38 ret = write(fd, buf, size);
39 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
41 return ret >= 0? ret : -E_WRITE;
47 * Write the whole buffer to a file descriptor.
49 * \param fd The file descriptor to write to.
50 * \param buf The buffer to write.
51 * \param size The length of \a buf in bytes.
53 * This function writes the given buffer and continues on short writes and
54 * when interrupted by a signal.
56 * \return Positive on success, negative on errors. Possible errors: any
57 * errors returned by para_write().
61 ssize_t write_all(int fd, const void *buf, size_t size)
63 // DEBUG_LOG("writing %zu bytes\n", size);
66 ssize_t ret = para_write(fd, b, size);
67 // DEBUG_LOG("ret: %zd\n", ret);
77 * Wrapper for the open(2) system call.
79 * \param path The filename.
80 * \param flags The usual open(2) flags.
81 * \param mode Specifies the permissions to use.
83 * The mode parameter must be specified when O_CREAT is in the flags, and is
86 * \return The file descriptor on success, negative on errors.
90 int para_open(const char *path, int flags, mode_t mode)
92 int ret = open(path, flags, mode);
96 return -ERRNO_TO_ERROR(errno);
100 * Open a file, write the given buffer and close the file.
102 * \param filename Full path to the file to open.
103 * \param buf The buffer to write to the file.
104 * \param size The size of \a buf.
106 * \return Positive on success, negative on errors. Possible errors include:
107 * any errors from para_open() or para_write().
109 * \sa para_open(), para_write().
111 int para_write_file(const char *filename, const void *buf, size_t size)
115 ret = para_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
119 ret = write_all(fd, buf, size);
130 * Check whether a file exists.
132 * \param fn The file name.
134 * \return Non-zero iff file exists.
136 int file_exists(const char *fn)
140 return !stat(fn, &statbuf);
144 * Paraslash's wrapper for select(2).
146 * It calls select(2) (with no exceptfds) and starts over if select() was
147 * interrupted by a signal.
149 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
150 * \param readfds fds that should be checked for readability.
151 * \param writefds fds that should be checked for writablility.
152 * \param timeout_tv upper bound on the amount of time elapsed before select()
155 * \return The return value of the underlying select() call on success, the
156 * negative system error code on errors.
158 * All arguments are passed verbatim to select(2).
159 * \sa select(2) select_tut(2).
161 int para_select(int n, fd_set *readfds, fd_set *writefds,
162 struct timeval *timeout_tv)
166 ret = select(n, readfds, writefds, NULL, timeout_tv);
168 } while (ret < 0 && err == EINTR);
170 return -ERRNO_TO_ERROR(errno);
175 * Set a file descriptor to blocking mode.
177 * \param fd The file descriptor.
181 __must_check int mark_fd_blocking(int fd)
183 int flags = fcntl(fd, F_GETFL);
185 return -ERRNO_TO_ERROR(errno);
186 flags = fcntl(fd, F_SETFL, ((long)flags) & ~O_NONBLOCK);
188 return -ERRNO_TO_ERROR(errno);
193 * Set a file descriptor to non-blocking mode.
195 * \param fd The file descriptor.
199 __must_check int mark_fd_nonblocking(int fd)
201 int flags = fcntl(fd, F_GETFL);
203 return -ERRNO_TO_ERROR(errno);
204 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
206 return -ERRNO_TO_ERROR(errno);
211 * Set a file descriptor in a fd_set.
213 * \param fd The file descriptor to be set.
214 * \param fds The file descriptor set.
215 * \param max_fileno Highest-numbered file descriptor.
217 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
218 * return, \a max_fileno contains the maximum of the old_value and \a fd.
222 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
225 if (fd < 0 || fd >= FD_SETSIZE) {
226 EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
231 int flags = fcntl(fd, F_GETFL);
232 if (!(flags & O_NONBLOCK)) {
233 EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
239 *max_fileno = MAX(*max_fileno, fd);
243 * Paraslash's wrapper for fgets(3).
245 * \param line Pointer to the buffer to store the line.
246 * \param size The size of the buffer given by \a line.
247 * \param f The stream to read from.
249 * \return Unlike the standard fgets() function, an integer value
250 * is returned. On success, this function returns 1. On errors, -E_FGETS
251 * is returned. A zero return value indicates an end of file condition.
253 __must_check int para_fgets(char *line, int size, FILE *f)
256 if (fgets(line, size, f))
262 if (errno != EINTR) {
263 ERROR_LOG("%s\n", strerror(errno));
271 * Paraslash's wrapper for mmap.
273 * \param length Number of bytes to mmap.
274 * \param prot Either PROT_NONE or the bitwise OR of one or more of
275 * PROT_EXEC PROT_READ PROT_WRITE.
276 * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
277 * \param fd The file to mmap from.
278 * \param offset Mmap start.
280 * \return This function either returns a valid pointer to the mapped area
281 * or calls exit() on errors.
283 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
285 void *ret = mmap(NULL, length, prot, flags, fd, offset);
286 if (ret != MAP_FAILED)
288 EMERG_LOG("mmap failed: %s\n", strerror(errno));
289 EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
290 length, flags, fd, (size_t)offset);
295 * Wrapper for chdir(2).
297 * \param path The specified directory.
301 int para_chdir(const char *path)
303 int ret = chdir(path);
307 return -ERRNO_TO_ERROR(errno);
311 * Save the cwd and open a given directory.
313 * \param dirname Path to the directory to open.
314 * \param dir Result pointer.
315 * \param cwd File descriptor of the current working directory.
319 * Opening the current directory (".") and calling fchdir() to return is
320 * usually faster and more reliable than saving cwd in some buffer and calling
321 * chdir() afterwards.
323 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
324 * stored in \a cwd. If the function returns success, and \a cwd is not \p
325 * NULL, the caller must close this file descriptor (probably after calling
328 * On errors, the function undos everything, so the caller needs neither close
329 * any files, nor change back to the original working directory.
334 int para_opendir(const char *dirname, DIR **dir, int *cwd)
339 ret = para_open(".", O_RDONLY, 0);
344 ret = para_chdir(dirname);
350 ret = -ERRNO_TO_ERROR(errno);
351 /* Ignore return value of fchdir() and close(). We're busted anyway. */
361 * A wrapper for fchdir().
363 * \param fd An open file descriptor.
367 int para_fchdir(int fd)
370 return -ERRNO_TO_ERROR(errno);
375 * A wrapper for mkdir(2).
377 * \param path Name of the directory to create.
378 * \param mode The permissions to use.
382 int para_mkdir(const char *path, mode_t mode)
384 if (!mkdir(path, mode))
386 return -ERRNO_TO_ERROR(errno);
390 * Open a file and map it into memory.
392 * \param path Name of the regular file to map.
393 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
394 * \param map On success, the mapping is returned here.
395 * \param size size of the mapping.
396 * \param fd_ptr The file descriptor of the mapping.
398 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
399 * open call is closed after mmap(). Otherwise the file is kept open and the
400 * file descriptor is returned in \a fd_ptr.
404 * \sa para_open(), mmap(2).
406 int mmap_full_file(const char *path, int open_mode, void **map,
407 size_t *size, int *fd_ptr)
409 int fd, ret, mmap_prot, mmap_flags;
410 struct stat file_status;
412 if (open_mode == O_RDONLY) {
413 mmap_prot = PROT_READ;
414 mmap_flags = MAP_PRIVATE;
416 mmap_prot = PROT_READ | PROT_WRITE;
417 mmap_flags = MAP_SHARED;
419 ret = para_open(path, open_mode, 0);
423 if (fstat(fd, &file_status) < 0) {
424 ret = -ERRNO_TO_ERROR(errno);
427 *size = file_status.st_size;
429 DEBUG_LOG("%s: size %zu\n", path, *size);
432 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
433 if (*map == MAP_FAILED) {
440 if (ret < 0 || !fd_ptr)
448 * A wrapper for munmap(2).
450 * \param start The start address of the memory mapping.
451 * \param length The size of the mapping.
453 * \return Positive on success, \p -E_MUNMAP on errors.
455 * \sa munmap(2), mmap_full_file().
457 int para_munmap(void *start, size_t length)
460 if (munmap(start, length) >= 0)
463 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
465 return -ERRNO_TO_ERROR(err);
469 * Check a file descriptor for writability.
471 * \param fd The file descriptor.
473 * \return positive if fd is ready for writing, zero if it isn't, negative if
479 struct timeval tv = {0, 0};
487 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
488 if (ret < 0 && errno == EINTR)