8706 libc lint library missing endian.h functions
[unleashed.git] / usr / src / cmd / ttymon / ulockf.c
blobf4c916ab76bd70082b002ad7b5823f70b381a3ea
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include "uucp.h"
34 #include <unistd.h>
35 /* #include <sys/types.h> */
36 /* #include <sys/stat.h> */
38 static struct stat _st_buf;
39 static char lockname[BUFSIZ];
41 #ifdef V7
42 #define O_RDONLY 0
43 #endif
45 static void stlock();
46 static int onelock();
49 * make a lock file with given 'name'
50 * If one already exists, send a signal 0 to the process--if
51 * it fails, then unlink it and make a new one.
53 * input:
54 * name - name of the lock file to make
56 * return:
57 * 0 -> success
58 * FAIL -> failure
61 GLOBAL int
62 mklock(name)
63 register char *name;
65 static char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */
66 static char tempfile[MAXNAMESIZE];
68 #ifdef V8
69 char *cp;
70 #endif
72 if (pid[0] == '\0') {
73 (void) sprintf(pid, "%*ld\n", SIZEOFPID, (long) getpid());
74 (void) sprintf(tempfile, "%s/LTMP.%ld", X_LOCKDIR, (long) getpid());
77 #ifdef V8 /* this wouldn't be a problem if we used lock directories */
78 /* some day the truncation of system names will bite us */
79 cp = rindex(name, '/');
80 if (cp++ != CNULL)
81 if (strlen(cp) > MAXBASENAME)
82 *(cp+MAXBASENAME) = NULLCHAR;
83 #endif /* V8 */
84 if (onelock(pid, tempfile, name) == -1) {
85 (void) unlink(tempfile);
86 if (cklock(name))
87 return(FAIL);
88 else {
89 if (onelock(pid, tempfile, name)) {
90 (void) unlink(tempfile);
91 DEBUG(4,"ulockf failed in onelock()\n%s", "");
92 return(FAIL);
97 stlock(name);
98 return(0);
102 * check to see if the lock file exists and is still active
103 * - use kill(pid,0)
105 * return:
106 * 0 -> success (lock file removed - no longer active
107 * FAIL -> lock file still active
109 GLOBAL int
110 cklock(name)
111 register char *name;
113 register int ret;
114 pid_t lpid = -1;
115 char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */
116 int fd;
118 fd = open(name, O_RDONLY);
119 DEBUG(4, "ulockf name %s\n", name);
120 if (fd == -1) {
121 if (errno == ENOENT) /* file does not exist -- OK */
122 return(0);
123 DEBUG(4,"Lock File--can't read (errno %d) --remove it!\n", errno);
124 goto unlk;
126 ret = read(fd, (char *) alpid, SIZEOFPID+1); /* +1 for '\n' */
127 (void) close(fd);
128 if (ret != (SIZEOFPID+1)) {
130 DEBUG(4, "Lock File--bad format--remove it!\n%s", "");
131 goto unlk;
133 lpid = (pid_t) strtol(alpid, (char **) NULL, 10);
134 if ((ret=kill(lpid, 0)) == 0 || errno == EPERM) {
135 DEBUG(4, "Lock File--process still active--not removed\n%s", "");
136 return(FAIL);
138 else { /* process no longer active */
139 DEBUG(4, "kill pid (%ld), ", (long) lpid);
140 DEBUG(4, "returned %d", ret);
141 DEBUG(4, "--ok to remove lock file (%s)\n", name);
143 unlk:
145 if (unlink(name) != 0) {
146 DEBUG(4,"ulockf failed in unlink()\n%s", "");
147 return(FAIL);
149 return(0);
152 #define MAXLOCKS 10 /* maximum number of lock files */
153 static char *Lockfile[MAXLOCKS];
154 GLOBAL int Nlocks = 0;
157 * put name in list of lock files
158 * return:
159 * none
161 static void
162 stlock(name)
163 char *name;
165 register int i;
166 char *p;
168 for (i = 0; i < Nlocks; i++) {
169 if (Lockfile[i] == NULL)
170 break;
172 ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i);
173 if (i >= Nlocks)
174 i = Nlocks++;
175 p = (char*) calloc((unsigned) strlen(name) + 1, sizeof (char));
176 ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0);
177 (void) strcpy(p, name);
178 Lockfile[i] = p;
179 return;
183 * remove the named lock. If named lock is NULL,
184 * then remove all locks currently in list.
185 * return:
186 * none
188 GLOBAL void
189 rmlock(name)
190 register char *name;
192 register int i;
193 #ifdef V8
194 char *cp;
196 cp = rindex(name, '/');
197 if (cp++ != CNULL)
198 if (strlen(cp) > MAXBASENAME)
199 *(cp+MAXBASENAME) = NULLCHAR;
200 #endif /* V8 */
203 for (i = 0; i < Nlocks; i++) {
204 if (Lockfile[i] == NULL)
205 continue;
206 if (name == NULL || EQUALS(name, Lockfile[i])) {
207 (void) unlink(Lockfile[i]);
208 free(Lockfile[i]);
209 Lockfile[i] = NULL;
212 return;
218 * remove a lock file
220 * Parameters:
221 * pre - Path and first part of file name of the lock file to be
222 * removed.
223 * s - The suffix part of the lock file. The name of the lock file
224 * will be derrived by concatenating pre, a period, and s.
226 * return:
227 * none
229 GLOBAL void
230 delock(pre, s)
231 char * pre;
232 char *s;
234 char ln[MAXNAMESIZE];
236 (void) sprintf(ln, "%s.%s", pre, s);
237 BASENAME(ln, '/')[MAXBASENAME] = '\0';
238 rmlock(ln);
239 return;
244 * create lock file
246 * Parameters:
247 * pre - Path and first part of file name of the lock file to be
248 * created.
249 * name - The suffix part of the lock file. The name of the lock file
250 * will be derrived by concatenating pre, a period, and name.
252 * return:
253 * 0 -> success
254 * FAIL -> failure
256 GLOBAL int
257 mlock(pre, name)
258 char * pre;
259 char *name;
261 char lname[MAXNAMESIZE];
264 * if name has a '/' in it, then it's a device name and it's
265 * not in /dev (i.e., it's a remotely-mounted device or it's
266 * in a subdirectory of /dev). in either case, creating our normal
267 * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if
268 * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never
269 * mind. since we're using advisory filelocks on the devices
270 * themselves, it'll be safe.
272 * of course, programs and people who are used to looking at the
273 * lockfiles to find out what's going on are going to be a trifle
274 * misled. we really need to re-consider the lockfile naming structure
275 * to accomodate devices in directories other than /dev ... maybe in
276 * the next release.
278 if ( strchr(name, '/') != NULL )
279 return(0);
280 (void) sprintf(lname, "%s.%s", pre, BASENAME(name, '/'));
281 BASENAME(lname, '/')[MAXBASENAME] = '\0';
282 return(mklock(lname));
286 * makes a lock on behalf of pid.
287 * input:
288 * pid - process id
289 * tempfile - name of a temporary in the same file system
290 * name - lock file name (full path name)
291 * return:
292 * -1 - failed
293 * 0 - lock made successfully
295 static int
296 onelock(pid,tempfile,name)
297 char *pid;
298 char *tempfile, *name;
300 register int fd;
301 char cb[100];
303 fd=creat(tempfile, (mode_t) 0444);
304 if(fd < 0){
305 (void) sprintf(cb, "%s %s %d",tempfile, name, errno);
306 logent("ULOCKC", cb);
307 if((errno == EMFILE) || (errno == ENFILE))
308 (void) unlink(tempfile);
309 return(-1);
311 /* +1 for '\n' */
312 if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) {
313 (void) sprintf(cb, "%s %s %d",tempfile, name, errno);
314 logent("ULOCKW", cb);
315 (void) unlink(tempfile);
316 return (-1);
318 (void) chmod(tempfile, (mode_t) 0444);
319 (void) chown(tempfile, UUCPUID, UUCPGID);
320 (void) close(fd);
321 if(link(tempfile,name)<0){
322 DEBUG(4, "%s: ", strerror(errno));
323 DEBUG(4, "link(%s, ", tempfile);
324 DEBUG(4, "%s)\n", name);
325 if(unlink(tempfile)< 0){
326 (void) sprintf(cb, "ULK err %s %d", tempfile, errno);
327 logent("ULOCKLNK", cb);
329 return(-1);
331 if(unlink(tempfile)<0){
332 (void) sprintf(cb, "%s %d",tempfile,errno);
333 logent("ULOCKF", cb);
335 return(0);
339 * fd_mklock(fd) - lock the device indicated by fd is possible
341 * return -
342 * SUCCESS - this process now has the fd locked
343 * FAIL - this process was not able to lock the fd
346 GLOBAL int
347 fd_mklock(fd)
348 int fd;
350 int tries = 0;
352 if ( fstat(fd, &_st_buf) != 0 )
353 return(FAIL);
355 (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
356 (unsigned long) major(_st_buf.st_dev),
357 (unsigned long) major(_st_buf.st_rdev),
358 (unsigned long) minor(_st_buf.st_rdev));
360 if ( mklock(lockname) == FAIL )
361 return(FAIL);
363 while ( lockf(fd, F_TLOCK, 0L) != 0 ) {
364 DEBUG(7, "fd_mklock: lockf returns %d\n", errno);
365 if ( (++tries >= MAX_LOCKTRY) || (errno != EAGAIN) ) {
366 rmlock(lockname);
367 logent("fd_mklock","lockf failed");
368 return(FAIL);
370 (void)sleep(2);
372 DEBUG(7, "fd_mklock: ok\n%s", "");
373 return(SUCCESS);
377 * fn_cklock(name) - determine if the device indicated by name is locked
379 * return -
380 * SUCCESS - the name is not locked
381 * FAIL - the name is locked by another process
384 GLOBAL int
385 fn_cklock(name)
386 char *name;
388 /* we temporarily use lockname to hold full path name */
389 (void) sprintf(lockname, "%s%s", (*name == '/' ? "" : "/dev/"), name);
391 if ( stat(lockname, &_st_buf) != 0 )
392 return(FAIL);
394 (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
395 (unsigned long) major(_st_buf.st_dev),
396 (unsigned long) major(_st_buf.st_rdev),
397 (unsigned long) minor(_st_buf.st_rdev));
399 return(cklock(lockname));
403 * fd_cklock(fd) - determine if the device indicated by fd is locked
405 * return -
406 * SUCCESS - the fd is not locked
407 * FAIL - the fd is locked by another process
410 GLOBAL int
411 fd_cklock(fd)
412 int fd;
414 if ( fstat(fd, &_st_buf) != 0 )
415 return(FAIL);
417 (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
418 (unsigned long) major(_st_buf.st_dev),
419 (unsigned long) major(_st_buf.st_rdev),
420 (unsigned long) minor(_st_buf.st_rdev));
422 if ( cklock(lockname) == FAIL )
423 return(FAIL);
424 else
425 return( lockf(fd, F_TEST, 0L) );
429 * remove the locks associated with the device file descriptor
431 * return -
432 * SUCCESS - both BNU lock file and advisory locks removed
433 * FAIL -
436 GLOBAL void
437 fd_rmlock(fd)
438 int fd;
440 if ( fstat(fd, &_st_buf) == 0 ) {
441 (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK,
442 (unsigned long) major(_st_buf.st_dev),
443 (unsigned long) major(_st_buf.st_rdev),
444 (unsigned long) minor(_st_buf.st_rdev));
445 rmlock(lockname);
447 (void) lockf(fd, F_ULOCK, 0L);
448 return;