2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.5 2005/08/08 15:00:12 joerg Exp $
36 * @(#)uucplock.c 8.1 (Berkeley) 6/6/93
39 #include <sys/types.h>
56 #define LOCKTMP "LCKTMP..%d"
57 #define LOCKFMT "LCK..%s"
59 #define GORET(level, val) { err = errno; uuerr = (val); \
60 goto __CONCAT(ret, level); }
62 /* Forward declarations */
63 static int put_pid (int fd
, pid_t pid
);
64 static pid_t
get_pid (int fd
,int *err
);
67 * uucp style locking routines
71 uu_lock(const char *tty_name
)
75 char lckname
[PATH_MAX
],
80 (void)snprintf(lcktmpname
, sizeof(lcktmpname
), _PATH_UUCPLOCK LOCKTMP
,
82 (void)snprintf(lckname
, sizeof(lckname
), _PATH_UUCPLOCK LOCKFMT
,
84 if ((tmpfd
= creat(lcktmpname
, 0664)) < 0)
85 GORET(0, UU_LOCK_CREAT_ERR
);
87 for (i
= 0; i
< MAXTRIES
; i
++) {
88 if (link (lcktmpname
, lckname
) < 0) {
90 GORET(1, UU_LOCK_LINK_ERR
);
92 * file is already locked
93 * check to see if the process holding the lock
96 if ((fd
= open(lckname
, O_RDONLY
)) < 0)
97 GORET(1, UU_LOCK_OPEN_ERR
);
99 if ((pid_old
= get_pid (fd
, &err
)) == -1)
100 GORET(2, UU_LOCK_READ_ERR
);
104 if (kill(pid_old
, 0) == 0 || errno
!= ESRCH
)
105 GORET(1, UU_LOCK_INUSE
);
107 * The process that locked the file isn't running, so
108 * we'll lock it ourselves
110 (void)unlink(lckname
);
112 if (!put_pid (tmpfd
, pid
))
113 GORET(3, UU_LOCK_WRITE_ERR
);
117 GORET(1, (i
>= MAXTRIES
) ? UU_LOCK_TRY_ERR
: UU_LOCK_OK
);
120 (void)unlink(lckname
);
126 (void)unlink(lcktmpname
);
133 uu_lock_txfr(const char *tty_name
, pid_t pid
)
136 char lckname
[PATH_MAX
];
138 snprintf(lckname
, sizeof(lckname
), _PATH_UUCPLOCK LOCKFMT
, tty_name
);
140 if ((fd
= open(lckname
, O_RDWR
)) < 0)
141 return UU_LOCK_OWNER_ERR
;
142 if (get_pid(fd
, &err
) != getpid())
143 err
= UU_LOCK_OWNER_ERR
;
145 lseek(fd
, 0, SEEK_SET
);
146 err
= put_pid(fd
, pid
) ? 0 : UU_LOCK_WRITE_ERR
;
154 uu_unlock(const char *tty_name
)
158 (void)snprintf(tbuf
, sizeof(tbuf
), _PATH_UUCPLOCK LOCKFMT
, tty_name
);
163 uu_lockerr(int uu_lockresult
)
165 static char errbuf
[128];
168 switch (uu_lockresult
) {
170 return "device in use";
173 case UU_LOCK_OPEN_ERR
:
174 fmt
= "open error: %s";
176 case UU_LOCK_READ_ERR
:
177 fmt
= "read error: %s";
179 case UU_LOCK_CREAT_ERR
:
180 fmt
= "creat error: %s";
182 case UU_LOCK_WRITE_ERR
:
183 fmt
= "write error: %s";
185 case UU_LOCK_LINK_ERR
:
186 fmt
= "link error: %s";
188 case UU_LOCK_TRY_ERR
:
189 fmt
= "too many tries: %s";
191 case UU_LOCK_OWNER_ERR
:
192 fmt
= "not locking process: %s";
195 fmt
= "undefined error: %s";
199 (void)snprintf(errbuf
, sizeof(errbuf
), fmt
, strerror(errno
));
204 put_pid(int fd
, pid_t pid
)
209 len
= sprintf (buf
, "%10d\n", (int)pid
);
210 return write (fd
, buf
, len
) == len
;
214 get_pid(int fd
, int *err
)
220 bytes_read
= read (fd
, buf
, sizeof (buf
) - 1);
221 if (bytes_read
> 0) {
222 buf
[bytes_read
] = '\0';
223 pid
= strtol (buf
, (char **) NULL
, 10);
226 *err
= bytes_read
? errno
: EINVAL
;
231 /* end of uucplock.c */