File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqttd.c
Revision 1.2.2.3: download - view: text, annotated - select for diffs - revision graph
Sun Apr 15 21:48:23 2012 UTC (12 years, 2 months ago) by misho
Branches: mqtt1_1
moved printf to ioMacros

    1: #include "global.h"
    2: #include "mqttd.h"
    3: #include "rtlm.h"
    4: #include "utils.h"
    5: #include "daemon.h"
    6: 
    7: 
    8: io_enableDEBUG;
    9: 
   10: cfg_root_t cfg;
   11: sessions_t Sessions;
   12: sched_root_task_t *root;
   13: sqlite3 *acc, *pub;
   14: pthread_mutex_t mtx_sess, mtx_pub;
   15: FILE *logg;
   16: extern char compiled[], compiledby[], compilehost[];
   17: static char szCfgName[MAXPATHLEN];
   18: intptr_t Kill;
   19: 
   20: 
   21: static void
   22: Usage(void)
   23: {
   24: 	printf(	" -= MQTT Broker =- MQTT Service from ELWIX\n"
   25: 		"=== %s@%s === Compiled: %s ===\n\n"
   26: 		"\t-c <config>\tService config\n"
   27: 		"\t-b\t\tBatch mode\n"
   28: 		"\t-v\t\tVerbose (more -vvv, more verbose)\n"
   29: 		"\t-h\t\tHelp! This screen\n\n", 
   30: 		compiledby, compilehost, compiled);
   31: }
   32: 
   33: static void
   34: sigHand(int sig)
   35: {
   36: 	int stat;
   37: 
   38: 	switch (sig) {
   39: 		case SIGHUP:
   40: 			cfgUnloadConfig(&cfg);
   41: 			if (!cfgLoadConfig(szCfgName, &cfg)) {
   42: 				ioDEBUG(1, "Config reload OK!");
   43: 				break;
   44: 			}
   45: 
   46: 			syslog(LOG_ERR, "Error:: can't reload #%d - %s", cfg_GetErrno(), cfg_GetError());
   47: 		case SIGTERM:
   48: 			ioDEBUG(1, "Terminate MQTT service in progress");
   49: 			Kill++;
   50: 			break;
   51: 		case SIGCHLD:
   52: 			while (waitpid(-1, &stat, WNOHANG) > 0);
   53: 			break;
   54: 		case SIGPIPE:
   55: 			break;
   56: 	}
   57: }
   58: 
   59: 
   60: int
   61: main(int argc, char **argv)
   62: {
   63: 	char ch, batch = 0;
   64: 	register int i;
   65: 	int sock = -1, ret = 0;
   66: 	struct passwd *pass;
   67: 	struct sigaction sa;
   68: 	ait_val_t v;
   69: 
   70: 	TAILQ_INIT(&Sessions);
   71: 
   72: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
   73: 	while ((ch = getopt(argc, argv, "hvbc:")) != -1)
   74: 		switch (ch) {
   75: 			case 'c':
   76: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
   77: 				break;
   78: 			case 'b':
   79: 				batch++;
   80: 				break;
   81: 			case 'v':
   82: 				io_incDebug;
   83: 				break;
   84: 			case 'h':
   85: 			default:
   86: 				Usage();
   87: 				return 1;
   88: 		}
   89: 	argc -= optind;
   90: 	argv += optind;
   91: 
   92: 	if (cfgLoadConfig(szCfgName, &cfg)) {
   93: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
   94: 		return 1;
   95: 	}
   96: 	pthread_mutex_init(&mtx_sess, NULL);
   97: 	pthread_mutex_init(&mtx_pub, NULL);
   98: 	openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
   99: 	/* load 3 plugins */
  100: 	for (i = 0; i < 3; i++)
  101: 		if (!mqttLoadRTLM(&cfg, i)) {
  102: 			printf("Error:: Can't load RTL module\n");
  103: 			while (i--)
  104: 				mqttUnloadRTLM(i);
  105: 			cfgUnloadConfig(&cfg);
  106: 			closelog();
  107: 			pthread_mutex_destroy(&mtx_pub);
  108: 			pthread_mutex_destroy(&mtx_sess);
  109: 			return 2;
  110: 		}
  111: 	acc = call.OpenACC(&cfg);
  112: 	if (!acc) {
  113: 		ret = 3;
  114: 		goto end;
  115: 	}
  116: 	pub = call.OpenPUB(&cfg);
  117: 	if (!pub) {
  118: 		ret = 3;
  119: 		goto end;
  120: 	}
  121: 	logg = call.OpenLOG(&cfg);
  122: 	if (!logg) {
  123: 		ret = 3;
  124: 		goto end;
  125: 	}
  126: 
  127: 	if (mqttMkDir(&cfg)) {
  128: 		printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
  129: 		ret = 3;
  130: 		goto end;
  131: 	}
  132: 
  133: 	if (!batch)
  134: 		switch (fork()) {
  135: 			case -1:
  136: 				printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
  137: 				ret = 5;
  138: 				goto end;
  139: 			case 0:
  140: 				setsid();
  141: 
  142: 				ret = open("/dev/null", O_RDWR);
  143: 				if (ret != -1) {
  144: 					dup2(ret, STDIN_FILENO);
  145: 					dup2(ret, STDOUT_FILENO);
  146: 					dup2(ret, STDERR_FILENO);
  147: 					close(ret);
  148: 				}
  149: 				ioDEBUG(2, "Welcome MQTT service into shadow land!");
  150: 				break;
  151: 			default:
  152: 				ioDEBUG(2, "MQTT service go to shadow land ...");
  153: 				sleep(1);
  154: 				ret = 0;
  155: 				goto end;
  156: 		}
  157: 	else
  158: 		ioDEBUG(1, "Start service in batch mode ...");
  159: 
  160: 	memset(&sa, 0, sizeof sa);
  161: 	sigemptyset(&sa.sa_mask);
  162: 	sa.sa_handler = sigHand;
  163: 	sigaction(SIGHUP, &sa, NULL);
  164: 	sigaction(SIGTERM, &sa, NULL);
  165: 	sigaction(SIGCHLD, &sa, NULL);
  166: 	sigaction(SIGPIPE, &sa, NULL);
  167: 	ioDEBUG(2, "Service is ready for starting engine ...");
  168: 
  169: 	if ((sock = srv_Socket(&cfg)) == -1) {
  170: 		ret = 4;
  171: 		goto end;
  172: 	}
  173: 
  174: 	cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
  175: 	pass = getpwnam(AIT_GET_STR(&v));
  176: 	AIT_FREE_VAL(&v);
  177: 	if (pass) {
  178: 		setgid(pass->pw_gid);
  179: 		setuid(pass->pw_uid);
  180: 		ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
  181: 	}
  182: 
  183: 	if (!(root = schedBegin())) {
  184: 		ioLIBERR(sched);
  185: 		ret = 6;
  186: 		goto end;
  187: 	}
  188: 
  189: 	/* go catch the cat ... */
  190: 	Run(sock);
  191: 
  192: 	schedEnd(&root);
  193: end:	/* free all resources */
  194: 	srv_Close(sock);
  195: 	call.CloseLOG(logg);
  196: 	call.ClosePUB(pub);
  197: 	call.CloseACC(acc);
  198: 	for (i = 0; i < 3; i++)
  199: 		mqttUnloadRTLM(i);
  200: 	closelog();
  201: 	cfgUnloadConfig(&cfg);
  202: 	pthread_mutex_destroy(&mtx_pub);
  203: 	pthread_mutex_destroy(&mtx_sess);
  204: 	return ret;
  205: }

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