Exemple #1
0
int main (int argc, char* argv[]) {
    int f[2000];
    oram_args_t *args = malloc(sizeof(oram_args_t));
    args_parse(args, argc, argv);
    signal(SIGINT, sig_handler);
    signal(SIGTERM, sig_handler);
    if (args->mode == ORAM_MODE_SERVER) {
        if (args->daemon == ORAM_DAEMON_STOP) {
            if (daemon_stop(args) != 0) {
                errf("cannot stop");
                return -1;
            }
            return 0;
        }
        else if (args->daemon == ORAM_DAEMON_RESTART) {
            if (daemon_stop(args) != 0) {
                errf("cannot stop");
                return -1;
            }
            if (daemon_start(args) != 0) {
                errf("cannot start");
                return -1;
            }
        }
        else if (args->daemon == ORAM_DAEMON_START) {
            if (daemon_start(args) != 0) {
                errf("cannot start");
                return -1;
            }
        }
        server_run(args, &sv_ctx);
    }
    else {
        client_ctx ctx;
        oram_client_args ar;
        ar.verbose = 1;
        if (client_init(&ctx, &ar) < 0)
            return -1;
//        if (client_create(&ctx, 6000, 1) < 0)
//            return -1;
        if (client_load(&ctx, 1) < 0)
            return -1;
        unsigned char data[ORAM_BLOCK_SIZE];
        int m;
        for(m = 0;m < 100;m++) {
            data[0] = m;
            oblivious_access(m, ORAM_ACCESS_WRITE, data, &ctx);
        }
        for(m = 0;m < 100;m++) {
            oblivious_access(m, ORAM_ACCESS_READ, data, &ctx);
            f[m] = data[0];
        }
        for (m = 0;m < 100;m++)
            assert(f[m] == m);
        client_save(&ctx, 0);
    }
    return 0;
}
Exemple #2
0
void	zbx_on_exit()
{
	zabbix_log(LOG_LEVEL_INFORMATION, "zbx_on_exit()");
#if !defined(_WINDOWS)

	int i = 0;

	if (threads != NULL) {
		for (	i = 1;
			i <= CONFIG_CONFSYNCER_FORKS + CONFIG_DATASENDER_FORKS + CONFIG_POLLER_FORKS
				+ CONFIG_TRAPPERD_FORKS + CONFIG_PINGER_FORKS + CONFIG_HOUSEKEEPER_FORKS
				+ CONFIG_UNREACHABLE_POLLER_FORKS + CONFIG_HTTPPOLLER_FORKS
				+ CONFIG_DISCOVERER_FORKS + CONFIG_DBSYNCER_FORKS + CONFIG_IPMIPOLLER_FORKS; i ++)
		{
			if (threads[i]) {
				kill(threads[i], SIGTERM);
				threads[i] = (ZBX_THREAD_HANDLE)NULL;
			}
		}
		zbx_free(threads);
	}

#endif /* not _WINDOWS */

#ifdef USE_PID_FILE

	daemon_stop();

#endif /* USE_PID_FILE */

	free_metrics();

	zbx_sleep(2); /* wait for all threads closing */

	DBconnect(ZBX_DB_CONNECT_EXIT);
	free_database_cache();
	DBclose();
/*	zbx_mutex_destroy(&node_sync_access);*/

#ifdef HAVE_OPENIPMI
	free_ipmi_handler();
#endif

	zabbix_close_log();

#ifdef  HAVE_SQLITE3
	php_sem_remove(&sqlite_access);
#endif /* HAVE_SQLITE3 */

	zabbix_log(LOG_LEVEL_INFORMATION, "ZABBIX Proxy stopped. ZABBIX %s (revision %s).",
		ZABBIX_VERSION,
		ZABBIX_REVISION);

	exit(SUCCEED);
}
Exemple #3
0
int main(int argc, char *argv[]) {
	daemon_t *daemon;

#ifdef _WIN32
#define sleep(x) Sleep((x)*1000)

	WORD wVersionRequested;
	WSADATA wsaData;
	int ret;

	wVersionRequested = MAKEWORD(2, 2);

	ret = WSAStartup(wVersionRequested, &wsaData);
	if (ret) {
		/* Couldn't find WinSock DLL */
		return -1;
	}

	if (LOBYTE(wsaData.wVersion) != 2 ||
	    HIBYTE(wsaData.wVersion) != 2) {
		/* Version mismatch, requested version not found */
		return -1;
	}
#endif

	daemon = daemon_init();
	daemon_start(daemon);

	running = 1;
	signal(SIGINT, handle_sigint);
	while (running) {
		sleep(1);
	}

	daemon_stop(daemon);
	daemon_destroy(daemon);

#ifdef _WIN32
	WSACleanup();
#endif

	return 0;
}
Exemple #4
0
int main(int argc, char** argv)
{
    if (argc < 2) {
        std::cerr << "usage: " << argv[0] << " conf_file ..." << std::endl;
        return 0;
    }

    ///创建i_ini_file实例
    if (create_ini_file_instance(&g_p_ini_file)) {
        std::cerr << "create ini_file instance failed" << std::endl;
        return -1;
    }

    if (g_p_ini_file->init(argv[1]) < 0) {
        std::cerr << "ini_file init failed, err: " << g_p_ini_file->get_last_errstr() << std::endl;
        g_p_ini_file->release();
        g_p_ini_file = NULL;
        return -1;
    }

    ///开启精灵模式
    if (daemon_start(argc, argv, g_p_ini_file) < 0) {
        return -1;
    }

    ///初始化日志系统
    if (log_init(g_p_ini_file) < 0) {
        return -1;
    }

    ///读取dll中函数接口
    char path[PATH_MAX] = {0};
    if (g_p_ini_file->read_string("WorkInfo", "dll", path, sizeof(path), NULL)) {
        ERROR_LOG("fail to read dll_path from conf_file");
        return -1;
    }

    g_dll_inst.set_dll_path(path);
    if (g_dll_inst.register_plugin() < 0) {
        ERROR_LOG("fail to register dll");
        return -1;
    }

    ///读取bind_file中的内容
    if (g_p_ini_file->read_string("WorkInfo", "bind_file", path, sizeof(path), NULL)) {
        ERROR_LOG("fail to read bind_file path from conf_file");
        return -1;
    }

    std::list<bind_conf_t> bind_conf_list;
    bind_conf bind_conf_inst(path);
    if (bind_conf_inst.load_bind_conf(bind_conf_list) < 0) {
        ERROR_LOG("fail to read info from bind_file");
        return -1;
    }

    ///初始化注册回调函数
    tcp_io_event tcp_io_event_inst;
    udp_io_event udp_io_event_inst;

    if (g_dll_inst.proc_pkg_from_client)
        tcp_io_event_inst.set_tcp_proc_callback(g_dll_inst.proc_pkg_from_client);
    if (g_dll_inst.get_pkg_len)
        tcp_io_event_inst.set_tcp_pkg_len_callback(g_dll_inst.get_pkg_len);
    if (g_dll_inst.on_client_conn_closed)
        tcp_io_event_inst.set_tcp_close_callback(g_dll_inst.on_client_conn_closed);

    if (g_dll_inst.proc_udp_pkg_from_client)
        udp_io_event_inst.set_udp_proc_callback(g_dll_inst.proc_udp_pkg_from_client);
    if (g_dll_inst.on_client_conn_closed)
        udp_io_event_inst.set_udp_close_conn_callback(g_dll_inst.on_client_conn_closed);

    ///初始化网络监听

    std::list<bind_conf_t>::iterator it = bind_conf_list.begin();
    for (; it != bind_conf_list.end(); ++it) {
            inet_address serv_addr;
#ifdef _IPV6
            if (it->ip_addr == "0:0:0:0:0:0:0:0"
                    || ip->ip_addr == "::"
                    || ip->ip_addr == "::/128") {
                serv_addr.set_port(it->port);
            } else {
                serv_addr.set_ip_addr(it->ip_addr);
                serv_addr.set_port(it->port);
            }
#else
            if (it->ip_addr == "0.0.0.0") {
                serv_addr.set_port(it->port);
            } else {
                serv_addr.set_ip_addr(it->ip_addr);
                serv_addr.set_port(it->port);
            }
#endif

        if (it->type == TYPE_TCP) {
            acceptor<connection<sock_stream>, sock_acceptor>* p_acceptor =
                new (std::nothrow) acceptor<connection<sock_stream>, sock_acceptor>(serv_addr);
            if (!p_acceptor) {
                ERROR_LOG("tcp acceptor instantiate failed");
                return -1;
            }

            p_acceptor->set_new_conn_callback(
                    boost::bind(&tcp_io_event::tcp_on_new_conn, &tcp_io_event_inst, _1));
            p_acceptor->set_on_message_callback(
                    boost::bind(&tcp_io_event::tcp_on_message, &tcp_io_event_inst, _1, _2, _3));
            //p_acceptor->set_on_write_comple_callback(
            //        boost::bind(&tcp_io_event::tcp_on_write_complete, &tcp_io_event_inst, _1));
            p_acceptor->set_on_close_callback(
                    boost::bind(&tcp_io_event::tcp_on_close_conn, &tcp_io_event_inst, _1));

            if (p_acceptor->start() < 0) {
                return -1;
            }
            g_tcp_acceptor_list.push_back(p_acceptor);

        } else {
            acceptor<connection<sock_dgram>, sock_dgram>* p_acceptor =
                new (std::nothrow) acceptor<connection<sock_dgram>, sock_dgram>(serv_addr);
            if (!p_acceptor) {
                ERROR_LOG("udp acceptor instantiate failed");
                return -1;
            }

            p_acceptor->set_new_conn_callback(
                    boost::bind(&udp_io_event::udp_on_new_conn, &udp_io_event_inst, _1));
            p_acceptor->set_on_message_callback(
                    boost::bind(&udp_io_event::udp_on_message, &udp_io_event_inst, _1, _2, _3));
            //p_acceptor->set_on_write_comple_callback(
            //        boost::bind(&udp_io_event::udp_on_write_comple, &udp_io_event_inst, _1));
            p_acceptor->set_on_close_callback(
                    boost::bind(&udp_io_event::udp_on_close_conn, &udp_io_event_inst, _1));

            if (p_acceptor->start() < 0) {
                return -1;
            }

            g_udp_acceptor_list.push_back(p_acceptor);
        }
    }


    ///创建socketpair
    g_work_num = g_p_ini_file->read_int("WorkInfo", "worker_num", 1);
    for (int i = 0; i < g_work_num; i++) {
        connections_pool<connection<vpipe_sockpair> >* p_pool =
            connections_pool<connection<vpipe_sockpair> >::instance();
        assert(p_pool);
        connection<vpipe_sockpair>* p_conn = p_pool->get_new_connection();
        assert(p_conn);
        if ((p_conn->get_ipc_conn())->open() < 0) {
            return -1;
        }

        worker_proc_t tmp;
        tmp.id = i;
        tmp.worker_pid = 0;
        tmp.p_sockpair = p_conn;

        g_worker_proc_vec.push_back(tmp);
    }

    if (g_dll_inst.init_service
            && g_dll_inst.init_service(g_saved_argc, g_saved_argv, PROC_MAIN) != 0) {
        ERROR_LOG("main proc init_service failed");
        return -1;
    }

    daemon_set_title("%s-[%s]", g_prog_name, "master");

    ///fork工作进程
    for (int i = 0; i < g_work_num; i++) {
        pid_t pid = fork();
        switch (pid) {
            case -1:
                ERROR_LOG("fork failed, err: %s", strerror(errno));
                return -1;
            case 0:
                ///子进程
                worker_proc_loop(i);
            default:
                ///master进程
                g_worker_proc_vec[i].worker_pid = pid;
                break;
        }
    }

    ///设置work进程cpu亲和性
    for (int i = 0; i < g_work_num; i++) {
        if (set_cpu_affinity(g_work_num,
                             g_worker_proc_vec[i].worker_pid,
                             g_worker_proc_vec[i].id) < 0) {
            DEBUG_LOG("set_cpu_affinity failed, err: %s", strerror(errno));
        }
    }

    master_proc_loop();
    daemon_stop();

    if (g_p_ini_file) {
        g_p_ini_file->uninit();
        g_p_ini_file->release();
        g_p_ini_file = NULL;
    }

    return 0;
}
Exemple #5
0
int main(int argc, char* argv[])
{
	pid_t pid;
	
	parse_args(argc, argv);

	if (config_init(argv[1]) == -1) {
		BOOT_LOG(-1, "Failed to Parse File '%s'", argv[1]);
	}

	if (config_init("common.conf") == -1) {
			BOOT_LOG(-1, "Failed to Parse File '%s'", argv[1]);
	}

	daemon_start(argc, argv);
	load_bind_file();
	log_init_ex( config_get_strval("log_dir"),
				config_get_intval("log_level", log_lvl_trace),
				config_get_intval("log_size", 1<<30),
				config_get_intval("max_log_files", 0),
				config_get_strval("log_prefix") ,
				config_get_intval("log_save_next_file_interval_min", 0) );

    //Èç¹ûʹÓÃÔà´Ê¼ì²âÂß¼­£¬Ôò¿ªÆô¸¸½ø³ÌµÄÔà´Ê¸üÐÂÂß¼­.
    if (config_get_intval("tm_dirty_use_dirty_logical", 1) == 1) {
        if (tm_dirty_daemon( config_get_strval("tm_dirty_local_dirty_file_path"),
                    config_get_strval("tm_dirty_server_addr"),
                    config_get_intval("tm_dirty_update_cycle", 600),
                    NULL) == -1) {
            BOOT_LOG(-1, "FAILED TO RUN TM_DIRTY_DAEMON");
        }   
    }   

	net_init(MAXFDS, MAXFDS);

	bind_config_t* bc = get_bind_conf();
//--------------------------------
	net_start(config_get_strval("online_ip"), config_get_intval("online_port", 443), bc->configs);
//--------------------------------

//	bind_config_elem_t* bc_elem;
	int i = 0;
	for ( ; i != bc->bind_num; ++i ) {
		bind_config_elem_t* bc_elem = &(bc->configs[i]);
		shmq_create(bc_elem);

		if ( (pid = fork ()) < 0 ) {
			BOOT_LOG(-1, "fork child process");
		} else if (pid > 0) { //parent process
			close_shmq_pipe(bc, i, 0);
			do_add_conn(bc_elem->sendq.pipe_handles[0], PIPE_TYPE_FD, 0, bc_elem);
			//net_start(bc_elem->bind_ip, bc_elem->bind_port, bc_elem);
		} else { //child process
			run_worker_process(bc, i, i + 1);
		}
	}

	while (!stop) {
		net_loop(-1, PAGESIZE, 1);
	}

	net_exit();
	shmq_destroy(0, bc->bind_num);
	daemon_stop();

	return 0;
}
Exemple #6
0
int main( int argc, char* argv[ ] ) {
    int ret = EXIT_SUCCESS;
    int make_daemon = 1, daemon_flags = 0;
    bool force_stop = false;

    ///////////////////////////////////////////////
    // parse command line options
    int i;
    for ( i = 1; i < argc; i++ ) {
        if ( argv[i][0] != '-' ) break;
        if ( strcmp( argv[i], "--no-daemon" ) == 0 )
            make_daemon = 0;
        else if ( strcmp( argv[i], "--force" ) == 0 ) {
            daemon_flags |= D_NOLOCKCHECK;
            force_stop = true;
        } else if ( strcmp( argv[i], "--no-lockfile" ) == 0 ) {
            daemon_flags |= D_NOLOCKFILE;
        } else {
            if ( strcmp( argv[i], "--help" ) != 0 && strcmp( argv[i], "-h" ) != 0 )
                printf( "\nUnknown Option: %s\n", argv[i] );
            print_usage();
            exit(0);
        }
    }

    if ( i == argc ) {
        print_usage();
        exit(0);
    }

    if ( i != argc - 1 ) {
        printf( "Error: Invalid Command\n\n" );
        print_usage();
        exit(0);
    }

    // parse command line commands
    if ( strcmp( argv[i], "start" ) != 0 ) {
        if ( strcmp( argv[i], "stop" ) == 0 ) {
            if ( !daemon_stop( force_stop ) ) exit( EXIT_FAILURE );
            exit( EXIT_SUCCESS );
        } else if ( strcmp( argv[i], "restart" ) == 0 ) {
            if ( !daemon_stop( force_stop ) ) exit( EXIT_FAILURE );
        } else {
            printf( "Error: Unknown Command: %s\n\n", argv[i] );
            print_usage();
            exit(0);
        }
    }
    /////////////////////////////////////////////////

    if ( make_daemon ) {
        if ( !daemon_init( daemon_flags ) ) {
            exit(0);
        }
    }

    zcnf_daemon_t* daemon_cnf = zcnf_daemon_load();
    if ( !daemon_cnf ) {
        fprintf( stderr, "failed to load config.\n" );
        exit( EXIT_FAILURE );
    }

    // Trap SIGPIPE
    signal( SIGPIPE, signal_handler );

    // call any needed library init functions
    website_init();
    zs_init( daemon_cnf->ssl_cert_path, daemon_cnf->ssl_key_path );
    memset( connections, 0, sizeof( connections ) );

    for ( i = 0; i < daemon_cnf->n; i++ ) {
        int sockfd = zsocket( inet_addr( daemon_cnf->proxies[ i ].ip ), daemon_cnf->proxies[ i ].port, ZSOCK_LISTEN, insock_event_hdlr, false );
        if ( sockfd == -1 ) {
            ret = EXIT_FAILURE;
            fprintf( stderr, "Proxy failed on %s:%d\n", daemon_cnf->proxies[ i ].ip, daemon_cnf->proxies[ i ].port );
            goto quit;
        }

        zs_set_read( sockfd );
        printf( " * started listening on %s:%d\n", daemon_cnf->proxies[ i ].ip, daemon_cnf->proxies[ i ].port );
    }

    FL_SET( daemon_flags, D_KEEPSTDIO );

#if defined(DEBUG)
    FL_SET( daemon_flags, D_NOCD );
    setlogmask( LOG_UPTO( LOG_DEBUG ) );
    openlog( DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER );
#else
    setlogmask( LOG_UPTO( LOG_INFO ) );
    openlog( DAEMON_NAME, LOG_CONS, LOG_USER );
#endif

    // daemonize
    if ( make_daemon ) {
        if ( !daemon_detach( daemon_flags ) ) {
            ret = EXIT_FAILURE;
            goto quit;
        }
    }

#ifndef DEBUG
    daemon_redirect_stdio();
#endif

    syslog( LOG_INFO, "initialized." );

    // starts a select() loop and calls
    // the associated file descriptor handlers
    // when they are ready to read/write
    do {
        do {
            msg_switch_select();
            zs_select();
        } while ( msg_switch_need_select() || zs_need_select() );
    } while ( zfd_select(2) );

quit:
    // cleanup

    zcnf_daemon_free( daemon_cnf );

    if ( ret == EXIT_FAILURE ) {
        syslog( LOG_INFO, "exit: failure" );
    } else {
        syslog( LOG_INFO, "exit: success" );
    }

    closelog();
    return ret;
}