void program_main(const argdata_t *ad) { // Extract executable file descriptor and argument data from sequence. argdata_seq_iterator_t it; argdata_seq_iterate(ad, &it); const argdata_t *fdv, *argv; int fd; if (!argdata_seq_next(&it, &fdv) || argdata_get_fd(fdv, &fd) != 0 || !argdata_seq_next(&it, &argv)) _Exit(127); // Serialize argument data that needs to be passed to the executable. size_t buflen, fdslen; argdata_get_buffer_length(argv, &buflen, &fdslen); int *fds = malloc(fdslen * sizeof(fds[0]) + buflen); if (fds == NULL) _Exit(127); void *buf = &fds[fdslen]; fdslen = argdata_get_buffer(argv, buf, fds); // Register file descriptors. struct fd_table ft; fd_table_init(&ft); for (size_t i = 0; i < fdslen; ++i) if (!fd_table_insert_existing(&ft, i, fds[i])) _Exit(127); // Start emulation. emulate(fd, buf, buflen, &posix_syscalls); _Exit(127); }
//main.c int main(int argc, char** argv) { addInfoLog(0,"==========billingd start=============="); addInfoLog(0,VER); //register the sig signal(SIGTERM, sigproc); signal(SIGHUP, sigproc); options_parse(argc, argv); if( daemon_mode ) daemonize(); if(check_running_pid()) exit(3); syn_time(); fd_table_init(); last_billing_out_time = time(NULL); // time() initialize last_rcv_ok = last_billing_out_time; last_up_time = last_billing_out_time; start_time.uptime_init = 0; start_time.write_init = 0; start_time.uptime_init = readuptime(); start_time.write_init = last_billing_out_time; //parse config file if ( !getBillingConfig(CONFIG_FILE) ) { addInfoLog(2,"Erorr when parsing config file"); exit(0); } if(conf.on_off == 0) { addInfoLog(0,"mod billing set off"); exit(0); } dealwith_folder(); hashtable_init(); billingd_epoll_init(); //fixme: 先写一次日志 addInfoLog(0,"first write billing data\n"); if( !writelog() ) { char errMessage[1024]; snprintf(errMessage, 1024, "[%s][%d]\t%s\n",(char *)__FILE__, __LINE__, "writelog Error"); addInfoLog(2,errMessage); exit(3); } //creat socket int SockFd; if( (SockFd = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { addInfoLog(2,"socket creat error,exit"); exit(0); } struct sockaddr_in ServerAddress; memset(&(ServerAddress), 0, sizeof(ServerAddress)); ServerAddress.sin_family = AF_INET; ServerAddress.sin_port = htons(Port); ServerAddress.sin_addr.s_addr = htonl(INADDR_ANY); int option = 1; if( setsockopt(SockFd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0 ) { addInfoLog(2,"setsockopt error,exit"); exit(0); } while( bind(SockFd,(struct sockaddr*)&ServerAddress, sizeof(struct sockaddr)) < 0 ) { addInfoLog(2,"socket bind error"); sleep(1); } if( listen(SockFd, 10) < 0 ) { addInfoLog(2,"socket listen error,exit"); exit(0); } // add the socket to epoll to listen new connections billingd_epoll_add(SockFd,EPOLLIN | EPOLLHUP | EPOLLERR); listen_fd = SockFd; time_t checktime; char checkMessage[1024]; uint32_t wait_count = 0; while(1) { checktime = time(NULL); //================= write the billing file according to the system time start======== if (checktime - last_billing_out_time > 599) { addInfoLog(2,"billingd time now has surpass last_write_ok override 600 seconds\n"); if (wait_count < 60 ) { //ensure all data come,wait 60s snprintf(checkMessage, 1024, "data comes,wait all data come[%d][%d]continue",wait_count,conf.interval); addInfoLog(0,checkMessage); wait_count++; } else { addInfoLog(0,"the last data comes ,cut it\n"); wait_count = 0; // count recover flag_cut_point = true; if(!writelog()) { char errMessage_1[1024]; snprintf(errMessage_1, 1024, "[%s][%d]\t%s\n",(char *)__FILE__, __LINE__, "writelog Error"); addInfoLog(2,errMessage_1); exit(3); } } } else if ( checktime - last_billing_out_time < 0) { snprintf(checkMessage, 1024, "[%s][%d]\tchecktime:%d < last_billing_out_time:%d\n",(char *)__FILE__, __LINE__, (int)checktime, (int)last_billing_out_time); addInfoLog(0,checkMessage); syn_time(); // adjust clock if(!writelog()) { char errMessage_1[1024]; snprintf(errMessage_1, 1024, "[%s][%d]\t%s\n",(char *)__FILE__, __LINE__, "writelog Error"); addInfoLog(2,errMessage_1); exit(3); } } //如果现在时间比上次写时间超过interval了, 就写个文件 else if (checktime - last_billing_out_time >= conf.interval ) { char result[200]; snprintf(result,200,"checktime:%d - last_billing_out_time:%d = %d;internal=%d",(int)checktime,(int)last_billing_out_time,(int)(checktime-last_billing_out_time),conf.interval); addInfoLog(1,result); addInfoLog(1,"checktime >= last_billing_out_time + conf.interval"); if(!writelog()) { char errMessage_2[1024]; snprintf(errMessage_2, 1024, "[%s][%d]\t%s\n",(char *)__FILE__, __LINE__, "writelog Error"); addInfoLog(2,errMessage_2); exit(3); } } else { // checktime - last_billing_out_time < conf.interval // do nothing } //================== write the billing file according to the system time end========== //如果需要关闭了, 也先写个文件, 然后退出 if(shutdown_flag == true) { writelog(); log_close(); fd_table_dest(); exit(1); } //如果需要reload, 写个文件, 然后清空配置(regex)链表, 重新load configure, 然后继续 if(reload_flag == true) { if(!writelog()) { char errMessage_3[1024]; snprintf(errMessage_3, 1024, "[%s][%d]\t%s\n",(char *)__FILE__, __LINE__, "writelog Error"); addInfoLog(2,errMessage_3); exit(3); } //释放掉正则连结构 struct billingConfNode* qstart = conf.confHead; struct billingConfNode* ploop = NULL; struct billingConfNode* ptemp = NULL; for( ploop = qstart; ploop; ) { ptemp = ploop->next; cc_free(ploop->regComp); cc_free(ploop); ploop = ptemp; } conf.confHead = NULL; //重新装载配置 if ( !getBillingConfig(CONFIG_FILE) ) { addInfoLog(2,"erorr happens when parsing config file"); exit(0); } reload_flag = false; } /* epoll wait when billing data arrive and new connection come this is a simple epoll,we only focus on read */ billing_epoll_wait(); } }
/* end by xin.yao: 2011-11-14 */ if(-1 == getOrigDomainConf(fp)) { addInfoLog(2, "getOriginDomainConf error[there are errors in conf file]"); } fclose(fp); } fp = fopen(config.origDomainIPHistoryFileName, "r"); if (NULL == fp) addInfoLog(2, "can not open origin_domain_ip"); else { g_pstOrigDomainHistory_init(); if (-1 == getOrigDomainHistory(fp)) addInfoLog(2, "getOriginDomainHistory error[here are errors in conf file]"); fclose(fp); } return 0; } #ifndef FRAMEWORK int main(int argc, char **argv) #else /* not FRAMEWORK */ int mod_main(int argc, char **argv) #endif /* not FRAMEWORK */ { /* parse command line param */ cmd_parser(argc, argv); /* open log file first, * because of we need write log in the following handled */ open_log(); /* initialize the config variable, * using default configuration or value */ load_default_conf(); detectGlobalInit(); check_conf_file_valid(); /* fork for detect, parent process will wait for child process */ #ifndef FRAMEWORK pid_t pid = fork(); if (pid < 0) { perror("fork()"); kill(0, SIGTERM); exit(1); } if (!pid) #else /* not FRAMEWORK */ { /* 复制自下部分代码,关于fork后主进程的工作 */ struct sigaction sa; struct itimerval itv; sa.sa_handler = alarmTimeoutHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGALRM, &sa, NULL); itv.it_interval.tv_sec = 1; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = custom_cfg.detect_timeout;//g_detect_timeout; //printf("detect_timeout: %ld\n", custom_cfg.detect_timeout); itv.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &itv, NULL); } #endif /* not FRAMEWORK */ { int rtn = 0; #ifndef FRAMEWORK int sleep_time; /* set current process resource limit * Limits on resources crontab, and the process is the child process crontab, * inherited this limit, so in order to the stability of the program to modify resource constraints */ if (set_process_rlimit() < 0) { return -1; } /* delayed time: [10, 30)s, * in order to try to spread more FC and upper detection */ if(!delay_start) { srand((unsigned)time(NULL)); //??ʼ???????? sleep_time = rand() % 20 + 10; fprintf(stdout, "sleep %d\n", sleep_time); sleep(sleep_time); } /* core task */ detect_epoll_init(); fd_table_init(); rtn = process_core_task(); if (rtn < 0) { rtn = -1; } fd_table_dest(); eventFreeMemory(); clean_detect_mem(); close_log(); return rtn; #else /* not FRAMEWORK */ if (set_process_rlimit() < 0) { module_enable = 0; return 1; } rtn = process_core_task_before(); if (rtn < 0) { module_enable = 0; return 1; } return 0; #endif /* not FRAMEWORK */ } #ifndef FRAMEWORK else {
void kernel_main(uint32_t r0, uint32_t r1, uint32_t *atags, uint32_t memory_kernel) { unsigned int memory_total; int init_process,idle_process; struct atag_info_t atag_info; uint32_t framebuffer_width=800,framebuffer_height=600; uint32_t temperature; (void) r0; /* Ignore boot method */ /* Initialize Software Structures */ processes_init(); /* Detect Hardware */ atags_detect(atags,&atag_info); hardware_type=atag_info.hardware_type; /* Initialize Hardware */ /* Serial console is most important so do that first */ uart_init(); /* Enable HW random number generator */ bcm2835_rng_init(); /* Enable Interrupts */ enable_interrupts(); /************************/ /* Boot message! */ /************************/ printk("\nBooting VMWos...\n"); /**************************/ /* Device Drivers */ /**************************/ /* Set up ACT LED */ led_init(); /* Set up timer */ timer_init(); /* Set up keyboard */ ps2_keyboard_init(); /* Enable the Framebuffer */ if (atag_info.framebuffer_x!=0) { framebuffer_width=atag_info.framebuffer_x; } if (atag_info.framebuffer_y!=0) { framebuffer_height=atag_info.framebuffer_y; } framebuffer_init(framebuffer_width,framebuffer_height,24); framebuffer_console_init(); /* Delay to allow time for serial port to settle */ /* So we can actually see the output on the terminal */ delay(0x3f0000); printk("\nWaiting for serial port to be ready (press any key)\n"); uart_getc(); uart_enable_interrupts(); /* Clear screen */ printk("\n\033[2J\n\n"); /* Print boot message */ printk("\033[0;41m \033[42m \033[44m \033[42m \033[44m \033[0m VMW OS\n"); printk(" \033[0;41m \033[42m \033[44m \033[42m \033[44m \033[0m Version 0.%d\n\n",VERSION); /* Print hardware version */ printk("Hardware version: %x ",r1); if (r1==0xc42) printk("(Raspberry Pi)"); else printk("(Unknown Hardware)"); printk("\n"); printk("Detected Model "); switch(hardware_type) { case RPI_MODEL_A: printk("A"); break; case RPI_MODEL_APLUS: printk("A+"); break; case RPI_MODEL_B: printk("B"); break; case RPI_MODEL_BPLUS: printk("B+"); break; case RPI_MODEL_B2: printk("B2"); break; case RPI_COMPUTE_NODE: printk("Compute Node"); break; default: printk("Unknown %x",hardware_type); break; } printk("\n"); /* Check temperature */ temperature=thermal_read(); printk("CPU Temperature: %dC, %dF\n", temperature/1000, ((temperature*9)/5000)+32); /* Print ATAGS */ atags_dump(atags); printk("\n"); /* Get amount of RAM from ATAGs */ memory_total=atag_info.ramsize; /* Init memory subsystem */ memory_init(memory_total,memory_kernel); /* Start HW Perf Counters */ arm1176_init_pmu(); #if 0 asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); // printk("Heisenbug!\n"); #endif /* Setup Memory Hierarchy */ #if 1 memset_benchmark(memory_total); #else /* Enable L1 i-cache */ printk("Enabling L1 icache\n"); enable_l1_dcache(); /* Enable branch predictor */ printk("Enabling branch predictor\n"); /* Enable L1 d-cache */ printk("Enabling MMU with 1:1 Virt/Phys page mapping\n"); enable_mmu(0,memory_total); printk("Enabling L1 dcache\n"); enable_l1_dcache(); #endif /* Init the file descriptor table */ fd_table_init(); /* Initialize the ramdisk */ ramdisk_init(initrd_image,sizeof(initrd_image)); /* Mount the ramdisk */ mount("/dev/ramdisk","/","romfs",0,NULL); /* Load the idle thread */ idle_process=load_process("idle",PROCESS_FROM_RAM, (char *)&idle_task,8,4096); init_process=load_process("shell",PROCESS_FROM_DISK, NULL,0,8192); load_process("printa",PROCESS_FROM_DISK, NULL,0,8192); load_process("printb",PROCESS_FROM_DISK, NULL,0,8192); /* Enter our "init" process*/ printk("\nEntering userspace by starting process %d!\n", init_process); process[idle_process].ready=1; process[init_process].ready=1; userspace_started=1; /* run init and restore stack as we won't return */ run_process(init_process,0x8000); /* we should never get here */ while(1) { /* Loop Forever */ /* Should probably execute a wfi instruction */ } }