src/mn-masterd/main.c
const char *
master_init_cb(void)
{
    FILE *fp;
    char *s;
    int o = 1, sock;
    openlog("mn-masterd", LOG_PID, LOG_DAEMON);

    if (!srv.auth.num_slaves) {
        syslog(LOG_WARNING, "No slave login declared, using default:default!");
        srv.auth.num_slaves = 1;
        if (!(srv.auth.slaves = calloc(1, sizeof(slave_t*)))
                || !(srv.auth.slaves[0] = calloc(1, sizeof(slave_t))))
            return "out of mem";
        slave_t *sl = srv.auth.slaves[0];
        if (!(sl->name = strdup("default")))
            return "out of mem";
        if (!(sl->password = strdup("default")))
            return "out of mem";
    }
    if (!srv.auth.num_clients) {
        syslog(LOG_WARNING, "No client login declared, using default:default!");
        srv.auth.num_clients = 1;
        if (!(srv.auth.clients = calloc(1, sizeof(client_t*)))
                || !(srv.auth.clients[0] = calloc(1, sizeof(client_t))))
            return "out of mem";
        client_t *cl = srv.auth.clients[0];
        if (!(cl->name = strdup("default")))
            return "out of mem";
        if (!(cl->password = strdup("default")))
            return "out of mem";
    }

    if (!opt_vals.config_file)
        return "no configuration file";

    if ((s = strchr(opt_vals.listen, ':'))) {
        srv.addr.sin_port = htons(atoi(s+1));
        *s = '\0';
    } else
        srv.addr.sin_port = htons(NOL_MASTER_DEFAULT_PORT);
    srv.addr.sin_addr.s_addr = inet_addr(opt_vals.listen);
    srv.addr.sin_family = AF_INET;

    /* load the configuration file */
    if (!(fp = fopen(opt_vals.config_file, "rb")))
        return "could not open configuration file";

    /* calculate the file length */
    fseek(fp, 0, SEEK_END);
    srv.config_sz = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    if (!(srv.config_buf = malloc(srv.config_sz))) {
        fclose(fp);
        return "out of mem";
    }

    if ((size_t)srv.config_sz != fread(srv.config_buf, 1, srv.config_sz, fp)) {
        fclose(fp);
        return "read error";
    }

    fclose(fp);
    fp = 0;

    if (nol_m_mysql_connect() != 0)
        return "could not connect to mysql server";
    if (nol_m_mysql_setup() != 0)
        return "settings up mysql tables failed";
    if (nol_m_reconfigure() != 0)
        return "settings up filetype tables failed";

    sock = socket(PF_INET, SOCK_STREAM, 0);
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &o, sizeof(o));

    if (bind(sock, (struct sockaddr*)&srv.addr, sizeof srv.addr) == -1) {
        syslog(LOG_ERR, "could not bind to %s:%hd",
                inet_ntoa(srv.addr.sin_addr),
                ntohs(srv.addr.sin_port));
        return "bind() failed";
    }
    if (listen(sock, 1024) == -1) {
        syslog(LOG_ERR, "could not listen on %s:%hd",
                inet_ntoa(srv.addr.sin_addr),
                ntohs(srv.addr.sin_port));
        return "listen() failed";
    }

    srv.listen_sock = sock;
    return load_hooks();
}
/* initialise the server, return 0 on success or a pointer
 * to a static error buffer on failure
 *
 * this will be run by the forked process, and called from
 * nol_daemon_launch()
 * */
initialise the server, return 0 on success or a pointer
to a static error buffer on failure

