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

1.4     ! misho       1: /*************************************************************************
        !             2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
        !             3: *  by Michael Pounov <misho@openbsd-bg.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: mqttd.c,v 1.3.2.1 2012/07/03 12:22:56 misho Exp $
        !             7: *
        !             8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
1.1       misho      46: #include "global.h"
1.2       misho      47: #include "mqttd.h"
                     48: #include "rtlm.h"
                     49: #include "utils.h"
                     50: #include "daemon.h"
                     51: 
                     52: 
                     53: io_enableDEBUG;
                     54: 
1.3       misho      55: cfg_root_t cfg;
1.2       misho      56: sessions_t Sessions;
                     57: sched_root_task_t *root;
                     58: sqlite3 *acc, *pub;
                     59: FILE *logg;
                     60: extern char compiled[], compiledby[], compilehost[];
                     61: static char szCfgName[MAXPATHLEN];
1.3       misho      62: volatile intptr_t Kill;
1.2       misho      63: 
                     64: 
                     65: static void
                     66: Usage(void)
                     67: {
                     68:        printf( " -= MQTT Broker =- MQTT Service from ELWIX\n"
                     69:                "=== %s@%s === Compiled: %s ===\n\n"
                     70:                "\t-c <config>\tService config\n"
                     71:                "\t-b\t\tBatch mode\n"
                     72:                "\t-v\t\tVerbose (more -vvv, more verbose)\n"
                     73:                "\t-h\t\tHelp! This screen\n\n", 
                     74:                compiledby, compilehost, compiled);
                     75: }
                     76: 
                     77: static void
                     78: sigHand(int sig)
                     79: {
                     80:        int stat;
                     81: 
                     82:        switch (sig) {
                     83:                case SIGHUP:
1.3       misho      84:                        cfgUnloadConfig(&cfg);
                     85:                        if (!cfgLoadConfig(szCfgName, &cfg)) {
1.2       misho      86:                                ioDEBUG(1, "Config reload OK!");
                     87:                                break;
                     88:                        }
                     89: 
1.3       misho      90:                        ioLIBERR(cfg);
                     91:                case SIGINT:
1.2       misho      92:                case SIGTERM:
                     93:                        ioDEBUG(1, "Terminate MQTT service in progress");
                     94:                        Kill++;
                     95:                        break;
                     96:                case SIGCHLD:
                     97:                        while (waitpid(-1, &stat, WNOHANG) > 0);
                     98:                        break;
                     99:                case SIGPIPE:
                    100:                        break;
                    101:        }
                    102: }
1.1       misho     103: 
                    104: 
                    105: int
                    106: main(int argc, char **argv)
                    107: {
1.3       misho     108:        char ch, batch = 0;
1.2       misho     109:        register int i;
                    110:        int sock = -1, ret = 0;
                    111:        struct passwd *pass;
                    112:        struct sigaction sa;
1.3       misho     113:        ait_val_t v;
1.2       misho     114: 
                    115:        TAILQ_INIT(&Sessions);
                    116: 
                    117:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
                    118:        while ((ch = getopt(argc, argv, "hvbc:")) != -1)
                    119:                switch (ch) {
                    120:                        case 'c':
                    121:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                    122:                                break;
                    123:                        case 'b':
                    124:                                batch++;
                    125:                                break;
                    126:                        case 'v':
                    127:                                io_incDebug;
                    128:                                break;
                    129:                        case 'h':
                    130:                        default:
                    131:                                Usage();
                    132:                                return 1;
                    133:                }
                    134:        argc -= optind;
                    135:        argv += optind;
                    136: 
1.3       misho     137:        if (cfgLoadConfig(szCfgName, &cfg)) {
1.2       misho     138:                printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
                    139:                return 1;
                    140:        }
                    141:        openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.3       misho     142:        /* load 3 plugins */
