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