All changes still related to original version:

Don't close line twice.
Change typo == to = in assignment.
Don't restart, if ioctl fails.
Implement uucp-style locking to help dialout pgms.
This commit is contained in:
Andrey A. Chernov 1995-09-14 22:01:06 +00:00
parent 7de6adb6fc
commit cb1efdbadd
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=10743
3 changed files with 195 additions and 59 deletions

View File

@ -1,6 +1,6 @@
# @(#)Makefile 8.1 (Berkeley) 6/5/93
PROG= startslip
MAN1= startslip.1
SRCS= startslip.c uucplock.c
.include <bsd.prog.mk>

View File

@ -76,6 +76,7 @@ int flowcontrol = FC_NONE;
char *annex;
int hup;
int terminate;
int locked;
int logged_in = 0;
int wait_time = 60; /* then back off */
int script_timeout = 90; /* connect script default timeout */
@ -86,6 +87,10 @@ int script_timeout = 90; /* connect script default timeout */
char *dials[MAXDIALS];
int diali, dialc;
FILE *pfd;
char *devname;
char pidfile[80];
#ifdef DEBUG
int debug = 1;
#undef LOG_ERR
@ -108,9 +113,8 @@ main(argc, argv)
int ch, disc;
int fd = -1;
void sighup(), sigterm();
FILE *wfd = NULL, *pfd;
FILE *wfd = NULL;
char *dialerstring = 0, buf[BUFSIZ];
char pidfile[32];
int unitnum;
char unitname[32];
char *devicename, *username, *password;
@ -207,11 +211,11 @@ main(argc, argv)
if (debug)
setbuf(stdout, NULL);
if ((cp = strrchr(devicename, '/')) == NULL)
cp = devicename;
if ((devname = strrchr(devicename, '/')) == NULL)
devname = devicename;
else
cp++;
sprintf(pidfile, PIDFILE, cp);
devname++;
sprintf(pidfile, PIDFILE, devname);
if ((pfd = fopen(pidfile, "r")) != NULL) {
pid = 0;
fscanf(pfd, "%d\n", &pid);
@ -226,22 +230,15 @@ main(argc, argv)
sprintf(buf, "%s %s down &", downscript ? downscript : "/sbin/ifconfig" , unitname);
(void) system(buf);
}
if (terminate) {
if (pfd)
unlink(pidfile);
exit(0);
}
if (terminate)
down(0);
logged_in = 0;
if (++tries > MAXTRIES) {
syslog(LOG_ERR, "exiting login after %d tries\n", tries);
/* ???
if (first)
*/
{
if (pfd)
unlink(pidfile);
exit(1);
}
down(3);
}
if (diali > 0)
dialerstring = dials[dialc++ % diali];
@ -273,21 +270,35 @@ main(argc, argv)
if (wfd) {
printd("fclose, ");
fclose(wfd);
wfd == NULL;
uu_unlock(devname);
locked = 0;
wfd = NULL;
fd = -1;
sleep(5);
}
if (fd >= 0) {
printd("close, ");
close(fd);
uu_unlock(devname);
locked = 0;
fd = -1;
sleep(5);
}
printd("open");
if (uu_lock(devname)) {
syslog(LOG_ERR, "can't lock %s", devicename);
syslog(LOG_INFO, "sleeping %d seconds (%d tries).\n", wait_time * tries, tries);
sleep(wait_time * tries);
goto restart;
}
locked = 1;
if ((fd = open(devicename, O_RDWR)) < 0) {
syslog(LOG_ERR, "open %s: %m\n", devicename);
if (first) {
if (pfd)
unlink(pidfile);
exit(1);
} else {
if (first)
down(1);
else {
uu_unlock(devname);
locked = 0;
syslog(LOG_INFO, "sleeping %d seconds (%d tries).\n", wait_time * tries, tries);
sleep(wait_time * tries);
goto restart;
@ -295,28 +306,30 @@ main(argc, argv)
}
printd(" %d", fd);
#ifdef TIOCSCTTY
if (ioctl(fd, TIOCSCTTY, 0) < 0)
if (ioctl(fd, TIOCSCTTY, 0) < 0) {
syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
down(2);
}
#endif
signal(SIGHUP, sighup);
signal(SIGTERM, sigterm);
if (debug) {
if (ioctl(fd, TIOCGETD, &disc) < 0)
syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
printd(" (disc was %d)", disc);
else
printd(" (disc was %d)", disc);
}
disc = TTYDISC;
if (ioctl(fd, TIOCSETD, &disc) < 0) {
syslog(LOG_ERR, "%s: ioctl (TIOCSETD 0): %m\n",
devicename);
down(2);
}
printd(", ioctl");
#ifdef POSIX
if (tcgetattr(fd, &t) < 0) {
syslog(LOG_ERR, "%s: tcgetattr: %m\n", devicename);
if (pfd)
unlink(pidfile);
exit(2);
down(2);
}
cfmakeraw(&t);
switch (flowcontrol) {
@ -330,23 +343,13 @@ main(argc, argv)
cfsetspeed(&t, speed);
if (tcsetattr(fd, TCSAFLUSH, &t) < 0) {
syslog(LOG_ERR, "%s: tcsetattr: %m\n", devicename);
if (first) {
if (pfd)
unlink(pidfile);
exit(2);
} else {
syslog(LOG_INFO, "sleeping %d seconds (%d tries).\n", wait_time * tries, tries);
sleep(wait_time * tries);
goto restart;
}
down(2);
}
#else
if (ioctl(fd, TIOCGETP, &sgtty) < 0) {
syslog(LOG_ERR, "%s: ioctl (TIOCGETP): %m\n",
devicename);
if (pfd)
unlink(pidfile);
exit(2);
down(2);
}
sgtty.sg_flags = RAW | ANYP;
sgtty.sg_erase = sgtty.sg_kill = 0377;
@ -354,15 +357,7 @@ main(argc, argv)
if (ioctl(fd, TIOCSETP, &sgtty) < 0) {
syslog(LOG_ERR, "%s: ioctl (TIOCSETP): %m\n",
devicename);
if (first) {
if (pfd)
unlink(pidfile);
exit(2);
} else {
syslog(LOG_INFO, "sleeping %d seconds (%d tries).\n", wait_time * tries, tries);
sleep(wait_time * tries);
goto restart;
}
down(2);
}
#endif
sleep(2); /* wait for flakey line to settle */
@ -371,10 +366,8 @@ main(argc, argv)
wfd = fdopen(fd, "w+");
if (wfd == NULL) {
syslog(LOG_ERR, "can't fdopen slip line\n");
if (pfd)
unlink(pidfile);
exit(10);
syslog(LOG_ERR, "can't fdopen %s: %m", devicename);
down(2);
}
setbuf(wfd, (char *)0);
if (dialerstring) {
@ -436,15 +429,11 @@ main(argc, argv)
if (ioctl(fd, TIOCSETD, &disc) < 0) {
syslog(LOG_ERR, "%s: ioctl (TIOCSETD SLIP): %m\n",
devicename);
if (pfd)
unlink(pidfile);
exit(1);
down(2);
}
if (ioctl(fd, SLIOCGUNIT, (caddr_t)&unitnum) < 0) {
syslog(LOG_ERR, "ioctl(SLIOCGUNIT): %m");
if (pfd)
unlink(pidfile);
exit(1);
down(2);
}
sprintf(unitname, "sl%d", unitnum);
if (first && debug == 0) {
@ -543,6 +532,15 @@ getline(buf, size, fd, timeout)
return (0);
}
down(code)
{
if (pfd)
unlink(pidfile);
if (locked)
uu_unlock(devname);
exit(code);
}
usage()
{
(void)fprintf(stderr, "\

138
sbin/startslip/uucplock.c Normal file
View File

@ -0,0 +1,138 @@
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#include <sys/types.h>
#include <sys/file.h>
#include <sys/dir.h>
#include <errno.h>
#include <syslog.h>
#define _PATH_LOCKDIRNAME "/var/spool/lock/LCK..%s"
/* Forward declarations */
static int put_pid (int fd, int pid);
static int get_pid (int fd);
/*
* uucp style locking routines
* return: 0 - success
* -1 - failure
*/
uu_lock (char *ttyname)
{
int fd, pid;
char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
off_t lseek();
(void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
if (fd < 0) {
/*
* file is already locked
* check to see if the process holding the lock still exists
*/
fd = open(tbuf, O_RDWR, 0);
if (fd < 0) {
syslog(LOG_ERR, "lock open: %m");
return(-1);
}
if (get_pid (fd) == -1) {
(void)close(fd);
syslog(LOG_ERR, "lock read: %m");
return(-1);
}
if (kill(pid, 0) == 0 || errno != ESRCH) {
(void)close(fd); /* process is still running */
return(-1);
}
/*
* The process that locked the file isn't running, so
* we'll lock it ourselves
*/
if (lseek(fd, 0L, L_SET) < 0) {
(void)close(fd);
syslog(LOG_ERR, "lock lseek: %m");
return(-1);
}
/* fall out and finish the locking process */
}
pid = getpid();
if (!put_pid (fd, pid)) {
(void)close(fd);
(void)unlink(tbuf);
syslog(LOG_ERR, "lock write: %m");
return(-1);
}
(void)close(fd);
return(0);
}
uu_unlock (char *ttyname)
{
char tbuf[sizeof(_PATH_LOCKDIRNAME) + MAXNAMLEN];
(void)sprintf(tbuf, _PATH_LOCKDIRNAME, ttyname);
return(unlink(tbuf));
}
static int put_pid (int fd, int pid)
{
char buf [32];
int len;
len = sprintf (buf, "%10ld\n", (long) pid);
return write (fd, buf, len) == len;
}
static int get_pid (int fd)
{
int bytes_read, pid;
char buf [32];
bytes_read = read (fd, buf, sizeof (buf) - 1);
if (bytes_read > 0) {
buf [bytes_read] = '\0';
pid = strtol (buf, (char **) NULL, 10);
}
else
pid = -1;
return pid;
}
/* end of uucplock.c */