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

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: 
1.1.1.1.2.13  misho       8: io_enableDEBUG;
                      9: 
1.1.1.1.2.1  misho      10: sl_config cfg;
1.1.1.1.2.10  misho      11: sessions_t Sessions;
1.1.1.1.2.7  misho      12: sched_root_task_t *root;
1.1.1.1.2.11  misho      13: sqlite3 *acc, *pub;
1.1.1.1.2.12  misho      14: pthread_mutex_t mtx_sess;
1.1.1.1.2.11  misho      15: FILE *logg;
1.1.1.1.2.2  misho      16: extern char compiled[], compiledby[], compilehost[];
1.1.1.1.2.8  misho      17: static char szCfgName[MAXPATHLEN];
1.1.1.1.2.13  misho      18: intptr_t Kill;
1.1.1.1.2.2  misho      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"
1.1.1.1.2.6  misho      27:                "\t-b\t\tBatch mode\n"
1.1.1.1.2.2  misho      28:                "\t-v\t\tVerbose (more -vvv, more verbose)\n"
                     29:                "\t-h\t\tHelp! This screen\n\n", 
                     30:                compiledby, compilehost, compiled);
                     31: }
1.1       misho      32: 
1.1.1.1.2.8  misho      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)) {
1.1.1.1.2.13  misho      42:                                ioDEBUG(1, "Config reload OK!");
1.1.1.1.2.8  misho      43:                                break;
                     44:                        }
                     45: 
                     46:                        syslog(LOG_ERR, "Error:: can't reload #%d - %s", cfg_GetErrno(), cfg_GetError());
                     47:                case SIGTERM:
1.1.1.1.2.13  misho      48:                        ioDEBUG(1, "Terminate MQTT service in progress");
1.1.1.1.2.8  misho      49:                        Kill++;
                     50:                        break;
                     51:                case SIGCHLD:
                     52:                        while (waitpid(-1, &stat, WNOHANG) > 0);
                     53:                        break;
1.1.1.1.2.14! misho      54:                case SIGPIPE:
        !            55:                        break;
1.1.1.1.2.8  misho      56:        }
                     57: }
                     58: 
1.1       misho      59: 
                     60: int
                     61: main(int argc, char **argv)
                     62: {
1.1.1.1.2.8  misho      63:        char ch, batch = 0, szStr[STRSIZ];
1.1.1.1.2.3  misho      64:        register int i;
1.1.1.1.2.6  misho      65:        int sock = -1, ret = 0;
                     66:        struct passwd *pass;
1.1.1.1.2.8  misho      67:        struct sigaction sa;
1.1.1.1.2.2  misho      68: 
1.1.1.1.2.10  misho      69:        TAILQ_INIT(&Sessions);
                     70: 
1.1.1.1.2.2  misho      71:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
1.1.1.1.2.6  misho      72:        while ((ch = getopt(argc, argv, "hvbc:")) != -1)
1.1.1.1.2.2  misho      73:                switch (ch) {
                     74:                        case 'c':
                     75:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                     76:                                break;
1.1.1.1.2.6  misho      77:                        case 'b':
                     78:                                batch++;
                     79:                                break;
1.1.1.1.2.2  misho      80:                        case 'v':
1.1.1.1.2.13  misho      81:                                io_incDebug;
1.1.1.1.2.2  misho      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:        }
1.1.1.1.2.12  misho      95:        pthread_mutex_init(&mtx_sess, NULL);
1.1.1.1.2.6  misho      96:        openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.1.1.1.2.3  misho      97:        for (i = 0; i < 3; i++)
                     98:                if (!mqttLoadRTLM(&cfg, i)) {
                     99:                        printf("Error:: Can't load RTL module\n");
                    100:                        while (i--)
                    101:                                mqttUnloadRTLM(i);
                    102:                        UnloadConfig(&cfg);
1.1.1.1.2.6  misho     103:                        closelog();
1.1.1.1.2.12  misho     104:                        pthread_mutex_destroy(&mtx_sess);
1.1.1.1.2.3  misho     105:                        return 2;
                    106:                }
                    107:        acc = call.OpenACC(&cfg);
1.1.1.1.2.5  misho     108:        if (!acc) {
                    109:                ret = 3;
1.1.1.1.2.3  misho     110:                goto end;
1.1.1.1.2.5  misho     111:        }
1.1.1.1.2.3  misho     112:        pub = call.OpenPUB(&cfg);
1.1.1.1.2.5  misho     113:        if (!pub) {
                    114:                ret = 3;
1.1.1.1.2.3  misho     115:                goto end;
1.1.1.1.2.5  misho     116:        }
1.1.1.1.2.3  misho     117:        logg = call.OpenLOG(&cfg);
1.1.1.1.2.5  misho     118:        if (!logg) {
                    119:                ret = 3;
1.1.1.1.2.3  misho     120:                goto end;
1.1.1.1.2.5  misho     121:        }
1.1.1.1.2.3  misho     122: 
                    123:        if (mqttMkDir(&cfg)) {
                    124:                printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
1.1.1.1.2.5  misho     125:                ret = 3;
1.1.1.1.2.3  misho     126:                goto end;
                    127:        }
                    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:                                }