this will be run by the forked process, and called from
nol_daemon_launch()
initialise the server return 0 on success or a pointer to a static error buffer on failure this will be run by the forked process and called from nol_daemon_launch ()
const char *
master_init_cb(void)
{
    FILE *fp;
    char *s;
    int o = 1, sock;
    openlog("mn-masterd", LOG_PID, LOG_DAEMON);
    if (!srv.auth.num_slaves) {
        syslog(LOG_WARNING, "No slave login declared, using default:default!");
        srv.auth.num_slaves = 1;
        if (!(srv.auth.slaves = calloc(1, sizeof(slave_t*)))
                || !(srv.auth.slaves[0] = calloc(1, sizeof(slave_t))))
            return "out of mem";
        slave_t *sl = srv.auth.slaves[0];
        if (!(sl->name = strdup("default")))
            return "out of mem";
        if (!(sl->password = strdup("default")))
            return "out of mem";
    }
    if (!srv.auth.num_clients) {
        syslog(LOG_WARNING, "No client login declared, using default:default!");
        srv.auth.num_clients = 1;
        if (!(srv.auth.clients = calloc(1, sizeof(client_t*)))
                || !(srv.auth.clients[0] = calloc(1, sizeof(client_t))))
            return "out of mem";
        client_t *cl = srv.auth.clients[0];
        if (!(cl->name = strdup("default")))
            return "out of mem";
        if (!(cl->password = strdup("default")))
            return "out of mem";
    }
    if (!opt_vals.config_file)
        return "no configuration file";
    if ((s = strchr(opt_vals.listen, ':'))) {
        srv.addr.sin_port = htons(atoi(s+1));
        *s = '\0';
    } else
        srv.addr.sin_port = htons(NOL_MASTER_DEFAULT_PORT);
    srv.addr.sin_addr.s_addr = inet_addr(opt_vals.listen);
    srv.addr.sin_family = AF_INET;
    if (!(fp = fopen(opt_vals.config_file, "rb")))
        return "could not open configuration file";
    fseek(fp, 0, SEEK_END);
    srv.config_sz = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (!(srv.config_buf = malloc(srv.config_sz))) {
        fclose(fp);
        return "out of mem";
    }
    if ((size_t)srv.config_sz != fread(srv.config_buf, 1, srv.config_sz, fp)) {
        fclose(fp);
        return "read error";
    }
    fclose(fp);
    fp = 0;
    if (nol_m_mysql_connect() != 0)
        return "could not connect to mysql server";
    if (nol_m_mysql_setup() != 0)
        return "settings up mysql tables failed";
    if (nol_m_reconfigure() != 0)
        return "settings up filetype tables failed";
    sock = socket(PF_INET, SOCK_STREAM, 0);
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &o, sizeof(o));
    if (bind(sock, (struct sockaddr*)&srv.addr, sizeof srv.addr) == -1) {
        syslog(LOG_ERR, "could not bind to %s:%hd",
                inet_ntoa(srv.addr.sin_addr),
                ntohs(srv.addr.sin_port));
        return "bind() failed";
    }
    if (listen(sock, 1024) == -1) {
        syslog(LOG_ERR, "could not listen on %s:%hd",
                inet_ntoa(srv.addr.sin_addr),
                ntohs(srv.addr.sin_port));
        return "listen() failed";
    }
    srv.listen_sock = sock;
    return load_hooks();
}
const char * master_init_cb ( void ) { FILE * fp ; char * s ; int o = 1 , sock ; openlog ( " " , LOG_PID , LOG_DAEMON ) ; if ( ! srv . auth . num_slaves ) { syslog ( LOG_WARNING , " " ) ; srv . auth . num_slaves = 1 ; if ( ! ( srv . auth . slaves = calloc ( 1 , sizeof ( slave_t * ) ) ) || ! ( srv . auth . slaves [ 0 ] = calloc ( 1 , sizeof ( slave_t ) ) ) ) return " " ; slave_t * sl = srv . auth . slaves [ 0 ] ; if ( ! ( sl -> name = strdup ( " " ) ) ) return " " ; if ( ! ( sl -> password = strdup ( " " ) ) ) return " " ; } if ( ! srv . auth . num_clients ) { syslog ( LOG_WARNING , " " ) ; srv . auth . num_clients = 1 ; if ( ! ( srv . auth . clients = calloc ( 1 , sizeof ( client_t * ) ) ) || ! ( srv . auth . clients [ 0 ] = calloc ( 1 , sizeof ( client_t ) ) ) ) return " " ; client_t * cl = srv . auth . clients [ 0 ] ; if ( ! ( cl -> name = strdup ( " " ) ) ) return " " ; if ( ! ( cl -> password = strdup ( " " ) ) ) return " " ; } if ( ! opt_vals . config_file ) return " " ; if ( ( s = strchr ( opt_vals . listen , ' ' ) ) ) { srv . addr . sin_port = htons ( atoi ( s + 1 ) ) ; * s = ' \0 ' ; } else srv . addr . sin_port = htons ( NOL_MASTER_DEFAULT_PORT ) ; srv . addr . sin_addr . s_addr = inet_addr ( opt_vals . listen ) ; srv . addr . sin_family = AF_INET ; if ( ! ( fp = fopen ( opt_vals . config_file , " " ) ) ) return " " ; fseek ( fp , 0 , SEEK_END ) ; srv . config_sz = ftell ( fp ) ; fseek ( fp , 0 , SEEK_SET ) ; if ( ! ( srv . config_buf = malloc ( srv . config_sz ) ) ) { fclose ( fp ) ; return " " ; } if ( ( size_t ) srv . config_sz != fread ( srv . config_buf , 1 , srv . config_sz , fp ) ) { fclose ( fp ) ; return " " ; } fclose ( fp ) ; fp = 0 ; if ( nol_m_mysql_connect ( ) != 0 ) return " " ; if ( nol_m_mysql_setup ( ) != 0 ) return " " ; if ( nol_m_reconfigure ( ) != 0 ) return " " ; sock = socket ( PF_INET , SOCK_STREAM , 0 ) ; setsockopt ( sock , SOL_SOCKET , SO_REUSEADDR , & o , sizeof ( o ) ) ; if ( bind ( sock , ( struct sockaddr * ) & srv . addr , sizeof srv . addr ) == -1 ) { syslog ( LOG_ERR , " " , inet_ntoa ( srv . addr . sin_addr ) , ntohs ( srv . addr . sin_port ) ) ; return " " ; } if ( listen ( sock , 1024 ) == -1 ) { syslog ( LOG_ERR , " " , inet_ntoa ( srv . addr . sin_addr ) , ntohs ( srv . addr . sin_port ) ) ; return " " ; } srv . listen_sock = sock ; return load_hooks ( ) ; }
initialise the server, return 0 on success or a pointer
 to a static error buffer on failure
initialise the server return 0 on success or a pointer to a static error buffer on failure
/* load the configuration file */ /* calculate the file length */