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