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

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

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