uucplock.c (28040) | uucplock.c (36451) |
---|---|
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 --- 16 unchanged lines hidden (view full) --- 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 * | 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 --- 16 unchanged lines hidden (view full) --- 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 * $Id: uucplock.c,v 1.7 1997/08/05 12:58:02 ache Exp $ | 33 * $Id: uucplock.c,v 1.8 1997/08/10 18:42:39 ache Exp $ |
34 * 35 */ 36 37#ifndef lint 38static const char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40 41#include <sys/types.h> --- 19 unchanged lines hidden (view full) --- 61/* Forward declarations */ 62static int put_pid (int fd, pid_t pid); 63static pid_t get_pid (int fd,int *err); 64 65/* 66 * uucp style locking routines 67 */ 68 | 34 * 35 */ 36 37#ifndef lint 38static const char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93"; 39#endif /* not lint */ 40 41#include <sys/types.h> --- 19 unchanged lines hidden (view full) --- 61/* Forward declarations */ 62static int put_pid (int fd, pid_t pid); 63static pid_t get_pid (int fd,int *err); 64 65/* 66 * uucp style locking routines 67 */ 68 |
69int uu_lock (const char *ttyname) | 69int 70uu_lock(const char *ttyname) |
70{ 71 int fd, tmpfd, i; 72 pid_t pid; 73 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN], 74 lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 75 int err, uuerr; 76 77 pid = getpid(); --- 44 unchanged lines hidden (view full) --- 122ret1: 123 (void)close(tmpfd); 124 (void)unlink(lcktmpname); 125ret0: 126 errno = err; 127 return uuerr; 128} 129 | 71{ 72 int fd, tmpfd, i; 73 pid_t pid; 74 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN], 75 lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 76 int err, uuerr; 77 78 pid = getpid(); --- 44 unchanged lines hidden (view full) --- 123ret1: 124 (void)close(tmpfd); 125 (void)unlink(lcktmpname); 126ret0: 127 errno = err; 128 return uuerr; 129} 130 |
130int uu_unlock (const char *ttyname) | 131int 132uu_lock_txfr(const char *ttyname, pid_t pid) |
131{ | 133{ |
134 int fd, err; 135 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 136 137 snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); 138 139 if ((fd = open(lckname, O_RDWR)) < 0) 140 return UU_LOCK_OWNER_ERR; 141 if (get_pid(fd, &err) != getpid()) 142 return UU_LOCK_OWNER_ERR; 143 lseek(fd, 0, SEEK_SET); 144 if (put_pid(fd, pid)) 145 return UU_LOCK_WRITE_ERR; 146 close(fd); 147 148 return UU_LOCK_OK; 149} 150 151int 152uu_unlock(const char *ttyname) 153{ |
|
132 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 133 134 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); 135 return unlink(tbuf); 136} 137 | 154 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 155 156 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); 157 return unlink(tbuf); 158} 159 |
138const char *uu_lockerr (int uu_lockresult) | 160const char * 161uu_lockerr(int uu_lockresult) |
139{ 140 static char errbuf[128]; 141 char *fmt; 142 143 switch (uu_lockresult) { 144 case UU_LOCK_INUSE: 145 return "device in use"; 146 case UU_LOCK_OK: --- 11 unchanged lines hidden (view full) --- 158 fmt = "write error: %s"; 159 break; 160 case UU_LOCK_LINK_ERR: 161 fmt = "link error: %s"; 162 break; 163 case UU_LOCK_TRY_ERR: 164 fmt = "too many tries: %s"; 165 break; | 162{ 163 static char errbuf[128]; 164 char *fmt; 165 166 switch (uu_lockresult) { 167 case UU_LOCK_INUSE: 168 return "device in use"; 169 case UU_LOCK_OK: --- 11 unchanged lines hidden (view full) --- 181 fmt = "write error: %s"; 182 break; 183 case UU_LOCK_LINK_ERR: 184 fmt = "link error: %s"; 185 break; 186 case UU_LOCK_TRY_ERR: 187 fmt = "too many tries: %s"; 188 break; |
189 case UU_LOCK_OWNER_ERR: 190 fmt = "not locking process: %s"; 191 break; |
|
166 default: 167 fmt = "undefined error: %s"; 168 break; 169 } 170 171 (void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno)); 172 return errbuf; 173} 174 | 192 default: 193 fmt = "undefined error: %s"; 194 break; 195 } 196 197 (void)snprintf(errbuf, sizeof(errbuf), fmt, strerror(errno)); 198 return errbuf; 199} 200 |
175static int put_pid (int fd, pid_t pid) | 201static int 202put_pid(int fd, pid_t pid) |
176{ 177 char buf[32]; 178 int len; 179 180 len = sprintf (buf, "%10d\n", pid); 181 return write (fd, buf, len) == len; 182} 183 | 203{ 204 char buf[32]; 205 int len; 206 207 len = sprintf (buf, "%10d\n", pid); 208 return write (fd, buf, len) == len; 209} 210 |
184static pid_t get_pid (int fd, int *err) | 211static pid_t 212get_pid(int fd, int *err) |
185{ 186 int bytes_read; 187 char buf[32]; 188 pid_t pid; 189 190 bytes_read = read (fd, buf, sizeof (buf) - 1); 191 if (bytes_read > 0) { 192 buf[bytes_read] = '\0'; 193 pid = strtol (buf, (char **) NULL, 10); 194 } else { 195 pid = -1; 196 *err = bytes_read ? errno : EINVAL; 197 } 198 return pid; 199} 200 201/* end of uucplock.c */ | 213{ 214 int bytes_read; 215 char buf[32]; 216 pid_t pid; 217 218 bytes_read = read (fd, buf, sizeof (buf) - 1); 219 if (bytes_read > 0) { 220 buf[bytes_read] = '\0'; 221 pid = strtol (buf, (char **) NULL, 10); 222 } else { 223 pid = -1; 224 *err = bytes_read ? errno : EINVAL; 225 } 226 return pid; 227} 228 229/* end of uucplock.c */ |