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

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 $
1.5     ! misho       6: * $Id: mqttd.c,v 1.4.4.2 2013/07/16 14:34:58 misho Exp $
1.4       misho       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: 
1.5     ! misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.4       misho      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: 
1.3       misho      53: cfg_root_t cfg;
1.2       misho      54: sessions_t Sessions;
                     55: sched_root_task_t *root;
                     56: sqlite3 *acc, *pub;
                     57: FILE *logg;
                     58: extern char compiled[], compiledby[], compilehost[];
                     59: static char szCfgName[MAXPATHLEN];
1.3       misho      60: volatile intptr_t Kill;
1.2       misho      61: 
                     62: 
                     63: static void
                     64: Usage(void)
                     65: {
                     66:        printf( " -= MQTT Broker =- MQTT Service from ELWIX\n"
                     67:                "=== %s@%s === Compiled: %s ===\n\n"
                     68:                "\t-c <config>\tService config\n"
                     69:                "\t-b\t\tBatch mode\n"
                     70:                "\t-v\t\tVerbose (more -vvv, more verbose)\n"
                     71:                "\t-h\t\tHelp! This screen\n\n", 
                     72:                compiledby, compilehost, compiled);
                     73: }
                     74: 
                     75: static void
                     76: sigHand(int sig)
                     77: {
                     78:        int stat;
                     79: 
                     80:        switch (sig) {
                     81:                case SIGHUP:
1.3       misho      82:                        cfgUnloadConfig(&cfg);
                     83:                        if (!cfgLoadConfig(szCfgName, &cfg)) {
1.5     ! misho      84:                                EVERBOSE(1, "Config reload OK!");
1.2       misho      85:                                break;
                     86:                        }
                     87: 
1.5     ! misho      88:                        ELIBERR(cfg);
1.3       misho      89:                case SIGINT:
1.2       misho      90:                case SIGTERM:
1.5     ! misho      91:                        EVERBOSE(1, "Terminate MQTT service in progress");
1.2       misho      92:                        Kill++;
                     93:                        break;
                     94:                case SIGCHLD:
                     95:                        while (waitpid(-1, &stat, WNOHANG) > 0);
                     96:                        break;
                     97:                case SIGPIPE:
                     98:                        break;
                     99:        }
                    100: }
