Return to mqttd.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / mqtt / src |
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: pthread_mutex_t mtx_sess, mtx_pub;
15: FILE *logg;
16: extern char compiled[], compiledby[], compilehost[];
17: static char szCfgName[MAXPATHLEN];
18: intptr_t Kill;
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:
46: syslog(LOG_ERR, "Error:: can't reload #%d - %s", cfg_GetErrno(), cfg_GetError());
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);
71:
72: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
73: while ((ch = getopt(argc, argv, "hvbc:")) != -1)
74: switch (ch) {
75: case 'c':
76: strlcpy(szCfgName, optarg, sizeof szCfgName);
77: break;
78: case 'b':
79: batch++;
80: break;
81: case 'v':
82: io_incDebug;
83: break;
84: case 'h':
85: default:
86: Usage();
87: return 1;
88: }
89: argc -= optind;
90: argv += optind;
91:
1.2.2.1 misho 92: if (cfgLoadConfig(szCfgName, &cfg)) {
1.2 misho 93: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
94: return 1;
95: }
96: pthread_mutex_init(&mtx_sess, NULL);
97: pthread_mutex_init(&mtx_pub, NULL);
98: openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.2.2.1 misho 99: /* load 3 plugins */
1.2 misho 100: for (i = 0; i < 3; i++)
101: if (!mqttLoadRTLM(&cfg, i)) {
102: printf("Error:: Can't load RTL module\n");
103: while (i--)
104: mqttUnloadRTLM(i);
1.2.2.1 misho 105: cfgUnloadConfig(&cfg);
1.2 misho 106: closelog();
107: pthread_mutex_destroy(&mtx_pub);
108: pthread_mutex_destroy(&mtx_sess);
109: return 2;
110: }
111: acc = call.OpenACC(&cfg);
112: if (!acc) {
113: ret = 3;
114: goto end;
115: }
116: pub = call.OpenPUB(&cfg);
117: if (!pub) {
118: ret = 3;
119: goto end;
120: }
121: logg = call.OpenLOG(&cfg);
122: if (!logg) {
123: ret = 3;
124: goto end;
125: }
126:
127: if (mqttMkDir(&cfg)) {
128: printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
129: ret = 3;
130: goto end;
131: }
132:
133: if (!batch)
134: switch (fork()) {
135: case -1:
136: printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
137: ret = 5;
138: goto end;
139: case 0:
140: setsid();
141:
142: ret = open("/dev/null", O_RDWR);
143: if (ret != -1) {
144: dup2(ret, STDIN_FILENO);
145: dup2(ret, STDOUT_FILENO);
146: dup2(ret, STDERR_FILENO);
147: close(ret);
148: }
149: ioDEBUG(2, "Welcome MQTT service into shadow land!");
150: break;
151: default:
152: ioDEBUG(2, "MQTT service go to shadow land ...");
153: sleep(1);
154: ret = 0;
155: goto end;
156: }
157: else
158: ioVERBOSE(1) printf("Start service in batch mode ...\n");
159:
160: memset(&sa, 0, sizeof sa);
161: sigemptyset(&sa.sa_mask);
162: sa.sa_handler = sigHand;
163: sigaction(SIGHUP, &sa, NULL);
164: sigaction(SIGTERM, &sa, NULL);
165: sigaction(SIGCHLD, &sa, NULL);
166: sigaction(SIGPIPE, &sa, NULL);
1.2.2.2 ! misho 167: ioDEBUG(2, "Service is ready for starting engine ...");
1.2 misho 168:
169: if ((sock = srv_Socket(&cfg)) == -1) {
170: ret = 4;
171: goto end;
172: }
173:
1.2.2.1 misho 174: cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
175: pass = getpwnam(AIT_GET_STR(&v));
176: AIT_FREE_VAL(&v);
1.2 misho 177: if (pass) {
178: setgid(pass->pw_gid);
179: setuid(pass->pw_uid);
180: ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
181: }
182:
183: if (!(root = schedBegin())) {
184: printf("Error:: scheduler #%d - %s\n", sched_GetErrno(), sched_GetError());
185: ret = 6;
186: goto end;
187: }
188:
189: Run(sock);
190:
191: schedEnd(&root);
192: end:
193: srv_Close(sock);
194: call.CloseLOG(logg);
195: call.ClosePUB(pub);
196: call.CloseACC(acc);
197: for (i = 0; i < 3; i++)
198: mqttUnloadRTLM(i);
199: closelog();
1.2.2.1 misho 200: cfgUnloadConfig(&cfg);
1.2 misho 201: pthread_mutex_destroy(&mtx_pub);
202: pthread_mutex_destroy(&mtx_sess);
203: return ret;
1.1 misho 204: }