/src/common/fd.h

https://code.google.com/ · C++ Header · 129 lines · 21 code · 20 blank · 88 comment · 0 complexity · 62bf0ec28e2709be223f3d8041d012f8 MD5 · raw file

  1. /*****************************************************************************
  2. * $Id$
  3. *****************************************************************************
  4. * This file is part of the Munge Uid 'N' Gid Emporium (MUNGE).
  5. * For details, see <http://www.llnl.gov/linux/munge/>.
  6. * UCRL-CODE-2003-???.
  7. *
  8. * Copyright (C) 2001-2003 The Regents of the University of California.
  9. * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  10. * Written by Chris Dunlap <cdunlap@llnl.gov>.
  11. *
  12. * This is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  20. * for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License;
  23. * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  24. * Suite 330, Boston, MA 02111-1307 USA.
  25. *****************************************************************************/
  26. #ifndef FD_H
  27. #define FD_H
  28. #if HAVE_CONFIG_H
  29. # include "config.h"
  30. #endif /* HAVE_CONFIG_H */
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. int fd_set_close_on_exec (int fd);
  34. /*
  35. * Sets the file descriptor [fd] to be closed on exec().
  36. * Returns 0 on success, or -1 on error.
  37. */
  38. int fd_set_nonblocking (int fd);
  39. /*
  40. * Sets the file descriptor [fd] for non-blocking I/O.
  41. * Returns 0 on success, or -1 on error.
  42. */
  43. int fd_get_read_lock (int fd);
  44. /*
  45. * Obtain a read lock on the file specified by [fd].
  46. * Returns 0 on success, or -1 if prevented from obtaining the lock.
  47. */
  48. int fd_get_readw_lock (int fd);
  49. /*
  50. * Obtain a read lock on the file specified by [fd],
  51. * blocking until one becomes available.
  52. * Returns 0 on success, or -1 on error.
  53. */
  54. int fd_get_write_lock (int fd);
  55. /*
  56. * Obtain a write lock on the file specified by [fd].
  57. * Returns 0 on success, or -1 if prevented from obtaining the lock.
  58. */
  59. int fd_get_writew_lock (int fd);
  60. /*
  61. * Obtain a write lock on the file specified by [fd],
  62. * blocking until one becomes available.
  63. * Returns 0 on success, or -1 on error.
  64. */
  65. int fd_release_lock (int fd);
  66. /*
  67. * Release a lock held on the file specified by [fd].
  68. * Returns 0 on success, or -1 on error.
  69. */
  70. pid_t fd_is_read_lock_blocked (int fd);
  71. /*
  72. * Checks to see if a lock exists on [fd] that would block a request for a
  73. * read-lock (ie, if a write-lock is already being held on the file).
  74. * Returns the pid of the process holding the lock, 0 if no lock exists,
  75. * or -1 on error.
  76. */
  77. pid_t fd_is_write_lock_blocked (int fd);
  78. /*
  79. * Checks to see if a lock exists on [fd] that would block a request for a
  80. * write-lock (ie, if any lock is already being held on the file).
  81. * Returns the pid of the process holding the lock, 0 if no lock exists,
  82. * or -1 on error.
  83. */
  84. ssize_t fd_read_n (int fd, void *buf, size_t n);
  85. /*
  86. * Reads up to [n] bytes from [fd] into [buf].
  87. * Returns the number of bytes read, 0 on EOF, or -1 on error.
  88. */
  89. ssize_t fd_write_n (int fd, void *buf, size_t n);
  90. /*
  91. * Writes [n] bytes from [buf] to [fd].
  92. * Returns the number of bytes written, or -1 on error.
  93. */
  94. ssize_t fd_read_line (int fd, void *buf, size_t maxlen);
  95. /*
  96. * Reads at most [maxlen-1] bytes up to a newline from [fd] into [buf].
  97. * The [buf] is guaranteed to be NUL-terminated and will contain the
  98. * newline if it is encountered within [maxlen-1] bytes.
  99. * Returns the number of bytes read, 0 on EOF, or -1 on error.
  100. */
  101. ssize_t fd_null_read_n (int fd, void *buf, size_t maxlen);
  102. /*
  103. * Reads up to [n] bytes from [fd] into [buf].
  104. * Returns the number of bytes read, 0 on EOF, or -1 on error.
  105. * Differs from fd_read_n() in that it checks for the presence
  106. * a null along the partial read and breaks out if it does.
  107. * Added by Mike Haskell <mhaskell@llnl.gov>
  108. */
  109. #endif /* !FD_H */