Annotation of mqtt/src/mqttd.c, revision 1.1.1.1.2.10
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.2 misho 11: extern char compiled[], compiledby[], compilehost[];
1.1.1.1.2.8 misho 12: static char szCfgName[MAXPATHLEN];
13: int Verbose, Kill;
1.1.1.1.2.2 misho 14:
15:
16: static void
17: Usage(void)
18: {
19: printf( " -= MQTT Broker =- MQTT Service from ELWIX\n"
20: "=== %s@%s === Compiled: %s ===\n\n"
21: "\t-c <config>\tService config\n"
1.1.1.1.2.6 misho 22: "\t-b\t\tBatch mode\n"
1.1.1.1.2.2 misho 23: "\t-v\t\tVerbose (more -vvv, more verbose)\n"
24: "\t-h\t\tHelp! This screen\n\n",
25: compiledby, compilehost, compiled);
26: }
1.1 misho 27:
1.1.1.1.2.8 misho 28: static void
29: sigHand(int sig)
30: {
31: int stat;
32:
33: switch (sig) {
34: case SIGHUP:
35: UnloadConfig(&cfg);
36: if (!LoadConfig(szCfgName, &cfg)) {
37: VERB(1) syslog(LOG_DEBUG, "Config reload OK!");
38: break;
39: }
40:
41: syslog(LOG_ERR, "Error:: can't reload #%d - %s", cfg_GetErrno(), cfg_GetError());
42: case SIGTERM:
43: VERB(1) syslog(LOG_DEBUG, "Terminate MQTT service in progress");
44: Kill++;
45: break;
46: case SIGCHLD:
47: while (waitpid(-1, &stat, WNOHANG) > 0);
48: break;
49: }
50: }
51:
1.1 misho 52:
53: int
54: main(int argc, char **argv)
55: {
1.1.1.1.2.8 misho 56: char ch, batch = 0, szStr[STRSIZ];
1.1.1.1.2.3 misho 57: register int i;
58: sqlite3 *acc = NULL, *pub = NULL;
59: FILE *logg = NULL;
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>