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

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

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