Annotation of mqtt/src/mqttd.c, revision 1.2.2.4

1.1       misho       1: #include "global.h"
1.2       misho       2: #include "mqttd.h"
                      3: #include "rtlm.h"
                      4: #include "utils.h"
                      5: #include "daemon.h"
                      6: 
                      7: 
                      8: io_enableDEBUG;
                      9: 
1.2.2.1   misho      10: cfg_root_t cfg;
1.2       misho      11: sessions_t Sessions;
                     12: sched_root_task_t *root;
                     13: sqlite3 *acc, *pub;
                     14: pthread_mutex_t mtx_sess, mtx_pub;
                     15: FILE *logg;
                     16: extern char compiled[], compiledby[], compilehost[];
                     17: static char szCfgName[MAXPATHLEN];
                     18: intptr_t Kill;
                     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"
                     27:                "\t-b\t\tBatch mode\n"
                     28:                "\t-v\t\tVerbose (more -vvv, more verbose)\n"
                     29:                "\t-h\t\tHelp! This screen\n\n", 
                     30:                compiledby, compilehost, compiled);
                     31: }
                     32: 
                     33: static void
                     34: sigHand(int sig)
                     35: {
                     36:        int stat;
                     37: 
                     38:        switch (sig) {
                     39:                case SIGHUP:
1.2.2.1   misho      40:                        cfgUnloadConfig(&cfg);
                     41:                        if (!cfgLoadConfig(szCfgName, &cfg)) {
1.2       misho      42:                                ioDEBUG(1, "Config reload OK!");
                     43:                                break;
                     44:                        }
                     45: 
1.2.2.4 ! misho      46:                        ioLIBERR(cfg);
1.2       misho      47:                case SIGTERM:
                     48:                        ioDEBUG(1, "Terminate MQTT service in progress");
                     49:                        Kill++;
                     50:                        break;
                     51:                case SIGCHLD:
                     52:                        while (waitpid(-1, &stat, WNOHANG) > 0);
                     53:                        break;
                     54:                case SIGPIPE:
                     55:                        break;
                     56:        }
                     57: }
