summaryrefslogtreecommitdiffstats
path: root/sbin/slattach/slattach.c
diff options
context:
space:
mode:
authorjoerg <joerg@FreeBSD.org>1995-03-12 15:04:18 +0000
committerjoerg <joerg@FreeBSD.org>1995-03-12 15:04:18 +0000
commitc019fd724050c5fc25f122d1877ff762b9cef597 (patch)
treefcdea8e5823515e77c4dd86f7cfc35fc661c9faa /sbin/slattach/slattach.c
parentdb2028940b5b3b6722f6cce89408024bd71a039a (diff)
downloadFreeBSD-src-c019fd724050c5fc25f122d1877ff762b9cef597.zip
FreeBSD-src-c019fd724050c5fc25f122d1877ff762b9cef597.tar.gz
Make slattach create a PID file under /var/run when the connection is
established. This way, automatic scripts are possible that might control the SLIP connection. It's unacceptable for a daemon that's being controlled by a variety of signals to not leave its PID somewhere. The file name contains the terminal path name component of the associated tty device, so it should be unique even with multiple parallel slattach's running. The file will be unlinked at regular exit. Also found a minor bug in the option handling by compiling with -Wall.
Diffstat (limited to 'sbin/slattach/slattach.c')
-rw-r--r--sbin/slattach/slattach.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/sbin/slattach/slattach.c b/sbin/slattach/slattach.c
index a773493..f89bc7d 100644
--- a/sbin/slattach/slattach.c
+++ b/sbin/slattach/slattach.c
@@ -89,6 +89,7 @@ int exiting = 0; /* allready running exit_handler */
struct termios tty; /* tty configuration/state */
char tty_path[32]; /* path name of the tty (e.g. /dev/tty01) */
+char pidfilename[40]; /* e.g. /var/run/slattach.tty01.pid */
char *redial_cmd = 0; /* command to exec upon shutdown. */
char *config_cmd = 0; /* command to exec if slip unit changes. */
char *exit_cmd = 0; /* command to exec before exiting. */
@@ -112,6 +113,7 @@ int main(int argc, char **argv)
int option;
extern char *optarg;
extern int optind;
+ char *cp;
while ((option = getopt(argc, argv, "ace:fhlnr:s:u:z")) != EOF) {
switch (option) {
@@ -152,7 +154,7 @@ int main(int argc, char **argv)
break;
default:
fprintf(stderr, "%s: Invalid option -- '%c'\n",
- option);
+ argv[0], option);
case '?':
fprintf(stderr, usage_str, argv[0]);
exit_handler(1);
@@ -179,7 +181,11 @@ int main(int argc, char **argv)
strncat(tty_path, dev, 10);
dev = tty_path;
}
-
+ cp = strrchr(dev, '/'); /* always succeeds */
+ cp++; /* trailing tty pathname component */
+ sprintf(pidfilename, "%sslattach.%s.pid", _PATH_VARRUN, cp);
+ printf("%s\n",pidfilename);
+
if (!foreground)
daemon(0,0); /* fork, setsid, chdir /, and close std*. */
/* daemon() closed stderr, so log errors from here on. */
@@ -225,7 +231,7 @@ int main(int argc, char **argv)
void acquire_line()
{
int ttydisc = TTYDISC;
- int pgrp;
+ FILE *pidfile;
ioctl(fd, TIOCSETD, &ttydisc); /* reset to tty discipline */
@@ -241,6 +247,14 @@ void acquire_line()
while (getppid () != 1)
sleep (1); /* Wait for parent to die. */
+ /* create PID file */
+ if((pidfile = fopen(pidfilename, "w")) == NULL) {
+ syslog(LOG_NOTICE,"cannot create PID file: %m");
+ } else {
+ fprintf(pidfile, "%ld\n", getpid());
+ fclose(pidfile);
+ }
+
if ((int)signal(SIGHUP,sighup_handler) < 0) /* Re-enable HUP signal */
syslog(LOG_NOTICE,"cannot install SIGHUP handler: %m");
@@ -438,6 +452,10 @@ void exit_handler(int ret)
*/
if (fd != -1)
close(fd);
+
+ /* Remove the PID file */
+ (void)unlink(pidfilename);
+
/* invoke a shell for exit_cmd. */
if (exit_cmd) {
syslog(LOG_NOTICE,"exiting after running %s", exit_cmd);
OpenPOWER on IntegriCloud