summaryrefslogtreecommitdiff
path: root/usr.sbin/altq/altqd
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2001-08-08 07:07:28 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2001-08-08 07:07:28 +0000
commitb563b1409c3005f908f0f3c52e84779c1e2041ab (patch)
treeb8990b178772a8f50c843c7a26c1aae29d16c390 /usr.sbin/altq/altqd
parent7e28826f68cb7337ace4ef9449150b82057afc41 (diff)
rewrite signal handlers to be race-safe. rewrite USC/ISI code I think
sufficiently so that there is nothing in there that doesn't look like any generic select loop from stevens. The FD_SET code remains overflowable... next thing to fix..
Diffstat (limited to 'usr.sbin/altq/altqd')
-rw-r--r--usr.sbin/altq/altqd/altqd.c209
1 files changed, 110 insertions, 99 deletions
diff --git a/usr.sbin/altq/altqd/altqd.c b/usr.sbin/altq/altqd/altqd.c
index 2510b064628..0e093528b55 100644
--- a/usr.sbin/altq/altqd/altqd.c
+++ b/usr.sbin/altq/altqd/altqd.c
@@ -1,6 +1,29 @@
-/* $OpenBSD: altqd.c,v 1.1 2001/06/27 18:23:17 kjc Exp $ */
+/* $OpenBSD: altqd.c,v 1.2 2001/08/08 07:07:27 deraadt Exp $ */
/* $KAME: altqd.c,v 1.2 2000/10/18 09:15:15 kjc Exp $ */
/*
+ * Copyright (c) 2001 Theo de Raadt
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ *
* Copyright (C) 1997-2000
* Sony Computer Science Laboratories, Inc. All rights reserved.
*
@@ -25,32 +48,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-/*******************************************************************
-
- Copyright (c) 1996 by the University of Southern California
- All rights reserved.
-
- Permission to use, copy, modify, and distribute this software and its
- documentation in source and binary forms for any purpose and without
- fee is hereby granted, provided that both the above copyright notice
- and this permission notice appear in all copies. and that any
- documentation, advertising materials, and other materials related to
- such distribution and use acknowledge that the software was developed
- in part by the University of Southern California, Information
- Sciences Institute. The name of the University may not be used to
- endorse or promote products derived from this software without
- specific prior written permission.
-
- THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
- the suitability of this software for any purpose. THIS SOFTWARE IS
- PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
- INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
-
- Other copyrights might apply to parts of this software and are so
- noted when applicable.
-
-********************************************************************/
#include <stdio.h>
#include <stdlib.h>
@@ -76,66 +73,57 @@
#include "altq_qop.h"
#include "quip_server.h"
-#define ALTQD_PID_FILE "/var/run/altqd.pid"
-
-static int altqd_socket = -1;
+#define ALTQD_PID_FILE "/var/run/altqd.pid"
#define MAX_CLIENT 10
-static FILE *client[MAX_CLIENT];
-/* for command mode */
-int T; /* Current Thread number */
-FILE *infp; /* Input file pointer */
-char *infile = NULL; /* command input file. stdin if NULL. */
-fd_set fds, t_fds;
+int altqd_socket = -1;
+FILE * client[MAX_CLIENT];
+int T; /* Current Thread number */
+FILE *infp; /* Input file pointer */
+char *infile = NULL; /* command input file. stdin if NULL. */
+fd_set fds, t_fds;
-#define DEFAULT_DEBUG_MASK 0
+#define DEFAULT_DEBUG_MASK 0
#define DEFAULT_LOGGING_LEVEL LOG_INFO
-static void usage(void)
+void
+usage(void)
{
- fprintf(stderr, "usage: altqd [options]\n");
- fprintf(stderr, " options:\n");
- fprintf(stderr, " -f config_file : set config file\n");
- fprintf(stderr, " -v : verbose (no daemonize)\n");
- fprintf(stderr, " -d : debug (no daemonize)\n");
+ fprintf(stderr, "usage: altqd [-vd] [-f config]\n");
+ exit(1);
}
-static void
-sig_handler(int sig)
+void
+sig_pipe(int sig)
{
- if (sig == SIGPIPE) {
- /*
- * we have lost an API connection.
- * a subsequent output operation will catch EPIPE.
- */
- return;
- }
-
- qcmd_destroyall();
-
- if (sig == SIGHUP) {
- printf("reinitializing altqd...\n");
- qcmd_init();
- return;
- }
+ /*
+ * we have lost an API connection.
+ * a subsequent output operation will catch EPIPE.
+ */
+}
- fprintf(stderr, "Exiting on signal %d\n", sig);
+int gotsig_hup, gotsig_int, gotsig_term;
- /* if we have a pid file, remove it */
- if (daemonize) {
- unlink(ALTQD_PID_FILE);
- closelog();
- }
+void
+sig_hup()
+{
+ gotsig_hup = 1;
+}
- if (altqd_socket >= 0) {
- close(altqd_socket);
- altqd_socket = -1;
- }
+void
+sig_int()
+{
+ gotsig_int = 1;
+}
- exit(0);
+void
+sig_term()
+{
+ gotsig_term = 1;
}
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
{
int c;
int i, maxfd;
@@ -149,35 +137,30 @@ int main(int argc, char **argv)
case 'f':
altqconfigfile = optarg;
break;
-
case 'D': /* -D => dummy mode */
Debug_mode = 1;
printf("Debug mode set.\n");
break;
-
case 'v':
l_debug = LOG_DEBUG;
m_debug |= DEBUG_ALTQ;
daemonize = 0;
break;
-
case 'd':
daemonize = 0;
break;
-
case 'l':
l_debug = atoi(optarg);
break;
-
default:
usage();
}
}
- signal(SIGINT, sig_handler);
- signal(SIGTERM, sig_handler);
- signal(SIGHUP, sig_handler);
- signal(SIGPIPE, sig_handler);
+ signal(SIGINT, sig_int);
+ signal(SIGTERM, sig_term);
+ signal(SIGHUP, sig_hup);
+ signal(SIGPIPE, sig_pipe);
if (daemonize)
openlog("altqd", LOG_PID, LOG_DAEMON);
@@ -191,27 +174,25 @@ int main(int argc, char **argv)
/*
* open a unix domain socket for altqd clients
*/
- for (i = 0; i < MAX_CLIENT; i++)
- client[i] = NULL;
if ((altqd_socket = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
LOG(LOG_ERR, errno, "can't open unix domain socket\n");
else {
struct sockaddr_un addr;
- unlink(QUIP_PATH);
bzero(&addr, sizeof(addr));
addr.sun_family = AF_LOCAL;
- strcpy(addr.sun_path, QUIP_PATH);
+ strlcpy(addr.sun_path, QUIP_PATH, sizeof(addr.sun_path));
+ unlink(QUIP_PATH);
if (bind(altqd_socket, (struct sockaddr *)&addr,
- sizeof(addr)) < 0) {
- LOG(LOG_ERR, errno, "can't bind to %s\n",
- QUIP_PATH);
+ sizeof(addr)) < 0) {
+ LOG(LOG_ERR, errno, "can't bind to %s\n", QUIP_PATH);
+ close(altqd_socket);
altqd_socket = -1;
}
chmod(QUIP_PATH, 0666);
if (listen(altqd_socket, SOMAXCONN) < 0) {
- LOG(LOG_ERR, errno, "can't listen to %s\n",
- QUIP_PATH);
+ LOG(LOG_ERR, errno, "can't listen to %s\n", QUIP_PATH);
+ close(altqd_socket);
altqd_socket = -1;
}
}
@@ -225,14 +206,13 @@ int main(int argc, char **argv)
if ((fp = fopen(ALTQD_PID_FILE, "w")) != NULL) {
fprintf(fp, "%d\n", getpid());
fclose(fp);
- }
- else
+ } else
warn("can't open pid file: %s: %s",
- ALTQD_PID_FILE, strerror(errno));
+ ALTQD_PID_FILE, strerror(errno));
} else {
/* interactive mode */
if (infile) {
- if (NULL == (infp = fopen(infile, "r"))) {
+ if ((infp = fopen(infile, "r")) == NULL) {
perror("Cannot open input file");
exit(1);
}
@@ -261,21 +241,51 @@ int main(int argc, char **argv)
while (1) {
int rc;
+ if (gotsig_hup) {
+ qcmd_destroyall();
+ gotsig_hup = 0;
+ printf("reinitializing altqd...\n");
+ qcmd_init();
+ }
+ if (gotsig_term) {
+ fprintf(stderr, "Exiting on signal %d\n", SIGTERM);
+
+ qcmd_destroyall();
+
+ /* if we have a pid file, remove it */
+ if (daemonize) {
+ unlink(ALTQD_PID_FILE);
+ closelog();
+ }
+ exit(0);
+ }
+ if (gotsig_int) {
+ fprintf(stderr, "Exiting on signal %d\n", SIGINT);
+ qcmd_destroyall();
+
+ /* if we have a pid file, remove it */
+ if (daemonize) {
+ unlink(ALTQD_PID_FILE);
+ closelog();
+ }
+ exit(0);
+ }
+
FD_COPY(&fds, &t_fds);
rc = select(maxfd, &t_fds, NULL, NULL, NULL);
if (rc < 0) {
- if (errno != EINTR) {
+ if (errno != EINTR) {
perror("select");
exit(1);
}
continue;
}
-
+
/*
* If there is control input, read the input line,
* parse it, and execute.
*/
- if (infp != NULL && FD_ISSET(fileno(infp), &t_fds)) {
+ if (infp && FD_ISSET(fileno(infp), &t_fds)) {
rc = DoCommand(infile, infp);
if (rc == 0) {
/*
@@ -285,8 +295,8 @@ int main(int argc, char **argv)
if (infile) {
infp = stdin;
infile = NULL;
- printf("\nEnter ? or command:\n");
- FD_SET(fileno(infp), &fds);
+ printf("\nEnter ? or command:\n");
+ FD_SET(fileno(infp), &fds);
} else {
LOG(LOG_INFO, 0, "Exiting.\n");
(void) qcmd_destroyall();
@@ -302,6 +312,7 @@ int main(int argc, char **argv)
* connection and add it to the select list.
*/
int newsock = accept(altqd_socket, NULL, NULL);
+
if (newsock == -1) {
LOG(LOG_ERR, errno, "accept error\n");
continue;