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