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