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