File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqttd.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Sun Oct 8 22:49:25 2017 UTC (6 years, 8 months ago) by misho
Branches: MAIN
CVS tags: mqtt2_1, MQTT2_0, HEAD
version 2.0

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: mqttd.c,v 1.5 2017/10/08 22:49:25 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "mqttd.h"
   48: #include "rtlm.h"
   49: #include "utils.h"
   50: #include "daemon.h"
   51: 
   52: 
   53: cfg_root_t cfg;
   54: sessions_t Sessions;
   55: sched_root_task_t *root;
   56: sqlite3 *acc, *pub;
   57: FILE *logg;
   58: extern char compiled[], compiledby[], compilehost[];
   59: static char szCfgName[MAXPATHLEN];
   60: volatile intptr_t Kill;
   61: 
   62: 
   63: static void
   64: Usage(void)
   65: {
   66: 	printf(	" -= MQTT Broker =- MQTT Service from ELWIX\n"
   67: 		"=== %s@%s === Compiled: %s ===\n\n"
   68: 		"\t-c <config>\tService config\n"
   69: 		"\t-b\t\tBatch mode\n"
   70: 		"\t-v\t\tVerbose (more -vvv, more verbose)\n"
   71: 		"\t-h\t\tHelp! This screen\n\n", 
   72: 		compiledby, compilehost, compiled);
   73: }
   74: 
   75: static void
   76: sigHand(int sig)
   77: {
   78: 	int stat;
   79: 
   80: 	switch (sig) {
   81: 		case SIGHUP:
   82: 			cfgUnloadConfig(&cfg);
   83: 			if (!cfgLoadConfig(szCfgName, &cfg)) {
   84: 				EVERBOSE(1, "Config reload OK!");
   85: 				break;
   86: 			}
   87: 
   88: 			ELIBERR(cfg);
   89: 		case SIGINT:
   90: 		case SIGTERM:
   91: 			EVERBOSE(1, "Terminate MQTT service in progress");
   92: 			Kill++;
   93: 			break;
   94: 		case SIGCHLD:
   95: 			while (waitpid(-1, &stat, WNOHANG) > 0);
   96: 			break;
   97: 		case SIGPIPE:
   98: 			break;
   99: 	}
  100: }
  101: 
  102: 
  103: int
  104: main(int argc, char **argv)
  105: {
  106: 	char ch, batch = 0;
  107: 	register int i;
  108: 	int sock = -1, ret = 0;
  109: 	struct passwd *pass;
  110: 	struct sigaction sa;
  111: 	ait_val_t v;
  112: 
  113: 	TAILQ_INIT(&Sessions);
  114: 
  115: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
  116: 	while ((ch = getopt(argc, argv, "hvbc:")) != -1)
  117: 		switch (ch) {
  118: 			case 'c':
  119: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
  120: 				break;
  121: 			case 'b':
  122: 				batch++;
  123: 				break;
  124: 			case 'v':
  125: 				e_incVerbose;
  126: 				EVERBS(2) elwix_Debug |= ELWIX_DEBUG_TRACE;
  127: 				break;
  128: 			case 'h':
  129: 			default:
  130: 				Usage();
  131: 				return 1;
  132: 		}
  133: 	argc -= optind;
  134: 	argv += optind;
  135: 
  136: 	if (cfgLoadConfig(szCfgName, &cfg)) {
  137: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
  138: 		return 1;
  139: 	}
  140: 	openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
  141: 	/* load 3 plugins */
  142: 	for (i = 0; i < 3; i++)
  143: 		if (!mqttLoadRTLM(&cfg, i)) {
  144: 			printf("Error:: Can't load RTL module\n");
  145: 			mqttUnloadRTLM(acc);
  146: 			mqttUnloadRTLM(pub);
  147: 			mqttUnloadRTLM(logg);
  148: 			cfgUnloadConfig(&cfg);
  149: 			closelog();
  150: 			return 2;
  151: 		}
  152: 	acc = call.OpenACC(&cfg);
  153: 	if (!acc) {
  154: 		ret = 3;
  155: 		goto end;
  156: 	}
  157: 	pub = call.OpenPUB(&cfg);
  158: 	if (!pub) {
  159: 		ret = 3;
  160: 		goto end;
  161: 	}
  162: 	logg = call.OpenLOG(&cfg);
  163: 	if (!logg) {
  164: 		ret = 3;
  165: 		goto end;
  166: 	}
  167: 
  168: 
  169: 	if (mqttMkDir(&cfg)) {
  170: 		printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
  171: 		ret = 3;
  172: 		goto end;
  173: 	}
  174: 
  175: 	if (!batch)
  176: 		switch (fork()) {
  177: 			case -1:
  178: 				printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
  179: 				ret = 5;
  180: 				goto end;
  181: 			case 0:
  182: 				setsid();
  183: 
  184: 				ret = open("/dev/null", O_RDWR);
  185: 				if (ret != -1) {
  186: 					dup2(ret, STDIN_FILENO);
  187: 					dup2(ret, STDOUT_FILENO);
  188: 					dup2(ret, STDERR_FILENO);
  189: 					close(ret);
  190: 				}
  191: 				EVERBOSE(2, "Welcome MQTT service into shadow land!");
  192: 				break;
  193: 			default:
  194: 				EVERBOSE(2, "MQTT service go to shadow land ...");
  195: 				sleep(1);
  196: 				ret = 0;
  197: 				goto end;
  198: 		}
  199: 	else
  200: 		EVERBOSE(1, "Start service in batch mode ...");
  201: 
  202: 	memset(&sa, 0, sizeof sa);
  203: 	sigemptyset(&sa.sa_mask);
  204: 	sa.sa_handler = sigHand;
  205: 	sigaction(SIGHUP, &sa, NULL);
  206: 	sigaction(SIGINT, &sa, NULL);
  207: 	sigaction(SIGTERM, &sa, NULL);
  208: 	sigaction(SIGCHLD, &sa, NULL);
  209: 	sigaction(SIGPIPE, &sa, NULL);
  210: 	EVERBOSE(2, "Service is ready for starting engine ...");
  211: 
  212: 	if ((sock = srv_Socket(&cfg)) == -1) {
  213: 		ret = 4;
  214: 		goto end;
  215: 	}
  216: 
  217: 	cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
  218: 	pass = getpwnam(AIT_GET_STR(&v));
  219: 	AIT_FREE_VAL(&v);
  220: 	if (pass) {
  221: 		setgid(pass->pw_gid);
  222: 		setuid(pass->pw_uid);
  223: 		EVERBOSE(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
  224: 	}
  225: 
  226: 	if (!(root = schedBegin())) {
  227: 		ELIBERR(sched);
  228: 		ret = 6;
  229: 		goto end;
  230: 	}
  231: 
  232: 	/* go catch the cat ... */
  233: 	Run(sock);
  234: 
  235: 	schedEnd(&root);
  236: end:	/* free all resources */
  237: 	srv_Close(sock);
  238: 	call.CloseLOG(logg);
  239: 	call.ClosePUB(pub);
  240: 	call.CloseACC(acc);
  241: 	mqttUnloadRTLM(acc);
  242: 	mqttUnloadRTLM(pub);
  243: 	mqttUnloadRTLM(logg);
  244: 	cfgUnloadConfig(&cfg);
  245: 	closelog();
  246: 	return ret;
  247: }

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