1.1       misho      58: 
                     59: 
                     60: int
                     61: main(int argc, char **argv)
                     62: {
1.2.2.1   misho      63:        char ch, batch = 0;
1.2       misho      64:        register int i;
                     65:        int sock = -1, ret = 0;
                     66:        struct passwd *pass;
                     67:        struct sigaction sa;
1.2.2.1   misho      68:        ait_val_t v;
1.2       misho      69: 
                     70:        TAILQ_INIT(&Sessions);
                     71: 
                     72:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
                     73:        while ((ch = getopt(argc, argv, "hvbc:")) != -1)
                     74:                switch (ch) {
                     75:                        case 'c':
                     76:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                     77:                                break;
                     78:                        case 'b':
                     79:                                batch++;
                     80:                                break;
                     81:                        case 'v':
                     82:                                io_incDebug;
                     83:                                break;
                     84:                        case 'h':
                     85:                        default:
                     86:                                Usage();
                     87:                                return 1;
                     88:                }
                     89:        argc -= optind;
                     90:        argv += optind;
                     91: 
1.2.2.1   misho      92:        if (cfgLoadConfig(szCfgName, &cfg)) {
1.2       misho      93:                printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
                     94:                return 1;
                     95:        }
                     96:        pthread_mutex_init(&mtx_sess, NULL);
                     97:        pthread_mutex_init(&mtx_pub, NULL);
                     98:        openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.2.2.1   misho      99:        /* load 3 plugins */
1.2       misho     100:        for (i = 0; i < 3; i++)
                    101:                if (!mqttLoadRTLM(&cfg, i)) {
                    102:                        printf("Error:: Can't load RTL module\n");
                    103:                        while (i--)
                    104:                                mqttUnloadRTLM(i);
1.2.2.1   misho     105:                        cfgUnloadConfig(&cfg);
1.2       misho     106:                        closelog();
                    107:                        pthread_mutex_destroy(&mtx_pub);
                    108:                        pthread_mutex_destroy(&mtx_sess);
                    109:                        return 2;
                    110:                }
                    111:        acc = call.OpenACC(&cfg);
                    112:        if (!acc) {
                    113:                ret = 3;
                    114:                goto end;
                    115:        }
                    116:        pub = call.OpenPUB(&cfg);
                    117:        if (!pub) {
                    118:                ret = 3;
                    119:                goto end;
                    120:        }
                    121:        logg = call.OpenLOG(&cfg);
                    122:        if (!logg) {
                    123:                ret = 3;
                    124:                goto end;
                    125:        }
                    126: 
                    127:        if (mqttMkDir(&cfg)) {
                    128:                printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
                    129:                ret = 3;
                    130:                goto end;
                    131:        }
                    132: 
                    133:        if (!batch)
                    134:                switch (fork()) {
                    135:                        case -1:
                    136:                                printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
                    137:                                ret = 5;
                    138:                                goto end;
                    139:                        case 0:
                    140:                                setsid();
                    141: 
                    142:                                ret = open("/dev/null", O_RDWR);
                    143:                                if (ret != -1) {
                    144:                                        dup2(ret, STDIN_FILENO);
                    145:                                        dup2(ret, STDOUT_FILENO);
                    146:                                        dup2(ret, STDERR_FILENO);
                    147:                                        close(ret);
                    148:                                }
                    149:                                ioDEBUG(2, "Welcome MQTT service into shadow land!");
                    150:                                break;
                    151:                        default:
                    152:                                ioDEBUG(2, "MQTT service go to shadow land ...");
                    153:                                sleep(1);
                    154:                                ret = 0;
                    155:                                goto end;
                    156:                }
                    157:        else
1.2.2.3   misho     158:                ioDEBUG(1, "Start service in batch mode ...");
1.2       misho     159: 
                    160:        memset(&sa, 0, sizeof sa);
                    161:        sigemptyset(&sa.sa_mask);
                    162:        sa.sa_handler = sigHand;
                    163:        sigaction(SIGHUP, &sa, NULL);
                    164:        sigaction(SIGTERM, &sa, NULL);
                    165:        sigaction(SIGCHLD, &sa, NULL);
                    166:        sigaction(SIGPIPE, &sa, NULL);
1.2.2.2   misho     167:        ioDEBUG(2, "Service is ready for starting engine ...");
1.2       misho     168: 
                    169:        if ((sock = srv_Socket(&cfg)) == -1) {
                    170:                ret = 4;
                    171:                goto end;
                    172:        }
                    173: 
1.2.2.1   misho     174:        cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
                    175:        pass = getpwnam(AIT_GET_STR(&v));
                    176:        AIT_FREE_VAL(&v);
1.2       misho     177:        if (pass) {
                    178:                setgid(pass->pw_gid);
                    179:                setuid(pass->pw_uid);
                    180:                ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
                    181:        }
                    182: 
                    183:        if (!(root = schedBegin())) {
1.2.2.3   misho     184:                ioLIBERR(sched);
1.2       misho     185:                ret = 6;
                    186:                goto end;
                    187:        }
                    188: 
1.2.2.3   misho     189:        /* go catch the cat ... */
1.2       misho     190:        Run(sock);
                    191: 
                    192:        schedEnd(&root);
1.2.2.3   misho     193: end:   /* free all resources */
1.2       misho     194:        srv_Close(sock);
                    195:        call.CloseLOG(logg);
                    196:        call.ClosePUB(pub);
                    197:        call.CloseACC(acc);
                    198:        for (i = 0; i < 3; i++)
                    199:                mqttUnloadRTLM(i);
                    200:        closelog();
1.2.2.1   misho     201:        cfgUnloadConfig(&cfg);
1.2       misho     202:        pthread_mutex_destroy(&mtx_pub);
                    203:        pthread_mutex_destroy(&mtx_sess);
                    204:        return ret;
1.1       misho     205: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>