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

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: 
        !            10: sl_config cfg;
        !            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:
        !            40:                        UnloadConfig(&cfg);
        !            41:                        if (!LoadConfig(szCfgName, &cfg)) {
        !            42:                                ioDEBUG(1, "Config reload OK!");
        !            43:                                break;
        !            44:                        }
        !            45: 
        !            46:                        syslog(LOG_ERR, "Error:: can't reload #%d - %s", cfg_GetErrno(), cfg_GetError());
        !            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     ! misho      63:        char ch, batch = 0, szStr[STRSIZ];
        !            64:        register int i;
        !            65:        int sock = -1, ret = 0;
        !            66:        struct passwd *pass;
        !            67:        struct sigaction sa;
        !            68: 
        !            69:        TAILQ_INIT(&Sessions);
        !            70: 
        !            71:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
        !            72:        while ((ch = getopt(argc, argv, "hvbc:")) != -1)
        !            73:                switch (ch) {
        !            74:                        case 'c':
        !            75:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
        !            76:                                break;
        !            77:                        case 'b':
        !            78:                                batch++;
        !            79:                                break;
        !            80:                        case 'v':
        !            81:                                io_incDebug;
        !            82:                                break;
        !            83:                        case 'h':
        !            84:                        default:
        !            85:                                Usage();
        !            86:                                return 1;
        !            87:                }
        !            88:        argc -= optind;
        !            89:        argv += optind;
        !            90: 
        !            91:        if (LoadConfig(szCfgName, &cfg)) {
        !            92:                printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
        !            93:                return 1;
        !            94:        }
        !            95:        pthread_mutex_init(&mtx_sess, NULL);
        !            96:        pthread_mutex_init(&mtx_pub, NULL);
        !            97:        openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
        !            98:        for (i = 0; i < 3; i++)
        !            99:                if (!mqttLoadRTLM(&cfg, i)) {
        !           100:                        printf("Error:: Can't load RTL module\n");
        !           101:                        while (i--)
        !           102:                                mqttUnloadRTLM(i);
        !           103:                        UnloadConfig(&cfg);
        !           104:                        closelog();
        !           105:                        pthread_mutex_destroy(&mtx_pub);
        !           106:                        pthread_mutex_destroy(&mtx_sess);
        !           107:                        return 2;
        !           108:                }
        !           109:        acc = call.OpenACC(&cfg);
        !           110:        if (!acc) {
        !           111:                ret = 3;
        !           112:                goto end;
        !           113:        }
        !           114:        pub = call.OpenPUB(&cfg);
        !           115:        if (!pub) {
        !           116:                ret = 3;
        !           117:                goto end;
        !           118:        }
        !           119:        logg = call.OpenLOG(&cfg);
        !           120:        if (!logg) {
        !           121:                ret = 3;
        !           122:                goto end;
        !           123:        }
        !           124: 
        !           125:        if (mqttMkDir(&cfg)) {
        !           126:                printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
        !           127:                ret = 3;
        !           128:                goto end;
        !           129:        }
        !           130: 
        !           131:        if (!batch)
        !           132:                switch (fork()) {
        !           133:                        case -1:
        !           134:                                printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
        !           135:                                ret = 5;
        !           136:                                goto end;
        !           137:                        case 0:
        !           138:                                setsid();
        !           139: 
        !           140:                                ret = open("/dev/null", O_RDWR);
        !           141:                                if (ret != -1) {
        !           142:                                        dup2(ret, STDIN_FILENO);
        !           143:                                        dup2(ret, STDOUT_FILENO);
        !           144:                                        dup2(ret, STDERR_FILENO);
        !           145:                                        close(ret);
        !           146:                                }
        !           147:                                ioDEBUG(2, "Welcome MQTT service into shadow land!");
        !           148:                                break;
        !           149:                        default:
        !           150:                                ioDEBUG(2, "MQTT service go to shadow land ...");
        !           151:                                sleep(1);
        !           152:                                ret = 0;
        !           153:                                goto end;
        !           154:                }
        !           155:        else
        !           156:                ioVERBOSE(1) printf("Start service in batch mode ...\n");
        !           157: 
        !           158:        memset(&sa, 0, sizeof sa);
        !           159:        sigemptyset(&sa.sa_mask);
        !           160:        sa.sa_handler = sigHand;
        !           161:        sigaction(SIGHUP, &sa, NULL);
        !           162:        sigaction(SIGTERM, &sa, NULL);
        !           163:        sigaction(SIGCHLD, &sa, NULL);
        !           164:        sigaction(SIGPIPE, &sa, NULL);
        !           165:        ioDEBUG(2, "Service is ready for start engine ...");
        !           166: 
        !           167:        if ((sock = srv_Socket(&cfg)) == -1) {
        !           168:                ret = 4;
        !           169:                goto end;
        !           170:        }
        !           171: 
        !           172:        cfg_LoadAttribute(&cfg, CFG("mqttd"), CFG("user"), CFG(szStr), sizeof szStr, MQTT_USER);
        !           173:        pass = getpwnam(szStr);
        !           174:        if (pass) {
        !           175:                setgid(pass->pw_gid);
        !           176:                setuid(pass->pw_uid);
        !           177:                ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
        !           178:        }
        !           179: 
        !           180:        if (!(root = schedBegin())) {
        !           181:                printf("Error:: scheduler #%d - %s\n", sched_GetErrno(), sched_GetError());
        !           182:                ret = 6;
        !           183:                goto end;
        !           184:        }
        !           185: 
        !           186:        Run(sock);
        !           187: 
        !           188:        schedEnd(&root);
        !           189: end:
        !           190:        srv_Close(sock);
        !           191:        call.CloseLOG(logg);
        !           192:        call.ClosePUB(pub);
        !           193:        call.CloseACC(acc);
        !           194:        for (i = 0; i < 3; i++)
        !           195:                mqttUnloadRTLM(i);
        !           196:        closelog();
        !           197:        UnloadConfig(&cfg);
        !           198:        pthread_mutex_destroy(&mtx_pub);
        !           199:        pthread_mutex_destroy(&mtx_sess);
        !           200:        return ret;
1.1       misho     201: }

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