Annotation of mqtt/src/mqttd.c, revision 1.1.1.1.2.6
1.1 misho 1: #include "global.h"
1.1.1.1.2.1 misho 2: #include "rtlm.h"
1.1.1.1.2.4 misho 3: #include "utils.h"
1.1.1.1.2.6! misho 4: #include "daemon.h"
1.1.1.1.2.1 misho 5:
6:
7: sl_config cfg;
1.1.1.1.2.2 misho 8: extern char compiled[], compiledby[], compilehost[];
9: int Verbose;
10:
11:
12: static void
13: Usage(void)
14: {
15: printf( " -= MQTT Broker =- MQTT Service from ELWIX\n"
16: "=== %s@%s === Compiled: %s ===\n\n"
17: "\t-c <config>\tService config\n"
1.1.1.1.2.6! misho 18: "\t-b\t\tBatch mode\n"
1.1.1.1.2.2 misho 19: "\t-v\t\tVerbose (more -vvv, more verbose)\n"
20: "\t-h\t\tHelp! This screen\n\n",
21: compiledby, compilehost, compiled);
22: }
1.1 misho 23:
24:
25: int
26: main(int argc, char **argv)
27: {
1.1.1.1.2.6! misho 28: char ch, batch = 0, szStr[STRSIZ], szCfgName[MAXPATHLEN];
1.1.1.1.2.3 misho 29: register int i;
30: sqlite3 *acc = NULL, *pub = NULL;
31: FILE *logg = NULL;
1.1.1.1.2.6! misho 32: int sock = -1, ret = 0;
! 33: struct passwd *pass;
1.1.1.1.2.2 misho 34:
35: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
1.1.1.1.2.6! misho 36: while ((ch = getopt(argc, argv, "hvbc:")) != -1)
1.1.1.1.2.2 misho 37: switch (ch) {
38: case 'c':
39: strlcpy(szCfgName, optarg, sizeof szCfgName);
40: break;
1.1.1.1.2.6! misho 41: case 'b':
! 42: batch++;
! 43: break;
1.1.1.1.2.2 misho 44: case 'v':
45: Verbose++;
46: break;
47: case 'h':
48: default:
49: Usage();
50: return 1;
51: }
52: argc -= optind;
53: argv += optind;
54:
55: if (LoadConfig(szCfgName, &cfg)) {
56: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
57: return 1;
58: }
1.1.1.1.2.6! misho 59: openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.1.1.1.2.3 misho 60: for (i = 0; i < 3; i++)
61: if (!mqttLoadRTLM(&cfg, i)) {
62: printf("Error:: Can't load RTL module\n");
63: while (i--)
64: mqttUnloadRTLM(i);
65: UnloadConfig(&cfg);
1.1.1.1.2.6! misho 66: closelog();
1.1.1.1.2.3 misho 67: return 2;
68: }
69: acc = call.OpenACC(&cfg);
1.1.1.1.2.5 misho 70: if (!acc) {
71: ret = 3;
1.1.1.1.2.3 misho 72: goto end;
1.1.1.1.2.5 misho 73: }
1.1.1.1.2.3 misho 74: pub = call.OpenPUB(&cfg);
1.1.1.1.2.5 misho 75: if (!pub) {
76: ret = 3;
1.1.1.1.2.3 misho 77: goto end;
1.1.1.1.2.5 misho 78: }
1.1.1.1.2.3 misho 79: logg = call.OpenLOG(&cfg);
1.1.1.1.2.5 misho 80: if (!logg) {
81: ret = 3;
1.1.1.1.2.3 misho 82: goto end;
1.1.1.1.2.5 misho 83: }
1.1.1.1.2.3 misho 84:
85: if (mqttMkDir(&cfg)) {
86: printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
1.1.1.1.2.5 misho 87: ret = 3;
1.1.1.1.2.3 misho 88: goto end;
89: }
90:
1.1.1.1.2.6! misho 91: VERB(2) syslog(LOG_DEBUG, "Service is ready for start engine ...");
1.1.1.1.2.2 misho 92:
1.1.1.1.2.5 misho 93: if ((sock = srv_Socket(&cfg)) == -1) {
94: ret = 4;
95: goto end;
96: }
97:
1.1.1.1.2.6! misho 98: if (!batch)
! 99: switch (fork()) {
! 100: case -1:
! 101: printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
! 102: ret = 5;
! 103: goto end;
! 104: case 0:
! 105: setsid();
! 106: cfg_LoadAttribute(&cfg, CFG("mqttd"), CFG("user"), CFG(szStr),
! 107: sizeof szStr, MQTT_USER);
! 108: pass = getpwnam(szStr);
! 109: if (pass) {
! 110: setgid(pass->pw_gid);
! 111: setuid(pass->pw_uid);
! 112: VERB(1) syslog(LOG_WARNING, "Try to change group #%d and user #%d",
! 113: pass->pw_gid, pass->pw_uid);
! 114: }
! 115: ret = open("/dev/null", O_RDWR);
! 116: if (ret != -1) {
! 117: dup2(ret, STDIN_FILENO);
! 118: dup2(ret, STDOUT_FILENO);
! 119: dup2(ret, STDERR_FILENO);
! 120: close(ret);
! 121: }
! 122: VERB(2) syslog(LOG_DEBUG, "Welcome MQTT service into shadow land!");
! 123: break;
! 124: default:
! 125: VERB(2) syslog(LOG_DEBUG, "MQTT service go to shadow land ...");
! 126: ret = 0;
! 127: goto end;
! 128: }
! 129: else
! 130: VERB(1) printf("Start service in batch mode ...\n");
! 131:
! 132: Run();
! 133:
1.1.1.1.2.3 misho 134: end:
1.1.1.1.2.6! misho 135: if (sock > STDERR_FILENO)
! 136: srv_Close(sock);
1.1.1.1.2.3 misho 137: call.CloseLOG(logg);
138: call.ClosePUB(pub);
139: call.CloseACC(acc);
140: for (i = 0; i < 3; i++)
141: mqttUnloadRTLM(i);
1.1.1.1.2.6! misho 142: closelog();
1.1.1.1.2.2 misho 143: UnloadConfig(&cfg);
1.1.1.1.2.5 misho 144: return ret;
1.1 misho 145: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>