Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.5 KiB

22 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
22 years ago
20 years ago
  1. /* $OpenBSD: uucplock.c,v 1.21 2019/07/03 03:24:04 deraadt Exp $ */
  2. /*
  3. * Copyright (c) 1988, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. *
  31. */
  32. #include <sys/types.h>
  33. #include <dirent.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <signal.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <paths.h>
  41. #include <string.h>
  42. #include "util.h"
  43. #define MAXTRIES 5
  44. #define LOCKTMP "LCKTMP..%ld"
  45. #define LOCKFMT "LCK..%s"
  46. #define GORET(level, val) { err = errno; uuerr = (val); \
  47. goto __CONCAT(ret, level); }
  48. /* Forward declarations */
  49. static int put_pid(int fd, pid_t pid);
  50. static pid_t get_pid(int fd,int *err);
  51. /*
  52. * uucp style locking routines
  53. */
  54. int
  55. uu_lock(const char *ttyname)
  56. {
  57. char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN],
  58. lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
  59. int fd, tmpfd, i, err, uuerr;
  60. pid_t pid, pid_old;
  61. pid = getpid();
  62. (void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP,
  63. (long)pid);
  64. (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT,
  65. ttyname);
  66. tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
  67. if (tmpfd == -1)
  68. GORET(0, UU_LOCK_CREAT_ERR);
  69. for (i = 0; i < MAXTRIES; i++) {
  70. if (link(lcktmpname, lckname) == -1) {
  71. if (errno != EEXIST)
  72. GORET(1, UU_LOCK_LINK_ERR);
  73. /*
  74. * file is already locked
  75. * check to see if the process holding the lock
  76. * still exists
  77. */
  78. if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) == -1)
  79. GORET(1, UU_LOCK_OPEN_ERR);
  80. if ((pid_old = get_pid(fd, &err)) == -1)
  81. GORET(2, UU_LOCK_READ_ERR);
  82. close(fd);
  83. if (kill(pid_old, 0) == 0 || errno != ESRCH)
  84. GORET(1, UU_LOCK_INUSE);
  85. /*
  86. * The process that locked the file isn't running, so
  87. * we'll lock it ourselves
  88. */
  89. (void)unlink(lckname);
  90. } else {
  91. if (!put_pid(tmpfd, pid))
  92. GORET(3, UU_LOCK_WRITE_ERR);
  93. break;
  94. }
  95. }
  96. GORET(1, (i >= MAXTRIES) ? UU_LOCK_TRY_ERR : UU_LOCK_OK);
  97. ret3:
  98. (void)unlink(lckname);
  99. goto ret1;
  100. ret2:
  101. (void)close(fd);
  102. ret1:
  103. (void)close(tmpfd);
  104. (void)unlink(lcktmpname);
  105. ret0:
  106. errno = err;
  107. return uuerr;
  108. }
  109. int
  110. uu_lock_txfr(const char *ttyname, pid_t pid)
  111. {
  112. char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
  113. int fd, err, ret;
  114. snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname);
  115. if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) == -1)
  116. return UU_LOCK_OWNER_ERR;
  117. if (get_pid(fd, &err) != getpid())
  118. ret = UU_LOCK_OWNER_ERR;
  119. else {
  120. lseek(fd, 0, SEEK_SET);
  121. ret = put_pid(fd, pid) ? UU_LOCK_OK : UU_LOCK_WRITE_ERR;
  122. }
  123. close(fd);
  124. return ret;
  125. }
  126. int
  127. uu_unlock(const char *ttyname)
  128. {
  129. char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
  130. (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname);
  131. return unlink(tbuf);
  132. }
  133. const char *
  134. uu_lockerr(int uu_lockresult)
  135. {
  136. static char errbuf[128];
  137. const char *err;
  138. switch (uu_lockresult) {
  139. case UU_LOCK_INUSE:
  140. return "device in use";
  141. case UU_LOCK_OK:
  142. return "";
  143. case UU_LOCK_OPEN_ERR:
  144. err = "open error";
  145. break;
  146. case UU_LOCK_READ_ERR:
  147. err = "read error";
  148. break;
  149. case UU_LOCK_CREAT_ERR:
  150. err = "creat error";
  151. break;
  152. case UU_LOCK_WRITE_ERR:
  153. err = "write error";
  154. break;
  155. case UU_LOCK_LINK_ERR:
  156. err = "link error";
  157. break;
  158. case UU_LOCK_TRY_ERR:
  159. err = "too many tries";
  160. break;
  161. case UU_LOCK_OWNER_ERR:
  162. err = "not locking process";
  163. break;
  164. default:
  165. err = "undefined error";
  166. break;
  167. }
  168. (void)snprintf(errbuf, sizeof(errbuf), "%s: %s", err, strerror(errno));
  169. return errbuf;
  170. }
  171. static int
  172. put_pid(int fd, pid_t pid)
  173. {
  174. char buf[32];
  175. int len;
  176. len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid);
  177. if (len < 0 || len >= sizeof buf)
  178. return 0;
  179. if (write(fd, buf, len) != len)
  180. return 0;
  181. /* We don't mind too much if ftruncate() fails - see get_pid */
  182. ftruncate(fd, (off_t)len);
  183. return 1;
  184. }
  185. static pid_t
  186. get_pid(int fd, int *err)
  187. {
  188. ssize_t bytes_read;
  189. char buf[32];
  190. pid_t pid;
  191. bytes_read = read(fd, buf, sizeof (buf) - 1);
  192. if (bytes_read > 0) {
  193. buf[bytes_read] = '\0';
  194. pid = (pid_t)strtoul(buf, (char **) NULL, 10);
  195. } else {
  196. pid = -1;
  197. *err = bytes_read ? errno : EINVAL;
  198. }
  199. return pid;
  200. }