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 * Write a buffer to a file descriptor, re-write on short writes.
21 * \param fd The file descriptor.
22 * \param buf The buffer to be sent.
23 * \param len The length of \a buf.
25 * \return Standard. In any case, the number of bytes that have been written is
28 int write_all(int fd
, const char *buf
, size_t *len
)
34 while (*len
< total
) {
35 int ret
= write(fd
, buf
+ *len
, total
- *len
);
37 return -ERRNO_TO_PARA_ERROR(errno
);
44 * Check whether a file exists.
46 * \param fn The file name.
48 * \return Non-zero iff file exists.
50 int file_exists(const char *fn
)
54 return !stat(fn
, &statbuf
);
58 * Paraslash's wrapper for select(2).
60 * It calls select(2) (with no exceptfds) and starts over if select() was
61 * interrupted by a signal.
63 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
64 * \param readfds fds that should be checked for readability.
65 * \param writefds fds that should be checked for writablility.
66 * \param timeout_tv upper bound on the amount of time elapsed before select()
69 * \return The return value of the underlying select() call on success, the
70 * negative system error code on errors.
72 * All arguments are passed verbatim to select(2).
73 * \sa select(2) select_tut(2).
75 int para_select(int n
, fd_set
*readfds
, fd_set
*writefds
,
76 struct timeval
*timeout_tv
)
80 ret
= select(n
, readfds
, writefds
, NULL
, timeout_tv
);
82 } while (ret
< 0 && err
== EINTR
);
84 return -ERRNO_TO_PARA_ERROR(errno
);
89 * Set a file descriptor to blocking mode.
91 * \param fd The file descriptor.
95 __must_check
int mark_fd_blocking(int fd
)
97 int flags
= fcntl(fd
, F_GETFL
);
99 return -ERRNO_TO_PARA_ERROR(errno
);
100 flags
= fcntl(fd
, F_SETFL
, ((long)flags
) & ~O_NONBLOCK
);
102 return -ERRNO_TO_PARA_ERROR(errno
);
107 * Set a file descriptor to non-blocking mode.
109 * \param fd The file descriptor.
113 __must_check
int mark_fd_nonblocking(int fd
)
115 int flags
= fcntl(fd
, F_GETFL
);
117 return -ERRNO_TO_PARA_ERROR(errno
);
118 flags
= fcntl(fd
, F_SETFL
, ((long)flags
) | O_NONBLOCK
);
120 return -ERRNO_TO_PARA_ERROR(errno
);
125 * Set a file descriptor in a fd_set.
127 * \param fd The file descriptor to be set.
128 * \param fds The file descriptor set.
129 * \param max_fileno Highest-numbered file descriptor.
131 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
132 * return, \a max_fileno contains the maximum of the old_value and \a fd.
136 void para_fd_set(int fd
, fd_set
*fds
, int *max_fileno
)
139 if (fd
< 0 || fd
>= FD_SETSIZE
) {
140 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd
);
145 int flags
= fcntl(fd
, F_GETFL
);
146 if (!(flags
& O_NONBLOCK
)) {
147 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd
);
153 *max_fileno
= PARA_MAX(*max_fileno
, fd
);
157 * Paraslash's wrapper for fgets(3).
159 * \param line Pointer to the buffer to store the line.
160 * \param size The size of the buffer given by \a line.
161 * \param f The stream to read from.
163 * \return Unlike the standard fgets() function, an integer value
164 * is returned. On success, this function returns 1. On errors, -E_FGETS
165 * is returned. A zero return value indicates an end of file condition.
167 __must_check
int para_fgets(char *line
, int size
, FILE *f
)
170 if (fgets(line
, size
, f
))
176 if (errno
!= EINTR
) {
177 PARA_ERROR_LOG("%s\n", strerror(errno
));
185 * Paraslash's wrapper for mmap.
187 * \param length Number of bytes to mmap.
188 * \param prot Either PROT_NONE or the bitwise OR of one or more of
189 * PROT_EXEC PROT_READ PROT_WRITE.
190 * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
191 * \param fd The file to mmap from.
192 * \param offset Mmap start.
194 * \return This function either returns a valid pointer to the mapped area
195 * or calls exit() on errors.
197 void *para_mmap(size_t length
, int prot
, int flags
, int fd
, off_t offset
)
199 void *ret
= mmap(NULL
, length
, prot
, flags
, fd
, offset
);
200 if (ret
!= MAP_FAILED
)
202 PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno
));
203 PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
204 length
, flags
, fd
, (size_t)offset
);
209 * Wrapper for the open(2) system call.
211 * \param path The filename.
212 * \param flags The usual open(2) flags.
213 * \param mode Specifies the permissions to use.
215 * The mode parameter must be specified when O_CREAT is in the flags, and is
218 * \return The file descriptor on success, negative on errors.
222 int para_open(const char *path
, int flags
, mode_t mode
)
224 int ret
= open(path
, flags
, mode
);
228 return -ERRNO_TO_PARA_ERROR(errno
);
232 * Wrapper for chdir(2).
234 * \param path The specified directory.
238 int para_chdir(const char *path
)
240 int ret
= chdir(path
);
244 return -ERRNO_TO_PARA_ERROR(errno
);
248 * Save the cwd and open a given directory.
250 * \param dirname Path to the directory to open.
251 * \param dir Result pointer.
252 * \param cwd File descriptor of the current working directory.
256 * Opening the current directory (".") and calling fchdir() to return is
257 * usually faster and more reliable than saving cwd in some buffer and calling
258 * chdir() afterwards.
260 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
261 * stored in \a cwd. If the function returns success, and \a cwd is not \p
262 * NULL, the caller must close this file descriptor (probably after calling
265 * On errors, the function undos everything, so the caller needs neither close
266 * any files, nor change back to the original working directory.
271 int para_opendir(const char *dirname
, DIR **dir
, int *cwd
)
276 ret
= para_open(".", O_RDONLY
, 0);
281 ret
= para_chdir(dirname
);
287 ret
= -ERRNO_TO_PARA_ERROR(errno
);
288 /* Ignore return value of fchdir() and close(). We're busted anyway. */
298 * A wrapper for fchdir().
300 * \param fd An open file descriptor.
304 int para_fchdir(int fd
)
307 return -ERRNO_TO_PARA_ERROR(errno
);
312 * A wrapper for mkdir(2).
314 * \param path Name of the directory to create.
315 * \param mode The permissions to use.
319 int para_mkdir(const char *path
, mode_t mode
)
321 if (!mkdir(path
, mode
))
323 return -ERRNO_TO_PARA_ERROR(errno
);
327 * Open a file and map it into memory.
329 * \param path Name of the regular file to map.
330 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
331 * \param map On success, the mapping is returned here.
332 * \param size size of the mapping.
333 * \param fd_ptr The file descriptor of the mapping.
335 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
336 * open call is closed after mmap(). Otherwise the file is kept open and the
337 * file descriptor is returned in \a fd_ptr.
341 * \sa para_open(), mmap(2).
343 int mmap_full_file(const char *path
, int open_mode
, void **map
,
344 size_t *size
, int *fd_ptr
)
346 int fd
, ret
, mmap_prot
, mmap_flags
;
347 struct stat file_status
;
349 if (open_mode
== O_RDONLY
) {
350 mmap_prot
= PROT_READ
;
351 mmap_flags
= MAP_PRIVATE
;
353 mmap_prot
= PROT_READ
| PROT_WRITE
;
354 mmap_flags
= MAP_SHARED
;
356 ret
= para_open(path
, open_mode
, 0);
360 if (fstat(fd
, &file_status
) < 0) {
361 ret
= -ERRNO_TO_PARA_ERROR(errno
);
364 *size
= file_status
.st_size
;
366 PARA_DEBUG_LOG("%s: size %zu\n", path
, *size
);
369 *map
= mmap(NULL
, *size
, mmap_prot
, mmap_flags
, fd
, 0);
370 if (*map
== MAP_FAILED
) {
377 if (ret
< 0 || !fd_ptr
)
385 * A wrapper for munmap(2).
387 * \param start The start address of the memory mapping.
388 * \param length The size of the mapping.
390 * \return Positive on success, \p -E_MUNMAP on errors.
392 * \sa munmap(2), mmap_full_file().
394 int para_munmap(void *start
, size_t length
)
397 if (munmap(start
, length
) >= 0)
400 PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start
, length
,
402 return -ERRNO_TO_PARA_ERROR(err
);
406 * Check a file descriptor for writability.
408 * \param fd The file descriptor.
410 * \return positive if fd is ready for writing, zero if it isn't, negative if
416 struct timeval tv
= {0, 0};
424 ret
= select(fd
+ 1, NULL
, &wfds
, NULL
, &tv
);
425 if (ret
< 0 && errno
== EINTR
)
431 * Ensure that file descriptors 0, 1, and 2 are valid.
433 * Common approach that opens /dev/null until it gets a file descriptor greater
436 * \sa okir's Black Hats Manual.
438 void valid_fd_012(void)
441 int fd
= open("/dev/null", O_RDWR
);