Ejemplo n.º 1
0
    PacketHandler(const std::string& pcap_name)
    : pcap_writer(NULL),
      pcap_filename(pcap_name)
    {
		pcap = pcap_open_dead(DLT_EN10MB, MAX_CAPLEN);
		open_logs();
    }
Ejemplo n.º 2
0
/* 	Main Routine				*/
int main_construct(FILE *log, dictionary *conf, int listenfd)
{
        int connfd = 0;
        struct sockaddr_in serv_addr;
        char sendBuff[140];

        //Start main loop
        while(1)
        {

                //If data is recieved - run mysql_status and write result to socket
                connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);

                if (connfd != -1)
                {
                        char *message = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: text/plain\r\nConnection: close\r\nContent-Length: 44\r\n\r\nMariaDB Cluster Node is not synced.\r\n";
                        char *status = mysql_status(conf, log);

                        if (strncmp("4", status, 1024) == 0)
                        {
                                message = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\nContent-Length: 40\r\n\r\nMariaDB Cluster Node is synced.\r\n";
                        }
                        snprintf(sendBuff, sizeof(sendBuff), "%s\r\n", message);
                        write(connfd, sendBuff, strlen(sendBuff));
                }

		//Signal Actions
		if (sig_flag == 4)
		{
			put_log(log, "INFO - Shutting down HAwk...");
        		put_log(log, "INFO - Releasing Socket");
        		close(listenfd);
			//Freeing configuration dictionary
			iniparser_freedict(conf);
        		put_log(log, "INFO - Closing Log Files");
			fflush(log);
			fclose(log);
			close(connfd);
			break;
		}
		if (sig_flag == 6)
		{
			put_log(log, "INFO - Received HUP. Reloading...");
			fflush(log);
			fclose(log);
			close(connfd);
			log = open_logs();
			put_log(log, "INFO - Successfully reloaded logs");
			//free(conf);
			iniparser_freedict(conf);
			conf = load_conf();
			sig_flag = 0;	
		}
		close(connfd);
                sleep(1);
        }
	return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    int c;                      
    int server_s;                   

    
    c = umask(~0600);
    if (c == -1) {
        perror("umask");
        exit(1);
    }

    devnullfd = open("/dev/null", 0);

    
    if (devnullfd == -1) {
        DIE("can't open /dev/null");
    }

    if (dup2(devnullfd, STDIN_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDIN_FILENO");
    }

    if (dup2(devnullfd, STDOUT_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDOUT_FILENO");
    }

    
    (void) time(&current_time);

    while ((c = getopt(argc, argv, "c:r:d")) != -1) {
        switch (c) {
        case 'c':
            if (server_root)
                free(server_root);
            server_root = strdup(optarg);
            if (!server_root) {
                perror("strdup (for server_root)");
                exit(1);
            }
            break;
        case 'r':
            if (chdir(optarg) == -1) {
                log_error_time();
                perror("chdir (to chroot)");
                exit(1);
            }
            if (chroot(optarg) == -1) {
                log_error_time();
                perror("chroot");
                exit(1);
            }
            if (chdir("/") == -1) {
                log_error_time();
                perror("chdir (after chroot)");
                exit(1);
            }
            break;
        case 'd':
            do_fork = 0;
            break;
        default:
            fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]);
            exit(1);
        }
    }

    fixup_server_root();
    read_config_files();
    open_logs();
    server_s = create_server_socket();
    boa_event_base = create_event_base(server_s);
    init_signals();
    drop_privs();
    create_common_env();
    build_needs_escape();

    if (max_connections < 1) {
        struct rlimit rl;

        
        c = getrlimit(RLIMIT_NOFILE, &rl);
        if (c < 0) {
            perror("getrlimit");
            exit(1);
        }
        max_connections = rl.rlim_cur;
    }

    
    if (do_fork) {
        switch(fork()) {
        case -1:
            
            perror("fork");
            exit(1);
            break;
        case 0:
            
            break;
        default:
            
            exit(0);
            break;
        }
    }

    
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;
    select_loop(server_s);
    return 0;
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
    int c;                      /* command line arg */
    int server_s;                   /* boa socket */

    /* set umask to u+rw, u-x, go-rwx */
    c = umask(~0600);
    if (c == -1) {
        perror("umask");
        exit(1);
    }

    devnullfd = open("/dev/null", 0);

    /* make STDIN and STDOUT point to /dev/null */
    if (devnullfd == -1) {
        DIE("can't open /dev/null");
    }