1.2       misho     143:        for (i = 0; i < 3; i++)
                    144:                if (!mqttLoadRTLM(&cfg, i)) {
                    145:                        printf("Error:: Can't load RTL module\n");
1.3       misho     146:                        mqttUnloadRTLM(acc);
                    147:                        mqttUnloadRTLM(pub);
                    148:                        mqttUnloadRTLM(logg);
                    149:                        cfgUnloadConfig(&cfg);
1.2       misho     150:                        closelog();
                    151:                        return 2;
                    152:                }
                    153:        acc = call.OpenACC(&cfg);
                    154:        if (!acc) {
                    155:                ret = 3;
                    156:                goto end;
                    157:        }
                    158:        pub = call.OpenPUB(&cfg);
                    159:        if (!pub) {
                    160:                ret = 3;
                    161:                goto end;
                    162:        }
                    163:        logg = call.OpenLOG(&cfg);
                    164:        if (!logg) {
                    165:                ret = 3;
                    166:                goto end;
                    167:        }
                    168: 
1.3       misho     169: 
1.2       misho     170:        if (mqttMkDir(&cfg)) {
                    171:                printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
                    172:                ret = 3;
                    173:                goto end;
                    174:        }
                    175: 
                    176:        if (!batch)
                    177:                switch (fork()) {
                    178:                        case -1:
                    179:                                printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
                    180:                                ret = 5;
                    181:                                goto end;
                    182:                        case 0:
                    183:                                setsid();
                    184: 
                    185:                                ret = open("/dev/null", O_RDWR);
                    186:                                if (ret != -1) {
                    187:                                        dup2(ret, STDIN_FILENO);
                    188:                                        dup2(ret, STDOUT_FILENO);
                    189:                                        dup2(ret, STDERR_FILENO);
                    190:                                        close(ret);
                    191:                                }
                    192:                                ioDEBUG(2, "Welcome MQTT service into shadow land!");
                    193:                                break;
                    194:                        default:
                    195:                                ioDEBUG(2, "MQTT service go to shadow land ...");
                    196:                                sleep(1);
                    197:                                ret = 0;
                    198:                                goto end;
                    199:                }
                    200:        else
1.3       misho     201:                ioDEBUG(1, "Start service in batch mode ...");
1.2       misho     202: 
                    203:        memset(&sa, 0, sizeof sa);
                    204:        sigemptyset(&sa.sa_mask);
                    205:        sa.sa_handler = sigHand;
                    206:        sigaction(SIGHUP, &sa, NULL);
1.3       misho     207:        sigaction(SIGINT, &sa, NULL);
1.2       misho     208:        sigaction(SIGTERM, &sa, NULL);
                    209:        sigaction(SIGCHLD, &sa, NULL);
                    210:        sigaction(SIGPIPE, &sa, NULL);
1.3       misho     211:        ioDEBUG(2, "Service is ready for starting engine ...");
1.2       misho     212: 
                    213:        if ((sock = srv_Socket(&cfg)) == -1) {
                    214:                ret = 4;
                    215:                goto end;
                    216:        }
                    217: 
1.3       misho     218:        cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
                    219:        pass = getpwnam(AIT_GET_STR(&v));
                    220:        AIT_FREE_VAL(&v);
1.2       misho     221:        if (pass) {
                    222:                setgid(pass->pw_gid);
                    223:                setuid(pass->pw_uid);
                    224:                ioDEBUG(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
                    225:        }
                    226: 
                    227:        if (!(root = schedBegin())) {
1.3       misho     228:                ioLIBERR(sched);
1.2       misho     229:                ret = 6;
                    230:                goto end;
                    231:        }
                    232: 
1.3       misho     233:        /* go catch the cat ... */
1.2       misho     234:        Run(sock);
                    235: 
                    236:        schedEnd(&root);
1.3       misho     237: end:   /* free all resources */
1.2       misho     238:        srv_Close(sock);
                    239:        call.CloseLOG(logg);
                    240:        call.ClosePUB(pub);
                    241:        call.CloseACC(acc);
1.3       misho     242:        mqttUnloadRTLM(acc);
                    243:        mqttUnloadRTLM(pub);
                    244:        mqttUnloadRTLM(logg);
                    245:        cfgUnloadConfig(&cfg);
1.2       misho     246:        closelog();
                    247:        return ret;
1.1       misho     248: }

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