uulock -- HDB style tty locking [mi.misc]
Jon Zeeff
zeeff at b-tech.ann-arbor.mi.us
Tue Dec 12 08:50:30 AEST 1989
Archive-name: uulock
Original-posting-by: zeeff at b-tech.ann-arbor.mi.us (Jon Zeeff)
Original-subject: KA9Q questions
Reposted-by: emv at math.lsa.umich.edu (Edward Vielmetti)
[This is an experimental alt.sources re-posting from the newsgroup(s)
mi.misc. Comments on this service to emv at math.lsa.umich.edu
(Edward Vielmetti).]
>(Anyone tried to implement locking so uucp doesn't get upset?)
Here is something I wrote for HDB uucp. Could probably be easily
modified for others.
/*
Lock a tty and then run a program. Blow away old locks.
Written for Sys V with HDB style locks.
Written by Jon Zeeff (zeeff at b-tech.ann-arbor.mi.us)
This program placed in the public domain.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <termio.h>
#include <sys/errno.h>
char *strcpy();
char *strcat();
unsigned sleep();
unsigned alarm();
void exit();
int pid;
FILE *lock;
char tty_lock[100];
main(argc,argv)
int argc;
char **argv;
{
if (argc < 3) {
fprintf(stderr,"Usage: uulock tty command args\n");
exit(1);
}
/* create a hdb style lock file */
strcpy(tty_lock,"/usr/spool/locks/LCK..");
strcat(tty_lock,argv[1]);
check_lock(tty_lock);
umask(022);
lock = fopen(tty_lock,"w");
if (lock) {
fprintf(lock,"%10.10d\n",(int)getpid());
fclose(lock);
if (pid = fork()) {
int w;
while ((w = wait((int *)0)) != pid && w != -1);
unlink(tty_lock);
} else {
execvp(argv[2],&argv[2]);
printf("Can't exec that program\n");
exit(2);
}
} else {
fprintf(stderr,"tty port in use\n");
return 2;
}
return 0;
}
/* remove a lock file if the process is no longer there */
check_lock(file_name)
char *file_name;
{
int fd;
char lock_pid[11];
extern int errno;
if ((fd = open(file_name, O_RDONLY)) == -1) {
unlink(file_name);
return 0;
}
read(fd,lock_pid, 10);
close(fd);
if ((kill(atoi(lock_pid), 0)) == 0 || errno == EPERM)
return 1; /* process is active */
unlink(file_name);
return 0;
}
--
Jon Zeeff zeeff at b-tech.ann-arbor.mi.us or b-tech!zeeff
More information about the Alt.sources
mailing list