08a06589bc4294fd6c1c8f9f0ca54fdf407a96e8
2 * Copyright (C) 2006-2007 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>
18 * check whether a file exists
20 * \param fn the file name
22 * \return Non-zero iff file exists.
24 int file_exists(const char *fn
)
28 return !stat(fn
, &statbuf
);
32 * Paraslash's wrapper for select(2).
34 * It calls select(2) (with no exceptfds) and starts over if select() was
35 * interrupted by a signal.
37 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
38 * \param readfds fds that should be checked for readability.
39 * \param writefds fds that should be checked for writablility.
40 * \param timeout_tv upper bound on the amount of time elapsed before select()
43 * \return The return value of the underlying select() call on success, the
44 * negative system error code on errors.
46 * All arguments are passed verbatim to select(2).
47 * \sa select(2) select_tut(2).
49 int para_select(int n
, fd_set
*readfds
, fd_set
*writefds
,
50 struct timeval
*timeout_tv
)
54 ret
= select(n
, readfds
, writefds
, NULL
, timeout_tv
);
56 } while (ret
< 0 && err
== EINTR
);
58 return -ERRNO_TO_PARA_ERROR(errno
);
63 * Set a file descriptor to non-blocking mode.
65 * \param fd The file descriptor.
69 int mark_fd_nonblock(int fd
)
71 int flags
= fcntl(fd
, F_GETFL
);
73 return -ERRNO_TO_PARA_ERROR(errno
);
74 flags
= fcntl(fd
, F_SETFL
, ((long)flags
) | O_NONBLOCK
);
76 return -ERRNO_TO_PARA_ERROR(errno
);
81 * set a file descriptor in a fd_set
83 * \param fd the file descriptor to be set
84 * \param fds the file descriptor set
85 * \param max_fileno highest-numbered file descriptor
87 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
88 * return, \a max_fileno contains the maximum of the old_value and \a fd.
92 void para_fd_set(int fd
, fd_set
*fds
, int *max_fileno
)
95 if (fd
< 0 || fd
>= FD_SETSIZE
) {
96 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd
);
101 int flags
= fcntl(fd
, F_GETFL
);
102 if (!(flags
& O_NONBLOCK
)) {
103 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd
);
109 *max_fileno
= PARA_MAX(*max_fileno
, fd
);
113 * paraslash's wrapper for fgets(3)
114 * \param line pointer to the buffer to store the line
115 * \param size the size of the buffer given by \a line
116 * \param f the stream to read from
118 * \return Unlike the standard fgets() function, an integer value
119 * is returned. On success, this function returns 1. On errors, -E_FGETS
120 * is returned. A zero return value indicates an end of file condition.
122 __must_check
int para_fgets(char *line
, int size
, FILE *f
)
125 if (fgets(line
, size
, f
))
131 if (errno
!= EINTR
) {
132 PARA_ERROR_LOG("%s\n", strerror(errno
));
140 * Paraslash's wrapper for mmap.
142 * \param length number of bytes to mmap
143 * \param prot either PROT_NONE or the bitwise OR of one or more of
144 * PROT_EXEC PROT_READ PROT_WRITE
145 * \param flags exactly one of MAP_SHARED and MAP_PRIVATE
146 * \param fd the file to mmap from
147 * \param offset mmap start
149 * \return This function either returns a valid pointer to the mapped area
150 * or calls exit() on errors.
152 void *para_mmap(size_t length
, int prot
, int flags
, int fd
, off_t offset
)
154 void *ret
= mmap(NULL
, length
, prot
, flags
, fd
, offset
);
155 if (ret
!= MAP_FAILED
)
157 PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno
));
158 PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
159 length
, flags
, fd
, (size_t)offset
);
164 * Wrapper for the open(2) system call.
166 * \param path The filename.
167 * \param flags The usual open(2) flags.
168 * \param mode Specifies the permissions to use.
170 * The mode parameter must be specified when O_CREAT is in the flags, and is ignored
173 * \return The file descriptor on success, negative on errors.
177 int para_open(const char *path
, int flags
, mode_t mode
)
179 int ret
= open(path
, flags
, mode
);
183 return -ERRNO_TO_PARA_ERROR(errno
);
187 * Wrapper for chdir(2).
189 * \param path the specified directory.
191 * \return Positive on success, negative on errors.
193 int para_chdir(const char *path
)
195 int ret
= chdir(path
);
199 return -ERRNO_TO_PARA_ERROR(errno
);
203 * Save the cwd and open a given directory.
205 * \param dirname Path to the directory to open.
206 * \param dir Result pointer.
207 * \param cwd File descriptor of the current working directory.
209 * \return Positive on success, negative on errors.
211 * Opening the current directory (".") and calling fchdir() to return is
212 * usually faster and more reliable than saving cwd in some buffer and calling
213 * chdir() afterwards.
215 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
216 * stored in \a cwd. If the function returns success, and \a cwd is not \p
217 * NULL, the caller must close this file descriptor (probably after calling
220 * On errors, the function undos everything, so the caller needs neither close
221 * any files, nor change back to the original working directory.
226 int para_opendir(const char *dirname
, DIR **dir
, int *cwd
)
231 ret
= para_open(".", O_RDONLY
, 0);
236 ret
= para_chdir(dirname
);
242 goto change_to_orig_dir
;
244 /* Ignore return value of fchdir() and close(). We're busted anyway. */
255 * A wrapper for fchdir().
257 * \param fd An open file descriptor
259 * \return Positive on success, negative on errors.
261 int para_fchdir(int fd
)
269 * A wrapper for mkdir(2).
271 * \param path Name of the directory to create.
272 * \param mode The permissions to use.
276 int para_mkdir(const char *path
, mode_t mode
)
278 if (!mkdir(path
, mode
))
280 return -ERRNO_TO_PARA_ERROR(errno
);