Annotation of mqtt/src/mqttd.c, revision 1.2.2.7
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;
1.2.2.5 misho 12: pubs_t Pubs;
1.2 misho 13: sched_root_task_t *root;
14: sqlite3 *acc, *pub;
15: FILE *logg;
16: extern char compiled[], compiledby[], compilehost[];
17: static char szCfgName[MAXPATHLEN];
1.2.2.6 misho 18: volatile intptr_t Kill;
1.2 misho 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:
1.2.2.1 misho 40: cfgUnloadConfig(&cfg);
41: if (!cfgLoadConfig(szCfgName, &cfg)) {
1.2 misho 42: ioDEBUG(1, "Config reload OK!");
43: break;
44: }
45:
1.2.2.4 misho 46: ioLIBERR(cfg);
1.2 misho 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: }
1.1 misho 58:
59:
60: int
61: main(int argc, char **argv)
62: {
1.2.2.1 misho 63: char ch, batch = 0;
1.2 misho 64: register int i;
65: int sock = -1, ret = 0;
66: struct passwd *pass;
67: struct sigaction sa;
1.2.2.1 misho 68: ait_val_t v;
1.2 misho 69:
70: TAILQ_INIT(&Sessions);
1.2.2.5 misho 71: TAILQ_INIT(&Pubs);
1.2 misho 72:
73: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
74: while ((ch = getopt(argc, argv, "hvbc:")) != -1)
75: switch (ch) {
76: case 'c':
77: strlcpy(szCfgName, optarg, sizeof szCfgName);
78: break;
79: case 'b':
80: batch++;
81: break;
82: case 'v':
83: io_incDebug;
84: break;
85: case 'h':
86: default:
87: Usage();
88: return 1;
89: }
90: argc -= optind;
91: argv += optind;
92:
1.2.2.1 misho 93: if (cfgLoadConfig(szCfgName, &cfg)) {
1.2 misho 94: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
95: return 1;
96: }
97: openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.2.2.1 misho 98: /* load 3 plugins */
1.2 misho 99: for (i = 0; i < 3; i++)
100: if (!mqttLoadRTLM(&cfg, i)) {
101: printf("Error:: Can't load RTL module\n");
102: while (i--)
103: mqttUnloadRTLM(i);
1.2.2.1 misho 104: cfgUnloadConfig(&cfg);
1.2 misho 105: closelog();
106: return 2;
107: }
108: acc = call.OpenACC(&cfg);
109: if (!acc) {
110: ret = 3;
111: goto end;
112: }
113: pub = call.OpenPUB(&cfg);
114: if (!pub) {
115: ret = 3;
116: goto end;
117: }
118: logg = call.OpenLOG(&cfg);
119: if (!logg) {
120: ret = 3;
121: goto end;
122: }
123:
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);
195: for (i = 0; i < 3; i++)
196: mqttUnloadRTLM(i);
197: closelog();
1.2.2.1 misho 198: cfgUnloadConfig(&cfg);
1.2 misho 199: return ret;
1.1 misho 200: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>