File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / dwds.c
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Wed Oct 27 15:23:49 2010 UTC (13 years, 8 months ago) by misho
Branches: tools1_0
added new source for wifi tools

    1: #include "global.h"
    2: #include "dwds.h"
    3: 
    4: 
    5: sl_config cfg;
    6: int Verbose, Kill, nif;
    7: char **ifs, szConfig[MAXPATHLEN] = DWDS_CONFIG;
    8: struct dwds_if *wds;
    9: extern char compiled[], compiledby[], compilehost[];
   10: 
   11: 
   12: static void
   13: Usage()
   14: {
   15: 	printf(	"-= dWDS =- WiFi dynamic WDS service managment for VAP\n"
   16: 		"=== %s === %s@%s ===\n\n"
   17: 		"  Syntax: dwds [options] <interface|any> [interface [...]]\n"
   18: 		"\n"
   19: 		"\t-v\t\tVerbose ...\n"
   20: 		"\t-f\t\tForeground, not demonize process ...\n"
   21: 		"\t-c <config>\tConfig file [default=/etc/dwds.conf]\n"
   22: 		"\n", compiled, compiledby, compilehost);
   23: }
   24: 
   25: static void
   26: sigHandler(int sig)
   27: {
   28: 	int stat;
   29: 	const u_char *v;
   30: 	char szStr[STRSIZ];
   31: 
   32: 	switch (sig) {
   33: 		case SIGHUP:
   34: 			UnloadConfig(&cfg);
   35: 			if (LoadConfig(szConfig, &cfg)) {
   36: 				printf("Error:: can`t load config %s ...\n", szConfig);
   37: 				Kill++;
   38: 			} else {
   39: 				closelog();
   40: 
   41: 				cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("name"), CFG(szStr), STRSIZ, DWDS_NAME);
   42: 				openlog(szStr, LOG_PID | LOG_CONS, LOG_DAEMON);
   43: 				v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("syslog_upto"));
   44: 				setlogmask(v ? strtol((char*) v, NULL, 0) : 0);
   45: 			}
   46: 			break;
   47: 		case SIGTERM:
   48: 			Kill++;
   49: 			break;
   50: 		case SIGCHLD:
   51: 			while (waitpid(-1, &stat, WNOHANG) > 0);
   52: 			break;
   53: 	}
   54: }
   55: 
   56: static int
   57: RtMsg(struct rt_msghdr *msg, size_t len)
   58: {
   59: 	return 0;
   60: }
   61: 
   62: // ---------------------------------------------------------------
   63: 
   64: int
   65: main(int argc, char **argv)
   66: {
   67: 	char ch, szStr[STRSIZ], fg = 0;
   68: 	const u_char *v, msg[2048];
   69: 	int s;
   70: 	struct sigaction sa;
   71: 	size_t len;
   72: 
   73: 	while ((ch = getopt(argc, argv, "hvfc:")) != -1)
   74: 		switch (ch) {
   75: 			case 'v':
   76: 				Verbose++;
   77: 				break;
   78: 			case 'f':
   79: 				fg = 1;
   80: 				break;
   81: 			case 'c':
   82: 				strlcpy(szConfig, optarg, MAXPATHLEN);
   83: 				break;
   84: 			case 'h':
   85: 			default:
   86: 				Usage();
   87: 				return 1;
   88: 		}
   89: 	argc -= optind;
   90: 	argv += optind;
   91: 	if (!argc) {
   92: 		printf("Error:: not specified interface for use ...\n");
   93: 		Usage();
   94: 		return 1;
   95: 	} else {
   96: 		nif = argc;
   97: 		ifs = argv;
   98: 	}
   99: 	if (LoadConfig(szConfig, &cfg)) {
  100: 		printf("Error:: can`t load config %s ...\n", szConfig);
  101: 		return 1;
  102: 	}
  103: 
  104: 	if (fg)
  105: 		switch (fork()) {
  106: 			case -1:
  107: 				printf("Error:: when fork() #%d - %s\n", errno, strerror(errno));
  108: 				UnloadConfig(&cfg);
  109: 				return 2;
  110: 			case 0 :
  111: 				VERB(1) printf("Going to shadow land ...\n");
  112: 
  113: 				setsid();
  114: 
  115: 				memset(&sa, 0, sizeof sa);
  116: 				sa.sa_handler = sigHandler;
  117: 				sigemptyset(&sa.sa_mask);
  118: 				sigaction(SIGHUP, &sa, NULL);
  119: 				sigaction(SIGTERM, &sa, NULL);
  120: 				sigaction(SIGCHLD, &sa, NULL);
  121: 				break;
  122: 			default:
  123: 				goto end;
  124: 		}
  125: 
  126: 	cfg_LoadAttribute(&cfg, CFG("dwds"), CFG("name"), CFG(szStr), STRSIZ, DWDS_NAME);
  127: 	openlog(szStr, LOG_PID | LOG_CONS, LOG_DAEMON);
  128: 	v = cfg_GetAttribute(&cfg, CFG("dwds"), CFG("syslog_upto"));
  129: 	setlogmask(v ? strtol((char*) v, NULL, 0) : 0);
  130: 
  131: 	s = socket(PF_ROUTE, SOCK_RAW, 0);
  132: 	if (s == -1) {
  133: 		syslog(LOG_ERR, "Error:: socket() #%d - %s\n", errno, strerror(errno));
  134: 		goto end;
  135: 	}
  136: 
  137: 	while (!Kill) {
  138: 		len = read(s, (void*) msg, sizeof msg);
  139: 		if (len == -1) {
  140: 			syslog(LOG_ERR, "Error:: read() #%d - %s\n", errno, strerror(errno));
  141: 			Kill++;
  142: 		} else
  143: 			RtMsg((struct rt_msghdr*) msg, len);
  144: 	}
  145: 
  146: 	close(s);
  147: end:
  148: 	closelog();
  149: 	UnloadConfig(&cfg);
  150: 	return 0;
  151: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>