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