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