Annotation of mqtt/src/mqttd.c, revision 1.2.2.8
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");
100: while (i--)
101: mqttUnloadRTLM(i);
1.2.2.1 misho 102: cfgUnloadConfig(&cfg);
1.2 misho 103: closelog();
104: return 2;
105: }
106: acc = call.OpenACC(&cfg);
107: if (!acc) {
108: ret = 3;
109: goto end;
110: }
111: pub = call.OpenPUB(&cfg);
112: if (!pub) {
113: ret = 3;
114: goto end;
115: }
116: logg = call.OpenLOG(&cfg);
117: if (!logg) {
118: ret = 3;
119: goto end;
120: }
121:
122: if (mqttMkDir(&cfg)) {
123: printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
124: ret = 3;
125: goto end;
126: }
127:
128: if (!batch)
129: switch (fork()) {
130: case -1:
131: printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
132: ret = 5;
133: goto end;
134: case 0:
135: setsid();
136:
137: ret = open("/dev/null", O_RDWR);
138: if (ret != -1) {
139: dup2(ret, STDIN_FILENO);
140: dup2(ret, STDOUT_FILENO);
141: dup2(ret, STDERR_FILENO);
142: close(ret);
143: }
144: ioDEBUG(2, "Welcome MQTT service into shadow land!");
145: break;
146: default:
147: ioDEBUG(2, "MQTT service go to shadow land ...");
148: sleep(1);
149: ret = 0;
150: goto end;
151: }
152: else
1.2.2.3 misho 153: ioDEBUG(1, "Start service in batch mode ...");
1.2 misho 154:
155: memset(&sa, 0, sizeof sa);
156: sigemptyset(&sa.sa_mask);
157: sa.sa_handler = sigHand;
158: sigaction(SIGHUP, &sa, NULL);
159: sigaction(SIGTERM, &sa, NULL);
160: sigaction(SIGCHLD, &sa, NULL);
161: sigaction(SIGPIPE, &sa, NULL);
1.2.2.2 misho 162: ioDEBUG(2, "Service is ready for starting engine ...");
1.2 misho 163:
164: if ((sock = srv_Socket(&cfg)) == -1) {
165: ret = 4;
166: goto end;
167: }
168:
1.2.2.1 misho 169: cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
170: pass = getpwnam(AIT_GET_STR(&v));
171: AIT_FREE_VAL(&v);
1.2 misho 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())) {
1.2.2.3 misho 179: ioLIBERR(sched);
1.2 misho 180: ret = 6;
181: goto end;
182: }
183:
1.2.2.3 misho 184: /* go catch the cat ... */
1.2 misho 185: Run(sock);
186:
187: schedEnd(&root);
1.2.2.3 misho 188: end: /* free all resources */
1.2 misho 189: srv_Close(sock);
190: call.CloseLOG(logg);
191: call.ClosePUB(pub);
192: call.CloseACC(acc);
193: for (i = 0; i < 3; i++)
194: mqttUnloadRTLM(i);
195: closelog();
1.2.2.1 misho 196: cfgUnloadConfig(&cfg);
1.2 misho 197: return ret;
1.1 misho 198: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>