uucplock.c (116344) | uucplock.c (121193) |
---|---|
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 --- 18 unchanged lines hidden (view full) --- 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 34#include <sys/cdefs.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 --- 18 unchanged lines hidden (view full) --- 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 34#include <sys/cdefs.h> |
35__FBSDID("$FreeBSD: head/lib/libutil/uucplock.c 116344 2003-06-14 18:42:37Z markm $"); | 35__FBSDID("$FreeBSD: head/lib/libutil/uucplock.c 121193 2003-10-18 10:04:16Z markm $"); |
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> 42#include <sys/file.h> 43#include <dirent.h> --- 18 unchanged lines hidden (view full) --- 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 | 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> 42#include <sys/file.h> 43#include <dirent.h> --- 18 unchanged lines hidden (view full) --- 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 |
70uu_lock(const char *ttyname) | 70uu_lock(const char *tty_name) |
71{ 72 int fd, tmpfd, i; 73 pid_t pid, pid_old; 74 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN], 75 lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 76 int err, uuerr; 77 78 pid = getpid(); 79 (void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP, 80 pid); 81 (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, | 71{ 72 int fd, tmpfd, i; 73 pid_t pid, pid_old; 74 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN], 75 lcktmpname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 76 int err, uuerr; 77 78 pid = getpid(); 79 (void)snprintf(lcktmpname, sizeof(lcktmpname), _PATH_UUCPLOCK LOCKTMP, 80 pid); 81 (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, |
82 ttyname); | 82 tty_name); |
83 if ((tmpfd = creat(lcktmpname, 0664)) < 0) 84 GORET(0, UU_LOCK_CREAT_ERR); 85 86 for (i = 0; i < MAXTRIES; i++) { 87 if (link (lcktmpname, lckname) < 0) { 88 if (errno != EEXIST) 89 GORET(1, UU_LOCK_LINK_ERR); 90 /* --- 33 unchanged lines hidden (view full) --- 124 (void)close(tmpfd); 125 (void)unlink(lcktmpname); 126ret0: 127 errno = err; 128 return uuerr; 129} 130 131int | 83 if ((tmpfd = creat(lcktmpname, 0664)) < 0) 84 GORET(0, UU_LOCK_CREAT_ERR); 85 86 for (i = 0; i < MAXTRIES; i++) { 87 if (link (lcktmpname, lckname) < 0) { 88 if (errno != EEXIST) 89 GORET(1, UU_LOCK_LINK_ERR); 90 /* --- 33 unchanged lines hidden (view full) --- 124 (void)close(tmpfd); 125 (void)unlink(lcktmpname); 126ret0: 127 errno = err; 128 return uuerr; 129} 130 131int |
132uu_lock_txfr(const char *ttyname, pid_t pid) | 132uu_lock_txfr(const char *tty_name, pid_t pid) |
133{ 134 int fd, err; 135 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 136 | 133{ 134 int fd, err; 135 char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 136 |
137 snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); | 137 snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name); |
138 139 if ((fd = open(lckname, O_RDWR)) < 0) 140 return UU_LOCK_OWNER_ERR; 141 if (get_pid(fd, &err) != getpid()) 142 err = UU_LOCK_OWNER_ERR; 143 else { | 138 139 if ((fd = open(lckname, O_RDWR)) < 0) 140 return UU_LOCK_OWNER_ERR; 141 if (get_pid(fd, &err) != getpid()) 142 err = UU_LOCK_OWNER_ERR; 143 else { |
144 lseek(fd, 0, SEEK_SET); | 144 lseek(fd, (off_t)0, SEEK_SET); |
145 err = put_pid(fd, pid) ? 0 : UU_LOCK_WRITE_ERR; 146 } 147 close(fd); 148 149 return err; 150} 151 152int | 145 err = put_pid(fd, pid) ? 0 : UU_LOCK_WRITE_ERR; 146 } 147 close(fd); 148 149 return err; 150} 151 152int |
153uu_unlock(const char *ttyname) | 153uu_unlock(const char *tty_name) |
154{ 155 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 156 | 154{ 155 char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; 156 |
157 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); | 157 (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, tty_name); |
158 return unlink(tbuf); 159} 160 161const char * 162uu_lockerr(int uu_lockresult) 163{ 164 static char errbuf[128]; | 158 return unlink(tbuf); 159} 160 161const char * 162uu_lockerr(int uu_lockresult) 163{ 164 static char errbuf[128]; |
165 char *fmt; | 165 const char *fmt; |
166 167 switch (uu_lockresult) { 168 case UU_LOCK_INUSE: 169 return "device in use"; 170 case UU_LOCK_OK: 171 return ""; 172 case UU_LOCK_OPEN_ERR: 173 fmt = "open error: %s"; --- 27 unchanged lines hidden (view full) --- 201 202static int 203put_pid(int fd, pid_t pid) 204{ 205 char buf[32]; 206 int len; 207 208 len = sprintf (buf, "%10d\n", (int)pid); | 166 167 switch (uu_lockresult) { 168 case UU_LOCK_INUSE: 169 return "device in use"; 170 case UU_LOCK_OK: 171 return ""; 172 case UU_LOCK_OPEN_ERR: 173 fmt = "open error: %s"; --- 27 unchanged lines hidden (view full) --- 201 202static int 203put_pid(int fd, pid_t pid) 204{ 205 char buf[32]; 206 int len; 207 208 len = sprintf (buf, "%10d\n", (int)pid); |
209 return write (fd, buf, len) == len; | 209 return write (fd, buf, (size_t)len) == len; |
210} 211 212static pid_t 213get_pid(int fd, int *err) 214{ 215 int bytes_read; 216 char buf[32]; 217 pid_t pid; 218 219 bytes_read = read (fd, buf, sizeof (buf) - 1); 220 if (bytes_read > 0) { 221 buf[bytes_read] = '\0'; | 210} 211 212static pid_t 213get_pid(int fd, int *err) 214{ 215 int bytes_read; 216 char buf[32]; 217 pid_t pid; 218 219 bytes_read = read (fd, buf, sizeof (buf) - 1); 220 if (bytes_read > 0) { 221 buf[bytes_read] = '\0'; |
222 pid = strtol (buf, (char **) NULL, 10); | 222 pid = (pid_t)strtol (buf, (char **) NULL, 10); |
223 } else { 224 pid = -1; 225 *err = bytes_read ? errno : EINVAL; 226 } 227 return pid; 228} 229 230/* end of uucplock.c */ | 223 } else { 224 pid = -1; 225 *err = bytes_read ? errno : EINVAL; 226 } 227 return pid; 228} 229 230/* end of uucplock.c */ |