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