File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / mqttd.c
Revision 1.1.1.1.2.10: download - view: text, annotated - select for diffs - revision graph
Fri Dec 9 09:53:54 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
patch libaitmqtt readCONNECT() for return undefined string with size
allocate if needed will strings

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

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