Annotation of mqtt/src/mqttd.c, revision 1.1.1.1.2.13
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.12 misho 14: pthread_mutex_t mtx_sess;
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;
54: }
55: }
56:
1.1 misho 57:
58: int
59: main(int argc, char **argv)
60: {
1.1.1.1.2.8 misho 61: char ch, batch = 0, szStr[STRSIZ];
1.1.1.1.2.3 misho 62: register int i;
1.1.1.1.2.6 misho 63: int sock = -1, ret = 0;
64: struct passwd *pass;
1.1.1.1.2.8 misho 65: struct sigaction sa;
1.1.1.1.2.2 misho 66:
1.1.1.1.2.10 misho 67: TAILQ_INIT(&Sessions);
68:
1.1.1.1.2.2 misho 69: strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
1.1.1.1.2.6 misho 70: while ((ch = getopt(argc, argv, "hvbc:")) != -1)
1.1.1.1.2.2 misho 71: switch (ch) {
72: case 'c':
73: strlcpy(szCfgName, optarg, sizeof szCfgName);
74: break;
1.1.1.1.2.6 misho 75: case 'b':
76: batch++;
77: break;
1.1.1.1.2.2 misho 78: case 'v':
1.1.1.1.2.13! misho 79: io_incDebug;
1.1.1.1.2.2 misho 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: }
1.1.1.1.2.12 misho 93: pthread_mutex_init(&mtx_sess, NULL);
1.1.1.1.2.6 misho 94: openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.1.1.1.2.3 misho 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);
1.1.1.1.2.6 misho 101: closelog();
1.1.1.1.2.12 misho 102: pthread_mutex_destroy(&mtx_sess);
1.1.1.1.2.3 misho 103: return 2;
104: }
105: acc = call.OpenACC(&cfg);
1.1.1.1.2.5 misho 106: if (!acc) {
107: ret = 3;
1.1.1.1.2.3 misho 108: goto end;
1.1.1.1.2.5 misho 109: }
1.1.1.1.2.3 misho 110: pub = call.OpenPUB(&cfg);
1.1.1.1.2.5 misho 111: if (!pub) {
112: ret = 3;
1.1.1.1.2.3 misho 113: goto end;
1.1.1.1.2.5 misho 114: }
1.1.1.1.2.3 misho 115: logg = call.OpenLOG(&cfg);
1.1.1.1.2.5 misho 116: if (!logg) {
117: ret = 3;
1.1.1.1.2.3 misho 118: goto end;
1.1.1.1.2.5 misho 119: }
1.1.1.1.2.3 misho 120:
121: if (mqttMkDir(&cfg)) {
122: printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
1.1.1.1.2.5 misho 123: ret = 3;
1.1.1.1.2.3 misho 124: goto end;
125: }
126:
1.1.1.1.2.6 misho 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();
1.1.1.1.2.8 misho 135:
1.1.1.1.2.6 misho 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: }
1.1.1.1.2.13! misho 143: ioDEBUG(2, "Welcome MQTT service into shadow land!");
1.1.1.1.2.6 misho 144: break;
145: default:
1.1.1.1.2.13! misho 146: ioDEBUG(2, "MQTT service go to shadow land ...");
1.1.1.1.2.8 misho 147: sleep(1);
1.1.1.1.2.6 misho 148: ret = 0;
149: goto end;
150: }
151: else
1.1.1.1.2.13! misho 152: ioVERBOSE(1) printf("Start service in batch mode ...\n");
1.1.1.1.2.6 misho 153:
1.1.1.1.2.13! misho 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 ...");
1.1.1.1.2.8 misho 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);
1.1.1.1.2.13! misho 172: ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
1.1.1.1.2.8 misho 173: }
174:
1.1.1.1.2.7 misho 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);
1.1.1.1.2.6 misho 182:
1.1.1.1.2.7 misho 183: schedEnd(&root);
1.1.1.1.2.3 misho 184: end:
1.1.1.1.2.13! misho 185: srv_Close(sock);
1.1.1.1.2.3 misho 186: call.CloseLOG(logg);
187: call.ClosePUB(pub);
188: call.CloseACC(acc);
189: for (i = 0; i < 3; i++)
190: mqttUnloadRTLM(i);
1.1.1.1.2.6 misho 191: closelog();
1.1.1.1.2.2 misho 192: UnloadConfig(&cfg);
1.1.1.1.2.12 misho 193: pthread_mutex_destroy(&mtx_sess);
1.1.1.1.2.5 misho 194: return ret;
1.1 misho 195: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>