#if 0
    if (dup2(devnullfd, STDIN_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDIN_FILENO");
    }

    if (dup2(devnullfd, STDOUT_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDOUT_FILENO");
    }
#endif

    /* but first, update timestamp, because log_error_time uses it */
    (void) time(&current_time);

    while ((c = getopt(argc, argv, "c:r:d")) != -1) {
        switch (c) {
        case 'c':
            if (server_root)
                free(server_root);
            server_root = strdup(optarg);
            if (!server_root) {
                perror("strdup (for server_root)");
                exit(1);
            }
            break;
        case 'r':
            if (chdir(optarg) == -1) {
                log_error_time();
                perror("chdir (to chroot)");
                exit(1);
            }
            if (chroot(optarg) == -1) {
                log_error_time();
                perror("chroot");
                exit(1);
            }
            if (chdir("/") == -1) {
                log_error_time();
                perror("chdir (after chroot)");
                exit(1);
            }
            break;
        case 'd':
            do_fork = 1;
            break;
        default:
            fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d] [-b] [-q bitrate]\n", argv[0]);
            exit(1);
        }
    }

#ifdef DAVINCI_IPCAM
	if(ApproDrvInit(BOA_MSG_TYPE) < 0){
		exit(1);
	}
	//printf("func_get_mem Begin\n");
    if (func_get_mem(NULL)) {
        ApproDrvExit();
        exit(1);
    }
	//printf("SysDrvInit Begin\n");

	if(SysDrvInit(SYS_BOA_MSG) < 0){
        ApproDrvExit();
        exit(1);
	}
	//printf("InitFileMsgDrv Begin\n");

    if (InitFileMsgDrv(FILE_MSG_KEY, FILE_BOA_MSG) < 0) {
	ApproDrvExit();
	SysDrvExit();
	exit(1);
    }
	//printf("hash_table_init Begin\n");

    if (hash_table_init() < 0) {
        exit(1);
    }
	//printf("arg_hash_table_init Begin\n");

    if(arg_hash_table_init() < 0) {
        exit(1);
    }
	//printf("uri_hash_table_init Begin\n");

    if(uri_hash_table_init() < 0) {
        exit(1);
    }
	
#endif

#ifdef PSIA_MEDIA
	//printf("psia_uri_hash_table_init Begin\n");

    if(psia_uri_hash_table_init() < 0) {
        exit(1);
    }
#endif
	//printf("fixup_server_root Begin\n");

    fixup_server_root();
	printf("read_config_files Begin\n");
    read_config_files();
#ifdef DAVINCI_IPCAM
    {
    //printf("ControlSystemData Begin\n");
        unsigned short value;
		#if 1
        if (ControlSystemData(SFIELD_GET_HTTPPORT, (void *)&value, sizeof(value)) >= 0)
            server_port = value;
		#endif
		
		dbg("server_port=%d\n", server_port);
    }
#endif
    open_logs();
    server_s = create_server_socket();
    init_signals();
    drop_privs();
    create_common_env();
    build_needs_escape();
	

    if (max_connections < 1) {
        struct rlimit rl;

        /* has not been set explicitly */
        c = getrlimit(RLIMIT_NOFILE, &rl);
        if (c < 0) {
            perror("getrlimit");
            exit(1);
        }
        max_connections = rl.rlim_cur;
    }
	

    /* background ourself */
    if (do_fork) {
        switch(fork()) {
        case -1:
            /* error */
            perror("fork");
            exit(1);
            break;
        case 0:
            /* child, success */
            break;
        default:
            /* parent, success */
            exit(0);
            break;
        }
    }
	

	/* Ask system server to start upnp */
	//system("/opt/dvr/boot_proc 2\n");//add by sxh upnp

    /* main loop */
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;
    select_loop(server_s);
    return 0;
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
    int c;                      /* command line arg */
    int server_s;                   /* boa socket */

    /* set umask to u+rw, u-x, go-rwx */
    c = umask(~0600);
    if (c == -1) {
        perror("umask");
        exit(1);
    }

    devnullfd = open("/dev/null", 0);

    /* make STDIN and STDOUT point to /dev/null */
    if (devnullfd == -1) {
        DIE("can't open /dev/null");
    }

    if (dup2(devnullfd, STDIN_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDIN_FILENO");
    }

    if (dup2(devnullfd, STDOUT_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDOUT_FILENO");
    }

    /* but first, update timestamp, because log_error_time uses it */
    (void) time(&current_time);

    while ((c = getopt(argc, argv, "c:r:d")) != -1) {
        switch (c) {
        case 'c':
            if (server_root)
                free(server_root);
            server_root = strdup(optarg);
            if (!server_root) {
                perror("strdup (for server_root)");
                exit(1);
            }
            break;
        case 'r':
            if (chdir(optarg) == -1) {
                log_error_time();
                perror("chdir (to chroot)");
                exit(1);
            }
            if (chroot(optarg) == -1) {
                log_error_time();
                perror("chroot");
                exit(1);
            }
            if (chdir("/") == -1) {
                log_error_time();
                perror("chdir (after chroot)");
                exit(1);
            }
            break;
        case 'd':
            do_fork = 0;
            break;
        default:
            fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]);
            exit(1);
        }
    }

    fixup_server_root();
    read_config_files();
    open_logs();
    server_s = create_server_socket();
    init_signals();
    drop_privs();
    create_common_env();
    build_needs_escape();

    if (max_connections < 1) {
        struct rlimit rl;

        /* has not been set explicitly */
        c = getrlimit(RLIMIT_NOFILE, &rl);
        if (c < 0) {
            perror("getrlimit");
            exit(1);
        }
        max_connections = rl.rlim_cur;
    }

    /* background ourself */
    if (do_fork) {
        switch(fork()) {
        case -1:
            /* error */
            perror("fork");
            exit(1);
            break;
        case 0:
            /* child, success */
            break;
        default:
            /* parent, success */
            exit(0);
            break;
        }
    }

    /* main loop */
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;
    select_loop(server_s);
    return 0;
}
Ejemplo n.º 6
0
int main(void)
{
        pid_t pid, sid;
        
        //Fork off the parent process
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        //If we got a good PID, then we can exit the parent process
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        //Change the file mode mask
        umask(0);       

	//Load Configuration File        
        dictionary *conf = load_conf();

        //Opening Log
	FILE *log = open_logs();

	//Query for UID/GID
	uid_t id = getid_byName(get_config(conf, "hawk:daemon_user"));

	//Set UID
	if (setuid(id) != 0)
	{
		put_log(log, "FATAL - Could not set UID for daemon");
		exit(1);
	}
	
        //Create a new SID for the child process 
        sid = setsid();
        if (sid < 0) {
                put_log(log, "FATAL - Session ID was not set properly");
                exit(EXIT_FAILURE);
        }

	//Write PID file
	if(strcmp(get_config(conf, "hawk:pid_path"),"") > 0)
	{
		FILE *pidfile;
		pidfile = fopen(get_config(conf, "hawk:pid_path"), "w");
		fprintf(pidfile, "%d", getpid());
		fflush(pidfile);
	}
        
	//Initialize Socket
	int listenfd = socket_init(conf);

        //Close out the standard file descriptors
        fflush(stdin);
	fflush(stdout);
	fflush(stderr);
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

	signal(SIGHUP, signal_handler);
	signal(SIGTERM, signal_handler);

        //Begin main routine
	put_log(log, "INFO - Starting HAwk...");
        main_construct(log, conf, listenfd);	
	return 0;
}
Ejemplo n.º 7
0
int main(int argc, char **argv)
{
    int c;                      /* command line arg */
    int server_s;                   /* boa socket */

    /* set umask to u+rw, u-x, go-rwx */
    c = umask(~0600);
    if (c == -1) {
        perror("umask");
        exit(1);
    }

    devnullfd = open("/dev/null", 0);

    /* make STDIN and STDOUT point to /dev/null */
    if (devnullfd == -1) {
        DIE("can't open /dev/null");
    }

    if (dup2(devnullfd, STDIN_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDIN_FILENO");
    }

    if (dup2(devnullfd, STDOUT_FILENO) == -1) {
        DIE("can't dup2 /dev/null to STDOUT_FILENO");
    }

    /* but first, update timestamp, because log_error_time uses it */
    (void) time(&current_time);

    while ((c = getopt(argc, argv, "c:r:d")) != -1) {
        switch (c) {
        case 'c':
            if (server_root)
                free(server_root);
            server_root = strdup(optarg);
            if (!server_root) {
                perror("strdup (for server_root)");
                exit(1);
            }
            break;
        case 'r':
            if (chdir(optarg) == -1) {
                log_error_time();
                perror("chdir (to chroot)");
                exit(1);
            }
            if (chroot(optarg) == -1) {
                log_error_time();
                perror("chroot");
                exit(1);
            }
            if (chdir("/") == -1) {
                log_error_time();
                perror("chdir (after chroot)");
                exit(1);
            }
            break;
        case 'd':
            do_fork = 0;
            break;
        default:
            fprintf(stderr, "Usage: %s [-c serverroot] [-r chroot] [-d]\n", argv[0]);
            exit(1);
        }
    }

    /* fix it first, see this file */
    fixup_server_root();
    /* parse configuration files, see config.c */
    read_config_files();
    /* log function, see log.c */
    open_logs();
    /* create http server socket, see this file */
    server_s = create_server_socket();
    /* init signal for later use, see signals.c */
    init_signals();
    /* give privs if we can, see this file */
    drop_privs();
    /* setup envrionment varables, see cgi.c */
    create_common_env();
    /* escape handle, see escape.c */
    build_needs_escape();

    /* max connections parse from config files,see config.c & y.tab.c for more detail */
    if (max_connections < 1) {
        struct rlimit rl;

        /* has not been set explicitly */
	/* getrlimit is system call, see man getrlimit for more detail */
        c = getrlimit(RLIMIT_NOFILE, &rl);
        if (c < 0) {
            perror("getrlimit");
            exit(1);
        }
	/* set to max allowed by HOST os. */
        max_connections = rl.rlim_cur;
    }

    /* background ourself */
    if (do_fork) {
        switch(fork()) {
        case -1:
            /* error */
            perror("fork");
            exit(1);
            break;
        case 0:
            /* child, success */
	    /* so,we run in child */
            break;
        default:
            /* parent, success */
	    /* exit in parent */
            exit(0);
            break;
        }
    }

    /* main loop */
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;

    /*start socket and process request, see select.c*/
    select_loop(server_s);
    return 0;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
    int server_s;               /* aspis socket */
    pid_t pid;

    /* set umask to u+rw, u-x, go-rwx */
    /* according to the man page, umask always succeeds */
    umask(077);

    /* but first, update timestamp, because log_error_time uses it */
    (void) time(&current_time);

    /* set timezone right away */
    tzset();

    {
        int devnullfd = -1;
        devnullfd = open("/dev/null", 0);

        /* make STDIN point to /dev/null */
        if (devnullfd == -1) {
            DIE("can't open /dev/null");
        }

        if (dup2(devnullfd, STDIN_FILENO) == -1) {
            DIE("can't dup2 /dev/null to STDIN_FILENO");
        }

        (void) close(devnullfd);
    }

    parse_commandline(argc, argv);
    fixup_server_root();
    read_config_files();
    drop_privs1();
    set_root();
    create_common_env();
    open_logs();
    server_s = create_server_socket();
    init_signals();
    build_needs_escape();

    /* background ourself */
    if (do_fork) {
        pid = fork();
    } else {
        pid = getpid();
    }

    switch (pid) {
    case -1:
        /* error */
        perror("fork/getpid");
        exit(EXIT_FAILURE);
    case 0:
        /* child, success */
        break;
    default:
        /* parent, success */
        if (pid_file != NULL) {
            FILE *PID_FILE = fopen(pid_file, "w");
            if (PID_FILE != NULL) {
                fprintf(PID_FILE, "%d", pid);
                fclose(PID_FILE);
            } else {
                perror("fopen pid file");
            }
        }

        if (do_fork)
            exit(EXIT_SUCCESS);
        break;
    }

    drop_privs2();
    /* main loop */
    timestamp();

    status.requests = 0;
    status.errors = 0;

    start_time = current_time;
    loop(server_s);
    close_logs();
    return 0;
}