Home | History | Annotate | Line # | Download | only in altqd
altqd.c revision 1.7
      1  1.7   itojun /*	$NetBSD: altqd.c,v 1.7 2002/03/05 04:11:51 itojun Exp $	*/
      2  1.7   itojun /*	$KAME: altqd.c,v 1.10 2002/02/20 10:42:26 kjc Exp $	*/
      3  1.1  thorpej /*
      4  1.4   itojun  * Copyright (c) 2001 Theo de Raadt
      5  1.4   itojun  * All rights reserved.
      6  1.4   itojun  *
      7  1.4   itojun  * Redistribution and use in source and binary forms, with or without
      8  1.4   itojun  * modification, are permitted provided that the following conditions
      9  1.4   itojun  * are met:
     10  1.4   itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.4   itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.4   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4   itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.4   itojun  *    documentation and/or other materials provided with the distribution.
     15  1.4   itojun  *
     16  1.4   itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.4   itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.4   itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.4   itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.4   itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.4   itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.4   itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.4   itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.4   itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.4   itojun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.4   itojun  *
     27  1.7   itojun  * Copyright (C) 1997-2002
     28  1.1  thorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
     29  1.1  thorpej  *
     30  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     31  1.1  thorpej  * modification, are permitted provided that the following conditions
     32  1.1  thorpej  * are met:
     33  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     34  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     35  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     36  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     37  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     38  1.1  thorpej  *
     39  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     40  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     41  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     43  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     44  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     45  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     47  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     48  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     49  1.1  thorpej  * SUCH DAMAGE.
     50  1.1  thorpej  */
     51  1.1  thorpej 
     52  1.7   itojun #include <sys/param.h>
     53  1.7   itojun #include <sys/socket.h>
     54  1.7   itojun #include <sys/un.h>
     55  1.7   itojun #include <sys/stat.h>
     56  1.7   itojun #include <net/if.h>
     57  1.7   itojun 
     58  1.1  thorpej #include <stdio.h>
     59  1.1  thorpej #include <stdlib.h>
     60  1.1  thorpej #include <unistd.h>
     61  1.1  thorpej #include <string.h>
     62  1.1  thorpej #include <errno.h>
     63  1.1  thorpej #include <signal.h>
     64  1.1  thorpej #include <fcntl.h>
     65  1.1  thorpej #include <syslog.h>
     66  1.1  thorpej #include <err.h>
     67  1.7   itojun #ifndef __FreeBSD__
     68  1.7   itojun #include <util.h>
     69  1.7   itojun #endif
     70  1.1  thorpej 
     71  1.1  thorpej #include <altq/altq.h>
     72  1.1  thorpej #include "altq_qop.h"
     73  1.1  thorpej #include "quip_server.h"
     74  1.1  thorpej 
     75  1.7   itojun #ifdef __FreeBSD__
     76  1.4   itojun #define  ALTQD_PID_FILE		"/var/run/altqd.pid"
     77  1.7   itojun #endif
     78  1.1  thorpej #define MAX_CLIENT		10
     79  1.1  thorpej 
     80  1.7   itojun static volatile sig_atomic_t gotsig_hup, gotsig_int, gotsig_term;
     81  1.7   itojun 
     82  1.7   itojun static void usage(void);
     83  1.7   itojun static void sig_handler(int);
     84  1.4   itojun 
     85  1.7   itojun static void
     86  1.4   itojun usage(void)
     87  1.1  thorpej {
     88  1.4   itojun 	fprintf(stderr, "usage: altqd [-vd] [-f config]\n");
     89  1.4   itojun 	exit(1);
     90  1.1  thorpej }
     91  1.1  thorpej 
     92  1.7   itojun static void
     93  1.7   itojun sig_handler(int sig)
     94  1.1  thorpej {
     95  1.7   itojun 	switch (sig) {
     96  1.7   itojun 	case SIGHUP:
     97  1.7   itojun 		gotsig_hup = 1;
     98  1.7   itojun 		break;
     99  1.7   itojun 	case SIGINT:
    100  1.7   itojun 		gotsig_int = 1;
    101  1.7   itojun 		break;
    102  1.7   itojun 	case SIGTERM:
    103  1.7   itojun 		gotsig_term = 1;
    104  1.7   itojun 		break;
    105  1.7   itojun 	case SIGPIPE:
    106  1.7   itojun 		/*
    107  1.7   itojun 		 * we have lost an API connection.
    108  1.7   itojun 		 * a subsequent output operation will catch EPIPE.
    109  1.7   itojun 		 */
    110  1.7   itojun 		break;
    111  1.7   itojun 	}
    112  1.1  thorpej }
    113  1.1  thorpej 
    114  1.4   itojun int
    115  1.4   itojun main(int argc, char **argv)
    116  1.1  thorpej {
    117  1.7   itojun 	int	i, c, maxfd, rval, qpsock;
    118  1.7   itojun 	fd_set	fds, rfds;
    119  1.7   itojun 	FILE	*fp, *client[MAX_CLIENT];
    120  1.7   itojun 
    121  1.7   itojun 	m_debug = 0;
    122  1.7   itojun 	l_debug = LOG_INFO;
    123  1.7   itojun 	fp = NULL;
    124  1.7   itojun 	for (i = 0; i < MAX_CLIENT; i++)
    125  1.7   itojun 		client[i] = NULL;
    126  1.1  thorpej 
    127  1.1  thorpej 	while ((c = getopt(argc, argv, "f:vDdl:")) != -1) {
    128  1.1  thorpej 		switch (c) {
    129  1.1  thorpej 		case 'f':
    130  1.1  thorpej 			altqconfigfile = optarg;
    131  1.1  thorpej 			break;
    132  1.1  thorpej 		case 'D':	/* -D => dummy mode */
    133  1.1  thorpej 			Debug_mode = 1;
    134  1.1  thorpej 			printf("Debug mode set.\n");
    135  1.1  thorpej 			break;
    136  1.1  thorpej 		case 'v':
    137  1.1  thorpej 			l_debug = LOG_DEBUG;
    138  1.1  thorpej 			m_debug |= DEBUG_ALTQ;
    139  1.1  thorpej 			daemonize = 0;
    140  1.1  thorpej 			break;
    141  1.1  thorpej 		case 'd':
    142  1.1  thorpej 			daemonize = 0;
    143  1.1  thorpej 			break;
    144  1.1  thorpej 		case 'l':
    145  1.1  thorpej 			l_debug = atoi(optarg);
    146  1.1  thorpej 			break;
    147  1.1  thorpej 		default:
    148  1.1  thorpej 			usage();
    149  1.1  thorpej 		}
    150  1.1  thorpej 	}
    151  1.1  thorpej 
    152  1.7   itojun 	signal(SIGINT, sig_handler);
    153  1.7   itojun 	signal(SIGTERM, sig_handler);
    154  1.7   itojun 	signal(SIGHUP, sig_handler);
    155  1.7   itojun 	signal(SIGPIPE, sig_handler);
    156  1.1  thorpej 
    157  1.1  thorpej 	if (daemonize)
    158  1.2    lukem 		openlog("altqd", LOG_PID, LOG_DAEMON);
    159  1.1  thorpej 
    160  1.1  thorpej 	if (qcmd_init() != 0) {
    161  1.1  thorpej 		if (daemonize)
    162  1.1  thorpej 			closelog();
    163  1.1  thorpej 		exit(1);
    164  1.1  thorpej 	}
    165  1.1  thorpej 
    166  1.1  thorpej 	/*
    167  1.1  thorpej 	 * open a unix domain socket for altqd clients
    168  1.1  thorpej 	 */
    169  1.7   itojun 	if ((qpsock = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
    170  1.6   itojun 		LOG(LOG_ERR, errno, "can't open unix domain socket");
    171  1.1  thorpej 	else {
    172  1.1  thorpej 		struct sockaddr_un addr;
    173  1.1  thorpej 
    174  1.1  thorpej 		bzero(&addr, sizeof(addr));
    175  1.1  thorpej 		addr.sun_family = AF_LOCAL;
    176  1.4   itojun 		strlcpy(addr.sun_path, QUIP_PATH, sizeof(addr.sun_path));
    177  1.4   itojun 		unlink(QUIP_PATH);
    178  1.7   itojun 		if (bind(qpsock, (struct sockaddr *)&addr,
    179  1.4   itojun 		    sizeof(addr)) < 0) {
    180  1.6   itojun 			LOG(LOG_ERR, errno, "can't bind to %s", QUIP_PATH);
    181  1.7   itojun 			close(qpsock);
    182  1.7   itojun 			qpsock = -1;
    183  1.1  thorpej 		}
    184  1.1  thorpej 		chmod(QUIP_PATH, 0666);
    185  1.7   itojun 		if (listen(qpsock, SOMAXCONN) < 0) {
    186  1.6   itojun 			LOG(LOG_ERR, errno, "can't listen to %s", QUIP_PATH);
    187  1.7   itojun 			close(qpsock);
    188  1.7   itojun 			qpsock = -1;
    189  1.1  thorpej 		}
    190  1.1  thorpej 	}
    191  1.1  thorpej 
    192  1.1  thorpej 	if (daemonize) {
    193  1.1  thorpej 		daemon(0, 0);
    194  1.1  thorpej 
    195  1.1  thorpej 		/* save pid to the pid file (/var/tmp/altqd.pid) */
    196  1.7   itojun #ifdef __FreeBSD__
    197  1.7   itojun 		{
    198  1.7   itojun 			FILE *fp;
    199  1.7   itojun 
    200  1.7   itojun 			if ((fp = fopen(ALTQD_PID_FILE, "w")) != NULL) {
    201  1.7   itojun 				fprintf(fp, "%d\n", getpid());
    202  1.7   itojun 				fclose(fp);
    203  1.7   itojun 			} else
    204  1.7   itojun 				LOG(LOG_WARNING, errno, "can't open pid file");
    205  1.7   itojun 		}
    206  1.7   itojun #else
    207  1.7   itojun 		pidfile(NULL);
    208  1.7   itojun #endif
    209  1.1  thorpej 	} else {
    210  1.1  thorpej 		/* interactive mode */
    211  1.7   itojun 		fp = stdin;
    212  1.7   itojun 		printf("\nEnter ? or command:\n");
    213  1.7   itojun 		printf("altqd %s> ", cur_ifname());
    214  1.1  thorpej 		fflush(stdout);
    215  1.1  thorpej 	}
    216  1.1  thorpej 
    217  1.1  thorpej 	/*
    218  1.1  thorpej 	 * go into the command mode.
    219  1.1  thorpej 	 */
    220  1.1  thorpej 	FD_ZERO(&fds);
    221  1.1  thorpej 	maxfd = 0;
    222  1.7   itojun 	if (fp != NULL) {
    223  1.7   itojun 		FD_SET(fileno(fp), &fds);
    224  1.7   itojun 		maxfd = MAX(maxfd, fileno(fp) + 1);
    225  1.1  thorpej 	}
    226  1.7   itojun 	if (qpsock >= 0) {
    227  1.7   itojun 		FD_SET(qpsock, &fds);
    228  1.7   itojun 		maxfd = MAX(maxfd, qpsock + 1);
    229  1.1  thorpej 	}
    230  1.1  thorpej 
    231  1.7   itojun 	rval = 1;
    232  1.7   itojun 	while (rval) {
    233  1.4   itojun 		if (gotsig_hup) {
    234  1.4   itojun 			qcmd_destroyall();
    235  1.4   itojun 			gotsig_hup = 0;
    236  1.7   itojun 			LOG(LOG_INFO, 0, "reinitializing altqd...");
    237  1.7   itojun 			if (qcmd_init() != 0) {
    238  1.7   itojun 				LOG(LOG_INFO, 0, "reinitialization failed");
    239  1.7   itojun 				break;
    240  1.4   itojun 			}
    241  1.4   itojun 		}
    242  1.7   itojun 		if (gotsig_term || gotsig_int) {
    243  1.7   itojun 			LOG(LOG_INFO, 0, "Exiting on signal %d",
    244  1.7   itojun 			    gotsig_term ? SIGTERM : SIGINT);
    245  1.7   itojun 			break;
    246  1.4   itojun 		}
    247  1.4   itojun 
    248  1.7   itojun 		FD_COPY(&fds, &rfds);
    249  1.7   itojun 		if (select(maxfd, &rfds, NULL, NULL, NULL) < 0) {
    250  1.7   itojun 			if (errno != EINTR)
    251  1.7   itojun 				err(1, "select");
    252  1.1  thorpej 			continue;
    253  1.1  thorpej 		}
    254  1.4   itojun 
    255  1.1  thorpej 		/*
    256  1.7   itojun 		 * if there is command input, read the input line,
    257  1.1  thorpej 		 * parse it, and execute.
    258  1.1  thorpej 		 */
    259  1.7   itojun 		if (fp && FD_ISSET(fileno(fp), &rfds)) {
    260  1.7   itojun 			rval = do_command(fp);
    261  1.7   itojun 			if (rval == 0) {
    262  1.7   itojun 				/* quit command or eof on input */
    263  1.7   itojun 				LOG(LOG_INFO, 0, "Exiting.");
    264  1.7   itojun 			} else if (fp == stdin)
    265  1.1  thorpej 				printf("altqd %s> ", cur_ifname());
    266  1.1  thorpej 			fflush(stdout);
    267  1.7   itojun 		} else if (qpsock >= 0 && FD_ISSET(qpsock, &rfds)) {
    268  1.1  thorpej 			/*
    269  1.1  thorpej 			 * quip connection request from client via unix
    270  1.1  thorpej 			 * domain socket; get a new socket for this
    271  1.1  thorpej 			 * connection and add it to the select list.
    272  1.1  thorpej 			 */
    273  1.7   itojun 			int newsock = accept(qpsock, NULL, NULL);
    274  1.4   itojun 
    275  1.1  thorpej 			if (newsock == -1) {
    276  1.6   itojun 				LOG(LOG_ERR, errno, "accept");
    277  1.1  thorpej 				continue;
    278  1.1  thorpej 			}
    279  1.1  thorpej 			FD_SET(newsock, &fds);
    280  1.1  thorpej 			for (i = 0; i < MAX_CLIENT; i++)
    281  1.1  thorpej 				if (client[i] == NULL) {
    282  1.1  thorpej 					client[i] = fdopen(newsock, "r+");
    283  1.1  thorpej 					break;
    284  1.1  thorpej 				}
    285  1.1  thorpej 			maxfd = MAX(maxfd, newsock + 1);
    286  1.1  thorpej 		} else {
    287  1.1  thorpej 			/*
    288  1.1  thorpej 			 * check input from a client via unix domain socket
    289  1.1  thorpej 			 */
    290  1.6   itojun 			for (i = 0; i < MAX_CLIENT; i++) {
    291  1.1  thorpej 				int fd;
    292  1.1  thorpej 
    293  1.1  thorpej 				if (client[i] == NULL)
    294  1.1  thorpej 					continue;
    295  1.1  thorpej 				fd = fileno(client[i]);
    296  1.7   itojun 				if (FD_ISSET(fd, &rfds)) {
    297  1.1  thorpej 					if (quip_input(client[i]) != 0 ||
    298  1.1  thorpej 					    fflush(client[i]) != 0) {
    299  1.1  thorpej 						/* connection closed */
    300  1.1  thorpej 						fclose(client[i]);
    301  1.1  thorpej 						client[i] = NULL;
    302  1.1  thorpej 						FD_CLR(fd, &fds);
    303  1.1  thorpej 					}
    304  1.1  thorpej 				}
    305  1.1  thorpej 			}
    306  1.1  thorpej 		}
    307  1.1  thorpej 	}
    308  1.7   itojun 
    309  1.7   itojun 	/* cleanup and exit */
    310  1.7   itojun 	qcmd_destroyall();
    311  1.7   itojun 	if (qpsock >= 0)
    312  1.7   itojun 		(void)close(qpsock);
    313  1.7   itojun 	unlink(QUIP_PATH);
    314  1.7   itojun 
    315  1.7   itojun 	for (i = 0; i < MAX_CLIENT; i++)
    316  1.7   itojun 		if (client[i] != NULL)
    317  1.7   itojun 			(void)fclose(client[i]);
    318  1.7   itojun 	if (daemonize) {
    319  1.7   itojun #ifdef __FreeBSD__
    320  1.7   itojun 		/* if we have a pid file, remove it */
    321  1.7   itojun 		unlink(ALTQD_PID_FILE);
    322  1.7   itojun #endif
    323  1.7   itojun 		closelog();
    324  1.7   itojun 	}
    325  1.7   itojun 	exit(0);
    326  1.1  thorpej }
    327