1: #include "global.h"
2: #include "mqttd.h"
3: #include "rtlm.h"
4: #include "utils.h"
5: #include "daemon.h"
6:
7:
8: io_enableDEBUG;
9:
10: sl_config cfg;
11: sessions_t Sessions;
12: sched_root_task_t *root;
13: sqlite3 *acc, *pub;
14: pthread_mutex_t mtx_sess;
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:
40: UnloadConfig(&cfg);
41: if (!LoadConfig(szCfgName, &cfg)) {
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: }
55: }
56:
57:
58: int
59: main(int argc, char **argv)
60: {
61: char ch, batch = 0, szStr[STRSIZ];
62: register int i;
63: int sock = -1, ret = 0;
64: struct passwd *pass;
65: struct sigaction sa;
66:
67: TAILQ_INIT(&Sessions);
68:
69: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
70: while ((ch = getopt(argc, argv, "hvbc:")) != -1)
71: switch (ch) {
72: case 'c':
73: strlcpy(szCfgName, optarg, sizeof szCfgName);
74: break;
75: case 'b':
76: batch++;
77: break;
78: case 'v':
79: io_incDebug;
80: break;
81: case 'h':
82: default:
83: Usage();
84: return 1;
85: }
86: argc -= optind;
87: argv += optind;
88:
89: if (LoadConfig(szCfgName, &cfg)) {
90: printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
91: return 1;
92: }
93: pthread_mutex_init(&mtx_sess, NULL);
94: openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
95: for (i = 0; i < 3; i++)
96: if (!mqttLoadRTLM(&cfg, i)) {
97: printf("Error:: Can't load RTL module\n");
98: while (i--)
99: mqttUnloadRTLM(i);
100: UnloadConfig(&cfg);
101: closelog();
102: pthread_mutex_destroy(&mtx_sess);
103: return 2;
104: }
105: acc = call.OpenACC(&cfg);
106: if (!acc) {
107: ret = 3;
108: goto end;
109: }
110: pub = call.OpenPUB(&cfg);
111: if (!pub) {
112: ret = 3;
113: goto end;
114: }
115: logg = call.OpenLOG(&cfg);
116: if (!logg) {
117: ret = 3;
118: goto end;
119: }
120:
121: if (mqttMkDir(&cfg)) {
122: printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
123: ret = 3;
124: goto end;
125: }
126:
127: if (!batch)
128: switch (fork()) {
129: case -1:
130: printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
131: ret = 5;
132: goto end;
133: case 0:
134: setsid();
135:
136: ret = open("/dev/null", O_RDWR);
137: if (ret != -1) {
138: dup2(ret, STDIN_FILENO);
139: dup2(ret, STDOUT_FILENO);
140: dup2(ret, STDERR_FILENO);
141: close(ret);
142: }
143: ioDEBUG(2, "Welcome MQTT service into shadow land!");
144: break;
145: default:
146: ioDEBUG(2, "MQTT service go to shadow land ...");
147: sleep(1);
148: ret = 0;
149: goto end;
150: }
151: else
152: ioVERBOSE(1) printf("Start service in batch mode ...\n");
153:
154: memset(&sa, 0, sizeof sa);
155: sigemptyset(&sa.sa_mask);
156: sa.sa_handler = sigHand;
157: sigaction(SIGHUP, &sa, NULL);
158: sigaction(SIGTERM, &sa, NULL);
159: sigaction(SIGCHLD, &sa, NULL);
160: ioDEBUG(2, "Service is ready for start engine ...");
161:
162: if ((sock = srv_Socket(&cfg)) == -1) {
163: ret = 4;
164: goto end;
165: }
166:
167: cfg_LoadAttribute(&cfg, CFG("mqttd"), CFG("user"), CFG(szStr), sizeof szStr, MQTT_USER);
168: pass = getpwnam(szStr);
169: if (pass) {
170: setgid(pass->pw_gid);
171: setuid(pass->pw_uid);
172: ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
173: }
174:
175: if (!(root = schedBegin())) {
176: printf("Error:: scheduler #%d - %s\n", sched_GetErrno(), sched_GetError());
177: ret = 6;
178: goto end;
179: }
180:
181: Run(sock);
182:
183: schedEnd(&root);
184: end:
185: srv_Close(sock);
186: call.CloseLOG(logg);
187: call.ClosePUB(pub);
188: call.CloseACC(acc);
189: for (i = 0; i < 3; i++)
190: mqttUnloadRTLM(i);
191: closelog();
192: UnloadConfig(&cfg);
193: pthread_mutex_destroy(&mtx_sess);
194: return ret;
195: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>