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