1.1       misho     101: 
                    102: 
                    103: int
                    104: main(int argc, char **argv)
                    105: {
1.3       misho     106:        char ch, batch = 0;
1.2       misho     107:        register int i;
                    108:        int sock = -1, ret = 0;
                    109:        struct passwd *pass;
                    110:        struct sigaction sa;
1.3       misho     111:        ait_val_t v;
1.2       misho     112: 
                    113:        TAILQ_INIT(&Sessions);
                    114: 
                    115:        strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
                    116:        while ((ch = getopt(argc, argv, "hvbc:")) != -1)
                    117:                switch (ch) {
                    118:                        case 'c':
                    119:                                strlcpy(szCfgName, optarg, sizeof szCfgName);
                    120:                                break;
                    121:                        case 'b':
                    122:                                batch++;
                    123:                                break;
                    124:                        case 'v':
1.5     ! misho     125:                                e_incVerbose;
        !           126:                                EVERBS(2) elwix_Debug |= ELWIX_DEBUG_TRACE;
1.2       misho     127:                                break;
                    128:                        case 'h':
                    129:                        default:
                    130:                                Usage();
                    131:                                return 1;
                    132:                }
                    133:        argc -= optind;
                    134:        argv += optind;
                    135: 
1.3       misho     136:        if (cfgLoadConfig(szCfgName, &cfg)) {
1.2       misho     137:                printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
                    138:                return 1;
                    139:        }
                    140:        openlog("mqttd", LOG_PID | LOG_CONS, LOG_DAEMON);
1.3       misho     141:        /* load 3 plugins */
1.2       misho     142:        for (i = 0; i < 3; i++)
                    143:                if (!mqttLoadRTLM(&cfg, i)) {
                    144:                        printf("Error:: Can't load RTL module\n");
1.3       misho     145:                        mqttUnloadRTLM(acc);
                    146:                        mqttUnloadRTLM(pub);
                    147:                        mqttUnloadRTLM(logg);
                    148:                        cfgUnloadConfig(&cfg);
1.2       misho     149:                        closelog();
                    150:                        return 2;
                    151:                }
                    152:        acc = call.OpenACC(&cfg);
                    153:        if (!acc) {
                    154:                ret = 3;
                    155:                goto end;
                    156:        }
                    157:        pub = call.OpenPUB(&cfg);
                    158:        if (!pub) {
                    159:                ret = 3;
                    160:                goto end;
                    161:        }
                    162:        logg = call.OpenLOG(&cfg);
                    163:        if (!logg) {
                    164:                ret = 3;
                    165:                goto end;
                    166:        }
                    167: 
1.3       misho     168: 
1.2       misho     169:        if (mqttMkDir(&cfg)) {
                    170:                printf("Error:: in statedir #%d - %s\n", errno, strerror(errno));
                    171:                ret = 3;
                    172:                goto end;
                    173:        }
                    174: 
                    175:        if (!batch)
                    176:                switch (fork()) {
                    177:                        case -1:
                    178:                                printf("Error:: in fork() #%d - %s\n", errno, strerror(errno));
                    179:                                ret = 5;
                    180:                                goto end;
                    181:                        case 0:
                    182:                                setsid();
                    183: 
                    184:                                ret = open("/dev/null", O_RDWR);
                    185:                                if (ret != -1) {
                    186:                                        dup2(ret, STDIN_FILENO);
                    187:                                        dup2(ret, STDOUT_FILENO);
                    188:                                        dup2(ret, STDERR_FILENO);
                    189:                                        close(ret);
                    190:                                }
1.5     ! misho     191:                                EVERBOSE(2, "Welcome MQTT service into shadow land!");
1.2       misho     192:                                break;
                    193:                        default:
1.5     ! misho     194:                                EVERBOSE(2, "MQTT service go to shadow land ...");
1.2       misho     195:                                sleep(1);
                    196:                                ret = 0;
                    197:                                goto end;
                    198:                }
                    199:        else
1.5     ! misho     200:                EVERBOSE(1, "Start service in batch mode ...");
1.2       misho     201: 
                    202:        memset(&sa, 0, sizeof sa);
                    203:        sigemptyset(&sa.sa_mask);
                    204:        sa.sa_handler = sigHand;
                    205:        sigaction(SIGHUP, &sa, NULL);
1.3       misho     206:        sigaction(SIGINT, &sa, NULL);
1.2       misho     207:        sigaction(SIGTERM, &sa, NULL);
                    208:        sigaction(SIGCHLD, &sa, NULL);
                    209:        sigaction(SIGPIPE, &sa, NULL);
1.5     ! misho     210:        EVERBOSE(2, "Service is ready for starting engine ...");
1.2       misho     211: 
                    212:        if ((sock = srv_Socket(&cfg)) == -1) {
                    213:                ret = 4;
                    214:                goto end;
                    215:        }
                    216: 
1.3       misho     217:        cfg_loadAttribute(&cfg, "mqttd", "user", &v, MQTT_USER);
                    218:        pass = getpwnam(AIT_GET_STR(&v));
                    219:        AIT_FREE_VAL(&v);
1.2       misho     220:        if (pass) {
                    221:                setgid(pass->pw_gid);
                    222:                setuid(pass->pw_uid);
1.5     ! misho     223:                EVERBOSE(2, "Try to change group #%d and user #%d", pass->pw_gid, pass->pw_uid);
1.2       misho     224:        }
                    225: 
                    226:        if (!(root = schedBegin())) {
1.5     ! misho     227:                ELIBERR(sched);
1.2       misho     228:                ret = 6;
                    229:                goto end;
                    230:        }
                    231: 
1.3       misho     232:        /* go catch the cat ... */
1.2       misho     233:        Run(sock);
                    234: 
                    235:        schedEnd(&root);
1.3       misho     236: end:   /* free all resources */
1.2       misho     237:        srv_Close(sock);
                    238:        call.CloseLOG(logg);
                    239:        call.ClosePUB(pub);
                    240:        call.CloseACC(acc);
1.3       misho     241:        mqttUnloadRTLM(acc);
                    242:        mqttUnloadRTLM(pub);
                    243:        mqttUnloadRTLM(logg);
                    244:        cfgUnloadConfig(&cfg);
1.2       misho     245:        closelog();
                    246:        return ret;
1.1       misho     247: }

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