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