WorkSpace::WorkSpace() : DaemonBase("127.0.0.1", "8081", 0) , checksum_table("CheckSums.db", "/var/db/FileIntegrity/CheckSums/") , files_by_ind_table("FilesNamesByInotifysDescriptors.db", "/var/db/FileIntegrity/InotifysDescriptors/") , ind_by_files_table("InotifysDescriptorsByFilesNames.db", "/var/db/FileIntegrity/InotifysDescriptors/") , inotify(inotify_init()) , m() { pthread_mutexattr_t a; pthread_mutexattr_init(&a); pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&m, &a); pthread_mutexattr_destroy(&a); AddFilesToInotify(); pthread_t id; pthread_create(&id, NULL, StatInotify, this); Daemon(); // if (!fork()) { // Daemon(); // wait(NULL); // } else { // RecCheck(); // } }
int main(){ Daemon();//成为守护进程 while(1){ sleep(1); } return 0; }
int32_t TrapHandler(struct NaClApp *nap, uint32_t args) { uint64_t *sargs; int retcode = 0; int i; assert(nap != NULL); assert(nap->manifest != NULL); /* * translate address from user space to system * note: cannot set "trap error" */ sargs = (uint64_t*)NaClUserToSys(nap, (uintptr_t)args); i = FunctionIndexById(*sargs); ZLOGS(LOG_DEBUG, "%s called", function[i]); ZTrace("untrusted code"); switch(*sargs) { case TrapFork: if(Daemon(nap) == 0) { SyscallZTrace(5, function[5]); ZVMExitHandle(nap, 0); } break; case TrapExit: ZVMExitHandle(nap, (int32_t)sargs[2]); break; case TrapRead: retcode = ZVMReadHandle(nap, (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]); break; case TrapWrite: retcode = ZVMWriteHandle(nap, (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]); break; case TrapJail: retcode = ZVMJailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]); break; case TrapUnjail: retcode = ZVMUnjailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]); break; default: retcode = -EPERM; ZLOG(LOG_ERROR, "function %ld is not supported", *sargs); break; } /* report, ztrace and return */ FastReport(); ZLOGS(LOG_DEBUG, "%s returned %d", function[i], retcode); SyscallZTrace(i, function[i], sargs[2], sargs[3], sargs[4], sargs[5], retcode); return retcode; }
int main(){ int i=0; Daemon(); while(i<10){ printf("%d\n",i); time_t ttime; time(&ttime); struct tm *pTm=gmtime(&ttime); syslog(LOG_INFO,"%d %04d-%02d-%02d %02d:%02d:%02d",i ,(1900+pTm->tm_year),(1+pTm->tm_mon),(pTm->tm_mday) ,(pTm->tm_hour),(pTm->tm_min),(pTm->tm_sec)); i++; sleep(2); } }
bool update_startd() { static Daemon startd( DT_STARTD ); static ReliSock * rsock = NULL; if( rsock == NULL ) { // We can't continue to use the original Daemon object, if the // reason we lost our connection is because the startd's address // changed (e.g., was restarted). We need to reconstruct on every // reconnection attempt, because the address may not have been // updated since the connection was lost (e.g., the startd just died). startd = Daemon( DT_STARTD ); rsock = new ReliSock(); if( ! startd.locate() ) { dprintf( D_ALWAYS, "Can't locate startd, aborting (%s)\n", startd.error() ); return false; } if(! rsock->connect( startd.addr(), 0 )) { dprintf( D_ALWAYS, "Can't connect to startd at: %s, " "aborting\n", startd.addr() ); return false; } } if( !startd.sendCommand(X_EVENT_NOTIFICATION, rsock, 3) ) { dprintf( D_ALWAYS, "Can't send X_EVENT_NOTIFICATION command " "to startd at: %s, aborting\n", startd.addr() ); rsock->close(); delete rsock; rsock = NULL; return false; } dprintf( D_FULLDEBUG, "Sent update to startd at: %s\n", startd.addr() ); return true; }
int CXtApp::InitInstance(void) { int nRet = FUN_RET_OK; if ( m_bDaemon ) /* == TRUE */ { nRet = Daemon(); if ( nRet < 0 ) /* Failed to transform the process into daemon mode. */ { m_bDaemon = FALSE; return -3; } } nRet = OnInitInstance(); if ( nRet < 0 ) { return -5; } return FUN_RET_OK; }
int main(int argc, char **argv) { #ifdef DAEMON /* отделяемся от родительского процесса */ int pid = fork(); switch (pid) { case 0: // child { umask(0); //777 setsid(); // Создание нового SID для дочернего процесса /*Закрываем стандартные файловые дескрипторы*/ close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO); signal(SIGHUP, signal_handler); signal(SIGTERM, signal_handler); openlog("daemon", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); #endif // CODE dmn = Daemon(); dmn.start(); exit(0); #ifdef DAEMON break; } case -1: // error! exit(EXIT_FAILURE); /*Если с PID ’ ом в с е получилось , то родительский процесс можно завершить .*/ default:// parent return 0; } #endif }
int main(int argc, char **argv) { const char* short_options = "b:a:p:k:c:f:s:u:hd"; struct option long_options[] = { { "bind", 1, NULL, 'b' }, { "addr", 1, NULL, 'a' }, { "port", 1, NULL, 'p' }, { "key", 1, NULL, 'k' }, { "cert", 1, NULL, 'c' }, { "file", 1, NULL, 'f' }, { "daemon", 0, NULL, 'd' }, { "server", 0, NULL, 's' }, { "ue", 0, NULL, 'u' }, { "help", 0, NULL, 'h' }, { 0, 0, 0, 0}, }; int c; while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1) { switch (c) { case 'b': g_bind_port = strtoul(optarg, NULL, 0); break; case 'a': g_proxy_host = optarg; break; case 'p': g_proxy_port = strtoul(optarg, NULL, 0); break; case 'k': g_key_path.assign(optarg); break; case 'c': g_cert_path.assign(optarg); break; case 'f': g_data_file.assign(optarg); break; case 's': g_replaceServ.assign(optarg); break; case 'u': g_replaceUE.assign(optarg); break; case 'd': g_daemon = true; break; case 'h': usage(); return 0; default: printf("unkown type %c\n", c); break; } } if (g_daemon && Daemon() < 0) { return -1; } BugReportRegister(argv[0], ".", NULL, NULL); SetSignal(); g_logFp = fopen(LOG_FILE, "w"); if (g_logFp == NULL) { printf("can't open log file %s\n", LOG_FILE); return -1; } if (g_key_path.empty()) g_key_path = g_cert_path; if (!g_data_file.empty()) { g_dataFp = fopen(g_data_file.c_str(), "w"); if (g_dataFp == NULL) { PrintLog(g_logFp, "can't open log file %s\n", g_data_file.c_str()); fclose(g_logFp); return -1; } } PrintLog(g_logFp, "proxy is run...\n"); Proxy proxy; proxy.Run(g_bind_port); if (g_dataFp) { fclose(g_dataFp); g_dataFp = NULL; } PrintLog(g_logFp, "proxy is exit...\n"); if (g_logFp) { fclose(g_logFp); g_logFp = NULL; } return 0; }
int32_t TrapHandler(struct NaClApp *nap, uint32_t args) { uint64_t *sargs; int retcode = 0; assert(nap != NULL); assert(nap->manifest != NULL); /* * translate address from user space to system * note: cannot set "trap error" */ sargs = (uint64_t*)NaClUserToSys(nap, (uintptr_t)args); ZLOGS(LOG_DEBUG, "%s called", FunctionName(*sargs)); ZTrace("untrusted code"); switch(*sargs) { case TrapFork: retcode = Daemon(nap); if(retcode) break; SyscallZTrace(*sargs, 0); SyscallZTrace(TrapExit, 0); ZVMExitHandle(nap, 0); break; case TrapExit: SyscallZTrace(*sargs, sargs[2]); ZVMExitHandle(nap, (int32_t)sargs[2]); break; case TrapRead: retcode = ZVMReadHandle(nap, (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]); break; case TrapWrite: retcode = ZVMWriteHandle(nap, (int)sargs[2], (char*)sargs[3], (int32_t)sargs[4], sargs[5]); break; case TrapJail: retcode = ZVMJailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]); break; case TrapUnjail: retcode = ZVMUnjailHandle(nap, (uint32_t)sargs[2], (int32_t)sargs[3]); break; #ifdef ZVM_SOCKETS case TrapSocket: retcode = ZVM_socket((int)sargs[2], (int)sargs[3], (int)sargs[4]); break; case TrapBind: { const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_bind((int)sargs[2], addr, (socklen_t)sargs[4]); break; } case TrapConnect: { const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_connect((int)sargs[2], addr, (socklen_t)sargs[4]); break; } case TrapAccept: { struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[4]); retcode = ZVM_accept((int)sargs[2], addr, len); break; } case TrapListen: retcode = ZVM_listen((int)sargs[2], (int)sargs[3]); break; case TrapRecv: { void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_recv((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5]); break; } case TrapRecvfrom: { void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]); socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[7]); retcode = ZVM_recvfrom((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5], addr, len); break; } case TrapRecvmsg: { struct msghdr *msg = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_recvmsg((int)sargs[2], msg, (int)sargs[4]); break; } case TrapSend: { const void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_send((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5]); break; } case TrapSendto: { const void *buf = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); const struct sockaddr *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]); retcode = ZVM_sendto((int)sargs[2], buf, (size_t)sargs[4], (int)sargs[5], addr, (socklen_t)sargs[7]); break; } case TrapSendmsg: { const struct msghdr *msg = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); retcode = ZVM_sendmsg((int)sargs[2], msg, (int)sargs[4]); break; } case TrapGetsockopt: { void *optval = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]); socklen_t *len = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]); retcode = ZVM_getsockopt((int)sargs[2], (int)sargs[3], (int)sargs[4], optval, len); break; } case TrapSetsockopt: { const void *optval = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]); retcode = ZVM_setsockopt((int)sargs[2], (int)sargs[3], (int)sargs[4], optval, (socklen_t)sargs[6]); break; } case TrapSelect: { fd_set *readfds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[3]); fd_set *writefds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[4]); fd_set *exceptfds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[5]); struct timeval *timeout = (void*)NaClUserToSys(nap, (uintptr_t)sargs[6]); retcode = ZVM_select((int)sargs[2], readfds, writefds, exceptfds, timeout); break; } case TrapPoll: { struct pollfd *fds = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]); retcode = ZVM_poll(fds, (nfds_t)sargs[3], (int)sargs[4]); break; } case TrapGethostbyname: { const char *name = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]); retcode = (int)(intptr_t)ZVM_gethostbyname(name); break; } case TrapGethostbyaddr: { const void *addr = (void*)NaClUserToSys(nap, (uintptr_t)sargs[2]); retcode = (int)(intptr_t)ZVM_gethostbyaddr(addr, (socklen_t)sargs[3], (int)sargs[4]); break; } case TrapClose: retcode = ZVM_close((int)sargs[2]); break; #endif default: retcode = -EPERM; ZLOG(LOG_ERROR, "function %ld is not supported", *sargs); break; } /* report, ztrace and return */ FastReport(); ZLOGS(LOG_DEBUG, "%s returned %d", FunctionName(*sargs), retcode); SyscallZTrace(*sargs, retcode, sargs[2], sargs[3], sargs[4], sargs[5], sargs[6], sargs[7]); return retcode; }
// Main entry point to application int main(int argc, char *argv[]) { BYTE MsgBuff[MAXIPQMSG]; struct sigaction SigAction; int i; // Read config & parameters if(!ReadConfig(argc, argv)) return EXIT_FAILURE; // If we should act as neither a master nor a client (config error) if(!(Config.Flags & CONFIGFLAGS_MASTER) && !(Config.Flags & CONFIGFLAGS_CLIENT)){ printf(APPTITLE": not configured to run as a master nor client\n"); return EXIT_FAILURE; } // If we're to be a client, check we have an IP if(Config.Flags & CONFIGFLAGS_CLIENT && Config.MasterIp[0] == 0x00){ printf(APPTITLE": Can not run as a client, no master IP specified 0x%02X\n", Config.Flags); return EXIT_FAILURE; } // Let the user know we're thinking of them if(Config.Flags & CONFIGFLAGS_MASTER) Log(LOG_NOTICE, "Acting as a master on port %d", Config.MasterPort); if(Config.Flags & CONFIGFLAGS_SELFCLIENT) Log(LOG_NOTICE, "Acting as a self-client with %d packet queues", Config.Queue); if(Config.Flags & CONFIGFLAGS_CLIENT) Log(LOG_NOTICE, "Acting as a client to %s:%d with %d packet queues", Config.MasterIp, Config.MasterPort, Config.Queue); else Log(LOG_NOTICE, "Acting stupid"); // If we should daemonise if(Config.Flags & CONFIGFLAGS_DAEMON) // Daemonise Daemon(); // If we should act as a master if(Config.Flags & CONFIGFLAGS_MASTER){ // If we should act as _only_ a master if(!(Config.Flags & CONFIGFLAGS_CLIENT) && !(Config.Flags & CONFIGFLAGS_SELFCLIENT)){ // Jump to main function in master.c which will perform this function MasterRun(NULL); return EXIT_SUCCESS; } // If we should act as a client as well else{ // Start main thread in master.c which will perform this function pthread_create(&idMasterThread, NULL, &MasterRun, NULL); } } // Create an IPQ handle hIpq = ipq_create_handle(0, PF_INET); if(hIpq == NULL){ // If we're running a master thread if(idMasterThread){ // Set flag & wait for master thread to exit ExitMasterThread = 1; pthread_join(idMasterThread, NULL); } // Log it Log(LOG_ERR, "Failed to initialise IPQ (%s)", ipq_errstr()); return EXIT_FAILURE; } // Set mode. Note: We set to packet mode so that we get to see the // number of bytes in the payload, the payload itself is ignored if (ipq_set_mode(hIpq, IPQ_COPY_PACKET, 0) == -1){ // If we're running a master thread if(idMasterThread){ // Set flag & wait for master thread to exit ExitMasterThread = 1; pthread_join(idMasterThread, NULL); } // Release IPQ ipq_destroy_handle(hIpq); // Tell the user Log(LOG_ERR, "Failed to configure IPQ (%s)", ipq_errstr()); return EXIT_FAILURE; } // Allocate Packet queue memory for(i = 0; i < PacketQueues; i++) PacketQueue[i] = malloc(Config.Queue * sizeof(struct ipq_packet_msg)); // Check allocations worked if(!PacketQueue){ // Free anything that was allocated if(PacketQueue) free(PacketQueue); // If we're running a master thread if(idMasterThread){ // Set flag & wait for master thread to exit ExitMasterThread = 1; pthread_join(idMasterThread, NULL); } // Release IPQ ipq_destroy_handle(hIpq); // Tell the user Log(LOG_ERR, "Failed to allocate sufficant memory"); return EXIT_FAILURE; } // Inititalise the PacketQueue mutex pthread_mutex_init(&PacketQueueMutex, NULL); // If we're acting as a normal client if(Config.Flags & CONFIGFLAGS_CLIENT) // Start control thread pthread_create(&idControlThread, NULL, &ControlRun, NULL); // Install SIGTERM/etc handler to get out of 'while(!ExitMain)' memset(&SigAction, 0, sizeof(SigAction)); SigAction.sa_handler = SignalHandler; sigaction(SIGINT, &SigAction, NULL); sigaction(SIGQUIT, &SigAction, NULL); sigaction(SIGTERM, &SigAction, NULL); // Install SIGHUP handler to reread config sigaction(SIGHUP, &SigAction, NULL); // Tell user Log(LOG_INFO, "Client up and running"); // Enter main loop while(!ExitMain){ // Wait up to 100mS to recieve a message int MsgSize = ipq_read(hIpq, MsgBuff, MAXIPQMSG, 100); // Error getting message if(MsgSize == -1){ // TODO: This has started to happen occasionally. Don't know why. // ipq_errstr() = "Failed to receive netlink message" // Tell the user Log(LOG_ERR, "Error reading message from IPQ (%s)", ipq_errstr()); } // Timeout getting message else if(MsgSize == 0){ // Do background processing BackgroundProcessing(); } // Got a message else{ // Switch on message type switch (ipq_message_type(MsgBuff)){ case NLMSG_ERROR: // Apparently we should call this (to clear the error?) ipq_get_msgerr(MsgBuff); // Tell the user Log(LOG_ERR, "Error reading message type from IPQ (%s)", ipq_errstr()); break; case IPQM_PACKET: // Call a function to process the packet ProcessPacketMessage(ipq_get_packet(MsgBuff)); // Do background processing BackgroundProcessing(); break; default: // Tell the user Log(LOG_WARNING, "Undefined message type from IPQ"); break; } } } // If we're running a control thread if(idControlThread){ // Set flag & wait for control thread to exit ExitControlThread = 1; pthread_join(idControlThread, NULL); } // If we're running a master thread if(idMasterThread){ // Set flag & wait for master thread to exit ExitMasterThread = 1; pthread_join(idMasterThread, NULL); } // Release IPQ ipq_destroy_handle(hIpq); // Release Packet queue memory for(i = 0; i < PacketQueues; i++) free(PacketQueue[i]); // Let the user know we're thinking of them Log(LOG_INFO, "Exited cleanly"); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int listenfd,connfd, port,clientlen; pid_t pid; struct sockaddr_in clientaddr; char isdaemon=0,*portp=NULL,*logp=NULL,tmpcwd[MAXLINE]; #ifdef HTTPS int sslport; char dossl=0,*sslportp=NULL; #endif openlog(argv[0],LOG_NDELAY|LOG_PID,LOG_DAEMON); cwd=(char*)get_current_dir_name(); strcpy(tmpcwd,cwd); strcat(tmpcwd,"/"); /* parse argv */ #ifdef HTTPS parse_option(argc,argv,&isdaemon,&portp,&logp,&sslportp,&dossl); sslportp==NULL ?(sslport=atoi(Getconfig("https"))) : (sslport=atoi(sslportp)); if(dossl==1||strcmp(Getconfig("dossl"),"yes")==0) dossl=1; #else parse_option(argc,argv,&isdaemon,&portp,&logp); #endif portp==NULL ?(port=atoi(Getconfig("http"))) : (port=atoi(portp)); Signal(SIGCHLD,sigChldHandler); /* init log */ if(logp==NULL) logp=Getconfig("log"); initlog(strcat(tmpcwd,logp)); /* whethe show dir */ if(strcmp(Getconfig("dir"),"no")==0) isShowdir=0; clientlen = sizeof(clientaddr); if(isdaemon==1||strcmp(Getconfig("daemon"),"yes")==0) Daemon(1,1); writePid(1); /* $https start */ #ifdef HTTPS if(dossl) { if((pid=Fork())==0) { listenfd= Open_listenfd(sslport); ssl_init(); while(1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { Close(connfd); continue; } else if(pid==0) { ishttps=1; doit(connfd); exit(1); } } } } #endif /* $end https */ listenfd = Open_listenfd(port); while (1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); if(access_ornot(inet_ntoa(clientaddr.sin_addr))==0) { clienterror(connfd,"maybe this web server not open to you!" , "403", "Forbidden", "Tiny couldn't read the file"); continue; } if((pid=Fork())>0) { Close(connfd); continue; } else if(pid==0) { doit(connfd); exit(1); } } }