uucplock.c (568b59b9df9cbf2d11dda28cefc9fc105d6ff8c6) uucplock.c (687d0cdeb381b9003bb907fac08f035be93d1d7f)
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

--- 33 unchanged lines hidden (view full) ---

42#ifndef USE_PERROR
43#include <syslog.h>
44#endif
45#include <unistd.h>
46#include <signal.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <paths.h>
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

--- 33 unchanged lines hidden (view full) ---

42#ifndef USE_PERROR
43#include <syslog.h>
44#endif
45#include <unistd.h>
46#include <signal.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <paths.h>
50#include <string.h>
51#include "libutil.h"
50
51#define LOCKFMT "LCK..%s"
52
53/* Forward declarations */
54static int put_pid (int fd, pid_t pid);
52
53#define LOCKFMT "LCK..%s"
54
55/* Forward declarations */
56static int put_pid (int fd, pid_t pid);
55static pid_t get_pid (int fd);
57static pid_t get_pid (int fd,int *err);
56
57/*
58 * uucp style locking routines
59 * return: 0 - success
60 * -1 - failure
61 */
62
63int uu_lock (char *ttyname)
64{
65 int fd;
66 pid_t pid;
67 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
58
59/*
60 * uucp style locking routines
61 * return: 0 - success
62 * -1 - failure
63 */
64
65int uu_lock (char *ttyname)
66{
67 int fd;
68 pid_t pid;
69 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
70 int err;
68
69 (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
70 fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
71 if (fd < 0) {
72 /*
73 * file is already locked
74 * check to see if the process holding the lock still exists
75 */
76 fd = open(tbuf, O_RDWR, 0);
71
72 (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
73 fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
74 if (fd < 0) {
75 /*
76 * file is already locked
77 * check to see if the process holding the lock still exists
78 */
79 fd = open(tbuf, O_RDWR, 0);
77 if (fd < 0) {
78#ifndef USE_PERROR
79 syslog(LOG_ERR, "lock open: %m");
80#else
81 perror("lock open");
82#endif
83 return(-1);
84 }
85 if ((pid = get_pid (fd)) == -1) {
86#ifndef USE_PERROR
87 syslog(LOG_ERR, "lock read: %m");
88#else
89 perror("lock read");
90#endif
80 if (fd < 0)
81 return UU_LOCK_OPEN_ERR;
82
83 if ((pid = get_pid (fd, &err)) == -1) {
91 (void)close(fd);
84 (void)close(fd);
92 return(-1);
85 errno = err;
86 return UU_LOCK_READ_ERR;
93 }
94
95 if (kill(pid, 0) == 0 || errno != ESRCH) {
96 (void)close(fd); /* process is still running */
87 }
88
89 if (kill(pid, 0) == 0 || errno != ESRCH) {
90 (void)close(fd); /* process is still running */
97 return(-1);
91 return UU_LOCK_INUSE;
98 }
99 /*
100 * The process that locked the file isn't running, so
101 * we'll lock it ourselves
102 */
103 if (lseek(fd, (off_t) 0, L_SET) < 0) {
92 }
93 /*
94 * The process that locked the file isn't running, so
95 * we'll lock it ourselves
96 */
97 if (lseek(fd, (off_t) 0, L_SET) < 0) {
104#ifndef USE_PERROR
105 syslog(LOG_ERR, "lock lseek: %m");
106#else
107 perror("lock lseek");
108#endif
98 err = errno;
109 (void)close(fd);
99 (void)close(fd);
110 return(-1);
100 errno = err;
101 return UU_LOCK_SEEK_ERR;
111 }
112 /* fall out and finish the locking process */
113 }
114 pid = getpid();
115 if (!put_pid (fd, pid)) {
102 }
103 /* fall out and finish the locking process */
104 }
105 pid = getpid();
106 if (!put_pid (fd, pid)) {
116#ifndef USE_PERROR
117 syslog(LOG_ERR, "lock write: %m");
118#else
119 perror("lock write");
120#endif
107 err = errno;
121 (void)close(fd);
122 (void)unlink(tbuf);
108 (void)close(fd);
109 (void)unlink(tbuf);
123 return(-1);
110 errno = err;
111 return UU_LOCK_WRITE_ERR;
124 }
125 (void)close(fd);
112 }
113 (void)close(fd);
126 return(0);
114 return UU_LOCK_OK;
127}
128
129int uu_unlock (char *ttyname)
130{
131 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
132
133 (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
115}
116
117int uu_unlock (char *ttyname)
118{
119 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
120
121 (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname);
134 return(unlink(tbuf));
122 return unlink(tbuf);
135}
136
123}
124
125char *uu_lockerr (int uu_lockresult)
126{
127 static char errbuf[512];
128 int len;
129
130 switch (uu_lockresult) {
131 case UU_LOCK_INUSE:
132 return "";
133 case UU_LOCK_OK:
134 return 0;
135 case UU_LOCK_OPEN_ERR:
136 strcpy(errbuf,"open error: ");
137 len = 12;
138 break;
139 case UU_LOCK_READ_ERR:
140 strcpy(errbuf,"read error: ");
141 len = 12;
142 break;
143 case UU_LOCK_SEEK_ERR:
144 strcpy(errbuf,"seek error: ");
145 len = 12;
146 break;
147 case UU_LOCK_WRITE_ERR:
148 strcpy(errbuf,"write error: ");
149 len = 13;
150 break;
151 default:
152 strcpy(errbuf,"Undefined error: ");
153 len = 17;
154 break;
155 }
156
157 strncpy(errbuf+len,strerror(errno),sizeof(errbuf)-len-1);
158 return errbuf;
159}
160
137static int put_pid (int fd, pid_t pid)
138{
139 char buf[32];
140 int len;
141
142 len = sprintf (buf, "%10d\n", pid);
143 return write (fd, buf, len) == len;
144}
145
161static int put_pid (int fd, pid_t pid)
162{
163 char buf[32];
164 int len;
165
166 len = sprintf (buf, "%10d\n", pid);
167 return write (fd, buf, len) == len;
168}
169
146static pid_t get_pid (int fd)
170static pid_t get_pid (int fd,int *err)
147{
148 int bytes_read;
149 char buf[32];
150 pid_t pid;
151
152 bytes_read = read (fd, buf, sizeof (buf) - 1);
153 if (bytes_read > 0) {
154 buf[bytes_read] = '\0';
155 pid = strtol (buf, (char **) NULL, 10);
171{
172 int bytes_read;
173 char buf[32];
174 pid_t pid;
175
176 bytes_read = read (fd, buf, sizeof (buf) - 1);
177 if (bytes_read > 0) {
178 buf[bytes_read] = '\0';
179 pid = strtol (buf, (char **) NULL, 10);
156 } else
180 } else {
157 pid = -1;
181 pid = -1;
182 *err = bytes_read ? errno : EINVAL;
183 }
158 return pid;
159}
160
161/* end of uucplock.c */
184 return pid;
185}
186
187/* end of uucplock.c */