1.1.1.1.2.13  misho     145:                                ioDEBUG(2, "Welcome MQTT service into shadow land!");
1.1.1.1.2.6  misho     146:                                break;
                    147:                        default:
1.1.1.1.2.13  misho     148:                                ioDEBUG(2, "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
1.1.1.1.2.13  misho     154:                ioVERBOSE(1) printf("Start service in batch mode ...\n");
1.1.1.1.2.6  misho     155: 
1.1.1.1.2.13  misho     156:        memset(&sa, 0, sizeof sa);
                    157:        sigemptyset(&sa.sa_mask);
                    158:        sa.sa_handler = sigHand;
                    159:        sigaction(SIGHUP, &sa, NULL);
                    160:        sigaction(SIGTERM, &sa, NULL);
                    161:        sigaction(SIGCHLD, &sa, NULL);
1.1.1.1.2.14! misho     162:        sigaction(SIGPIPE, &sa, NULL);
1.1.1.1.2.13  misho     163:        ioDEBUG(2, "Service is ready for start engine ...");
1.1.1.1.2.8  misho     164: 
                    165:        if ((sock = srv_Socket(&cfg)) == -1) {
                    166:                ret = 4;
                    167:                goto end;
                    168:        }
                    169: 
                    170:        cfg_LoadAttribute(&cfg, CFG("mqttd"), CFG("user"), CFG(szStr), sizeof szStr, MQTT_USER);
                    171:        pass = getpwnam(szStr);
                    172:        if (pass) {
                    173:                setgid(pass->pw_gid);
                    174:                setuid(pass->pw_uid);
1.1.1.1.2.13  misho     175:                ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
1.1.1.1.2.8  misho     176:        }
                    177: 
1.1.1.1.2.7  misho     178:        if (!(root = schedBegin())) {
                    179:                printf("Error:: scheduler #%d - %s\n", sched_GetErrno(), sched_GetError());
                    180:                ret = 6;
                    181:                goto end;
                    182:        }
                    183: 
                    184:        Run(sock);
1.1.1.1.2.6  misho     185: 
1.1.1.1.2.7  misho     186:        schedEnd(&root);
1.1.1.1.2.3  misho     187: end:
1.1.1.1.2.13  misho     188:        srv_Close(sock);
1.1.1.1.2.3  misho     189:        call.CloseLOG(logg);
                    190:        call.ClosePUB(pub);
                    191:        call.CloseACC(acc);
                    192:        for (i = 0; i < 3; i++)
                    193:                mqttUnloadRTLM(i);
1.1.1.1.2.6  misho     194:        closelog();
1.1.1.1.2.2  misho     195:        UnloadConfig(&cfg);
1.1.1.1.2.12  misho     196:        pthread_mutex_destroy(&mtx_sess);
1.1.1.1.2.5  misho     197:        return ret;
1.1       misho     198: }

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