File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqttd.c
Revision 1.1.1.1.2.14: download - view: text, annotated - select for diffs - revision graph
Thu Dec 29 14:13:13 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
fix mqttd to handle sigpipe :)
add file mgmt to publish

    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: sl_config cfg;
   11: sessions_t Sessions;
   12: sched_root_task_t *root;
   13: sqlite3 *acc, *pub;
   14: pthread_mutex_t mtx_sess;
   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: 			UnloadConfig(&cfg);
   41: 			if (!LoadConfig(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, szStr[STRSIZ];
   64: 	register int i;
   65: 	int sock = -1, ret = 0;
   66: 	struct passwd *pass;
   67: 	struct sigaction sa;
   68: 
   69: 	TAILQ_INIT(&Sessions);
   70: 
   71: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
   72: 	while ((ch = getopt(argc, argv, "hvbc:")) != -1)
   73: 		switch (ch) {
   74: 			case 'c':
   75: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
   76: 				break;
   77: 			case 'b':
   78: 				batch++;
   79: 				break;
   80: 			case 'v':
   81: 				io_incDebug;
   82: 				break;
   83: 			case 'h':
   84: 			default:
   85: 				Usage();
   86: 				return 1;
   87: 		}
   88: 	argc -= optind;
   89: 	argv += optind;
   90: 
   91: 	if (LoadConfig(szCfgName, &cfg)) {
   92: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
   93: 		return 1;
   94: 	}
   95: 	pthread_mutex_init(&mtx_sess, NULL);
   96: 	openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
   97: 	for (i = 0; i < 3; i++)
   98: 		if (!mqttLoadRTLM(&cfg, i)) {
   99: 			printf("Error:: Can't load RTL module\n");
  100: 			while (i--)
  101: 				mqttUnloadRTLM(i);
  102: 			UnloadConfig(&cfg);
  103: 			closelog();
  104: 			pthread_mutex_destroy(&mtx_sess);
  105: 			return 2;
  106: 		}
  107: 	acc = call.OpenACC(&cfg);
  108: 	if (!acc) {
  109: 		ret = 3;
  110: 		goto end;
  111: 	}
  112: 	pub = call.OpenPUB(&cfg);
  113: 	if (!pub) {
  114: 		ret = 3;
  115: 		goto end;
  116: 	}
  117: 	logg = call.OpenLOG(&cfg);
  118: 	if (!logg) {
  119: 		ret = 3;
  120: 		goto end;
  121: 	}
  122: 
  123: 	if (mqttMkDir(&cfg)) {
  124: 		printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
  125: 		ret = 3;
  126: 		goto end;
  127: 	}
  128: 
  129: 	if (!batch)
  130: 		switch (fork()) {
  131: 			case -1:
  132: 				printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
  133: 				ret = 5;
  134: 				goto end;
  135: 			case 0:
  136: 				setsid();
  137: 
  138: 				ret = open("/dev/null", O_RDWR);
  139: 				if (ret != -1) {
  140: 					dup2(ret, STDIN_FILENO);
  141: 					dup2(ret, STDOUT_FILENO);
  142: 					dup2(ret, STDERR_FILENO);
  143: 					close(ret);
  144: 				}
  145: 				ioDEBUG(2, "Welcome MQTT service into shadow land!");
  146: 				break;
  147: 			default:
  148: 				ioDEBUG(2, "MQTT service go to shadow land ...");
  149: 				sleep(1);
  150: 				ret = 0;
  151: 				goto end;
  152: 		}
  153: 	else
  154: 		ioVERBOSE(1) printf("Start service in batch mode ...\n");
  155: 
  156: 	memset(&sa, 0, sizeof sa);
  157: 	sigemptyset(&sa.sa_mask);
  158: 	sa.sa_handler = sigHand;
  159: 	sigaction(SIGHUP, &sa, NULL);
  160: 	sigaction(SIGTERM, &sa, NULL);
  161: 	sigaction(SIGCHLD, &sa, NULL);
  162: 	sigaction(SIGPIPE, &sa, NULL);
  163: 	ioDEBUG(2, "Service is ready for start engine ...");
  164: 
  165: 	if ((sock = srv_Socket(&cfg)) == -1) {
  166: 		ret = 4;
  167: 		goto end;
  168: 	}
  169: 
  170: 	cfg_LoadAttribute(&cfg, CFG("mqttd"), CFG("user"), CFG(szStr), sizeof szStr, MQTT_USER);
  171: 	pass = getpwnam(szStr);
  172: 	if (pass) {
  173: 		setgid(pass->pw_gid);
  174: 		setuid(pass->pw_uid);
  175: 		ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
  176: 	}
  177: 
  178: 	if (!(root = schedBegin())) {
  179: 		printf("Error:: scheduler #%d - %s\n", sched_GetErrno(), sched_GetError());
  180: 		ret = 6;
  181: 		goto end;
  182: 	}
  183: 
  184: 	Run(sock);
  185: 
  186: 	schedEnd(&root);
  187: end:
  188: 	srv_Close(sock);
  189: 	call.CloseLOG(logg);
  190: 	call.ClosePUB(pub);
  191: 	call.CloseACC(acc);
  192: 	for (i = 0; i < 3; i++)
  193: 		mqttUnloadRTLM(i);
  194: 	closelog();
  195: 	UnloadConfig(&cfg);
  196: 	pthread_mutex_destroy(&mtx_sess);
  197: 	return ret;
  198: }

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