int main(int argc, char **argv) { /* Mainly only for safety */ setupSignal(); /* Initialize decoder's */ DecoderSet(dInstr); banner(argc,argv); if( (u2AnonContext = parseCommandLine(argc,argv)) == NULL) { /* XXX */ CleanExit(1); } if(u2AnonContext->verbose_flag) { printContext(u2AnonContext); } if( fileOperation(u2AnonContext)) { /* XXX */ CleanExit(1); } CleanExit(0); /* Exit here but remove the compile warn ...*/ return 0; }
void GoDaemon(void) { pid_t fs; int fd; LogMessage("Initializing daemon mode\n"); fs = fork(); if(fs > 0) exit(0); /* parent */ if(fs < 0) { perror("fork"); CleanExit(1); } chdir("/"); setpgid(0, getpid()); if((fd = open("/dev/null", O_RDWR)) >= 0) { dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); } return; }
void* TransactWithClient(void* vSocket) { char *pBuff; pBuff = (char*)malloc(500); int nCSocket = (int)vSocket; bzero(pBuff,sizeof(pBuff)); while(strcpy(pBuff,RecieveMsg(nCSocket))!=NULL){ sleep(1); if(!strcmp(pBuff,"~2")) { CleanExit((void*)nCSocket); } else if(!strcmp(pBuff,"1")) { printf("Message from Client %d: ",nCSocket); RequestMonitor(nCSocket); } else if(!strcmp(pBuff,"2")) { printf("Message from Client %d: ",nCSocket); ViewMonitoringStatus(nCSocket); } else if(!strcmp(pBuff,"3")) { printf("Message from Client %d: ",nCSocket); Exit((void*)nCSocket); } } }
void setupSignal(void) { int x = 0; struct sigaction SigStr; struct sigaction oldSigStr; memset(&SigStr,'\0',sizeof(struct sigaction)); memset(&oldSigStr,'\0',sizeof(struct sigaction)); /* setup signal handlers */ SigStr.sa_handler = &sigHand; for(x = 1; x <= 31; x++) { /* Ignore sighand for SIGKILL and SIGSTOP */ if( (x != 9) && (x != 17) && (x != 19) && (x != 23)) { if( (sigaction(x,&SigStr,&oldSigStr))) { /* XXX */ CleanExit(1); } } } }
/** * @function Gui_DemoFileBrowser * @brief File browser demo * @param signal_t sig: unused * @return none */ void Gui_DemoFileBrowser(signal_t sig) { rect_st rec; coord_t hEntry, y, pitch; Gui_DemoNewPage("built-in file browser"); /*allocate memory for storing file paths*/ if(path_file == NULL) path_file = salloc(PATH_BUFF_SIZE); if(path_folder == NULL) path_folder = salloc(PATH_BUFF_SIZE); if(path_file != NULL && path_folder != NULL) { SetFont(G_FONT_DEFAULT); hEntry = P2D_GetTextHeight() + 4; pitch = hEntry - 3; y = 20; /** * widgets for file selection */ path_file[0] = 0; rec = GUI_Rect(15, y, 295, hEntry); GUI_W_TextAdd(&rec, "Select a file"); y += pitch; rec = GUI_Rect(15, y, 265, hEntry); GUI_W_UsrEntryAdd(&rec, path_file, PATH_BUFF_SIZE, false); rec = GUI_Rect(285, y, hEntry, hEntry); GUI_W_ButtonAdd(&rec, NULL, G_IMG_OPEN); GUI_SetSignal(E_PUSHED_TO_RELEASED, SIG_SELECT_FILE); /** * widgets for folder selection */ path_folder[0] = 0; y += pitch*2; rec = GUI_Rect(15, y, 295, hEntry); GUI_W_TextAdd(&rec, "Select a folder"); y += pitch; rec = GUI_Rect(15, y, 265, hEntry); GUI_W_UsrEntryAdd(&rec, path_folder, PATH_BUFF_SIZE, false); rec = GUI_Rect(285, y, hEntry, hEntry); GUI_W_ButtonAdd(&rec, NULL, G_IMG_OPEN); GUI_SetSignal(E_PUSHED_TO_RELEASED, SIG_SELECT_FOLDER); /* => page creation is now completed. Jump to the page's handler*/ GUI_SetUserTask(Gui_DemoFileBrowserHandler); } else { /*critical: salloc error*/ CleanExit(); } }
void sigHand(int sig) { printf("Parent PID [%u] Process PID [%u], received signal [%d], cleaning up and exiting \n", getppid(), getpid(), sig); /* Do not call clean exit on sigsegv... */ if(sig != 11) { CleanExit(sig); } else { exit(sig); } }
/* * Function: FatalError(const char *, ...) * * Purpose: When a fatal error occurs, this function prints the error message * and cleanly shuts down the program * * Arguments: format => the formatted error string to print out * ... => format commands/fillers * * Returns: void function */ void FatalError(const char *format,...) { char buf[STD_BUF+1]; va_list ap; va_start(ap, format); if(pv.daemon_flag) { vsnprintf(buf, STD_BUF, format, ap); syslog(LOG_CONS | LOG_DAEMON | LOG_ERR, "FATAL ERROR: %s", buf); } else { vfprintf(stderr, format, ap); fprintf(stderr,"Fatal Error, Quitting..\n"); } CleanExit(1); }
BOOL PutEvent ( PINPUT_RECORD InputRecord ) { EnterCriticalSection(&(EventBuffer.CriticalSection)); // // If no space at beginning of buffer, resize and shift right // if ( EventBuffer.EventIndex == 0 ) { EventBuffer.EventBuffer = REALLOC( EventBuffer.EventBuffer, (EventBuffer.MaxEvents + EVENT_INCREMENT) * sizeof(INPUT_RECORD)); if ( !EventBuffer.EventBuffer ) { CleanExit(1, 0); } memmove( EventBuffer.EventBuffer + EVENT_INCREMENT, EventBuffer.EventBuffer , EventBuffer.NumberOfEvents * sizeof(INPUT_RECORD) ); EventBuffer.EventIndex = EVENT_INCREMENT; } // // Add event // EventBuffer.EventIndex--; EventBuffer.NumberOfEvents++; memcpy( EventBuffer.EventBuffer + EventBuffer.EventIndex, InputRecord, sizeof(INPUT_RECORD )); LeaveCriticalSection(&(EventBuffer.CriticalSection)); return TRUE; }
/** * @function Gui_DemoFileBrowserHandler * @brief file browser demo handler * @param signal_t sig: signal coming from widgets * @return none */ static void Gui_DemoFileBrowserHandler(signal_t sig) { switch(sig) { case SIG_SELECT_FILE: GUI_M_FileBrowser(FILE_BROWSER_SEL_FILE, path_file, PATH_BUFF_SIZE); break; case SIG_SELECT_FOLDER: GUI_M_FileBrowser(FILE_BROWSER_SEL_FOLDER, path_folder, PATH_BUFF_SIZE); break; /*'menu' button*/ case SIG_DEMO_MENU: CleanExit(); break; /*build-in file browser buttons*/ case FILE_BROWSER_BTN_SELECT: case FILE_BROWSER_BTN_CANCEL: default: break; } }
/** * @function Gui_DemoKeyboardHandler * @brief keyboard demo handler * @param signal_t sig: signal coming from widgets * @return none */ static void Gui_DemoKeyboardHandler(signal_t sig) { switch(sig) { case SIG_CREATE_AZERTY: GUI_M_KeyboardCreate(20, 90, KEYBOARD_TYPE_FR_FR, buf_azerty, 200, SIG_KBD_OK, SIG_KBD_ESC); break; case SIG_CREATE_QWERTY: GUI_M_KeyboardCreate(20, 90, KEYBOARD_TYPE_EN_US, buf_qwerty, 200, SIG_KBD_OK, SIG_KBD_ESC); break; case SIG_CREATE_NUM: GUI_M_KeyboardCreate(200, 90, KEYBOARD_TYPE_NUM, buf_num, 200, SIG_KBD_OK, SIG_KBD_ESC); break; /*'menu' button*/ case SIG_DEMO_MENU: CleanExit(); break; default: break; } }
PINPUT_RECORD NextEvent ( BOOL fAdvance, BOOL fWait ) /*++ Routine Description: Returns pointer to next event record. Arguments: fAdvance - Supplies a flag: if TRUE: Advance to next event record if FALSE: Do not advance to next event record fWait - Supplies a flag: if TRUE, the blocks until an event is ready. if FALSE, return immediately. Return Value: Pointer to event record, or NULL. --*/ { PINPUT_RECORD pEvent; BOOL Success; EnterCriticalSection(&(EventBuffer.CriticalSection)); // // If the busy flag is set, then the buffer is in the process of // being read. Only one thread should want to wait, so it is // safe to simply return. // if ( EventBuffer.BusyFlag ) { assert( !fWait ); LeaveCriticalSection(&(EventBuffer.CriticalSection)); return NULL; } if (EventBuffer.NumberOfEvents == 0) { // // No events in buffer, read as many as we can // DWORD NumberOfEvents; // // If the buffer is too big, resize it // if ( EventBuffer.MaxEvents > MAX_EVENTS ) { EventBuffer.EventBuffer = REALLOC( EventBuffer.EventBuffer, MAX_EVENTS * sizeof( INPUT_RECORD ) ); EventBuffer.MaxEvents = MAX_EVENTS; assert( EventBuffer.EventBuffer ); if ( !EventBuffer.EventBuffer ) { CleanExit( 1, 0 ); } } Success = PeekConsoleInput( hInput, EventBuffer.EventBuffer, EventBuffer.MaxEvents, &NumberOfEvents); if ((!Success || (NumberOfEvents == 0)) && (!fWait)) { // // No events available and don't want to wait, // return. // LeaveCriticalSection(&(EventBuffer.CriticalSection)); return NULL; } // // Since we will block, we have to leave the critical section. // We set the Busy flag to indicate that the buffer is being // read. // EventBuffer.BusyFlag = TRUE; LeaveCriticalSection(&(EventBuffer.CriticalSection)); Success = ReadConsoleInput( hInput, EventBuffer.EventBuffer, NumberOfEvents, &EventBuffer.NumberOfEvents); EnterCriticalSection(&(EventBuffer.CriticalSection)); EventBuffer.BusyFlag = FALSE; if (!Success) { #if defined( DEBUG ) // OutputDebugString(" Error: Cannot read console events\n"); assert( Success ); #endif EventBuffer.NumberOfEvents = 0; } EventBuffer.EventIndex = 0; } if (EventBuffer.NumberOfEvents == 0) { #if defined( DEBUG ) // OutputDebugString(" Error: Cannot read console events\n"); assert( Success ); #endif return NULL; } pEvent = EventBuffer.EventBuffer + EventBuffer.EventIndex; // // If Advance flag is set, we advance the pointer to the next // record. // if (fAdvance) { if (--(EventBuffer.NumberOfEvents)) { switch (EVENT_TYPE(pEvent)) { case KEY_EVENT: case MOUSE_EVENT: case WINDOW_BUFFER_SIZE_EVENT: case MENU_EVENT: case FOCUS_EVENT: (EventBuffer.EventIndex)++; break; default: #if defined( DEBUG) sprintf(DbgBuffer, "WARNING: unknown event type %X\n", EVENT_TYPE(pEvent)); OutputDebugString(DbgBuffer); #endif (EventBuffer.EventIndex)++; break; } } } LeaveCriticalSection(&(EventBuffer.CriticalSection)); return pEvent; }
u2AnonConfig * parseCommandLine(int argc,char **argv) { u2AnonConfig *rConfig = NULL; int cOpt = 0; u_int32_t add_leading_slash = 0; u_int32_t pathlen = 0; if( (argc < ANON_STARTUP_ARG_COUNT) || (*argv == NULL)) { /* XXX */ usage(); return NULL; } if( (rConfig=(u2AnonConfig *)calloc(1,sizeof(u2AnonConfig))) == NULL) { /* XXX */ return NULL; } while( (cOpt = getopt(argc,argv,"r:R:o:O:s:ElLehPpxXv")) != -1) { add_leading_slash = 0; pathlen = 0; switch(cOpt) { case 's': if(optarg != NULL) { if( (rConfig->u2FilePrefix = strndup(optarg,(strlen(optarg)+1))) == NULL) { goto f_err; } } else { goto f_err; } break; case 'r': if(optarg != NULL) { if( (rConfig->inputFile = strndup(optarg,(strlen(optarg)+1))) == NULL) { goto f_err; } } else { goto f_err; } break; case 'R': if(optarg != NULL) { pathlen = strlen(optarg); if(optarg[pathlen] != '/') { pathlen += 1; add_leading_slash = 1; } if( (rConfig->inputDirectory = strndup(optarg,pathlen+1)) == NULL) { goto f_err; } if(add_leading_slash) { rConfig->inputDirectory[pathlen-1] = '/'; } /* We will be processing a directory */ rConfig->batchProcess = 1; } else { goto f_err; } break; case 'o': if(optarg != NULL) { if( (rConfig->outputFile = strndup(optarg,strlen(optarg)+1)) == NULL) { goto f_err; } } else { goto f_err; } break; case 'O': if(optarg != NULL) { pathlen = strlen(optarg); if(optarg[pathlen] != '/') { pathlen += 1; add_leading_slash = 1; } if( (rConfig->outputDirectory = strndup(optarg,pathlen+1)) == NULL) { goto f_err; } if(add_leading_slash) { rConfig->outputDirectory[pathlen-1] = '/'; } } else { goto f_err; } break; case 'e': case 'E': rConfig->process_flag ^= (ANON_EVENT); break; case 'l': case 'L': rConfig->process_flag ^= (ANON_LINK_LAYER); break; case 'P': case 'p': rConfig->process_flag ^= (ANON_PACKET); break; case 'x': case 'X': rConfig->process_flag ^= (ANON_EXTRA_DATA); break; case 'H': case 'h': usage(); if(rConfig != NULL) { free(rConfig); rConfig = NULL; } CleanExit(0); break; case 'v': rConfig->verbose_flag++; break; default: printf("[%s()]: Unknown option specified [%c][0x%x], bailing.... \n", __FUNCTION__, cOpt, cOpt); goto f_err; break; } } if( (validateCmdLine(rConfig))) { goto ex_err; } return rConfig; f_err: printf("[%s()]: option [%c], ERROR processing option \n", __FUNCTION__, cOpt); ex_err: if(rConfig != NULL) { free(rConfig); rConfig = NULL; } usage(); return NULL; }
/** * @function Gui_DemoKeyboard * @brief keyboard demo * @param signal_t sig: unused * @return none */ void Gui_DemoKeyboard(signal_t sig) { rect_st rec; coord_t hEntry, y, pitch; Gui_DemoNewPage("built-in keyboards"); /*allocate memory for usr entries*/ if(buf_azerty == NULL) buf_azerty = salloc(KBD_BUFF_SIZE); if(buf_qwerty == NULL) buf_qwerty = salloc(KBD_BUFF_SIZE); if(buf_num == NULL) buf_num = salloc(KBD_BUFF_SIZE); if(buf_azerty != NULL && buf_qwerty != NULL && buf_num != NULL) { SetFont(G_FONT_DEFAULT); hEntry = P2D_GetTextHeight() + 4; pitch = hEntry - 3; y = 20; /** * widgets for azerty */ rec = GUI_Rect(15, y, 295, hEntry); GUI_W_TextAdd(&rec, "String input, with AZERTY keyboard"); y += pitch; snprintf(buf_azerty, KBD_BUFF_SIZE, "azerty"); /*make sure that the buffer is \0 terminated*/ rec = GUI_Rect(15, y, 265, hEntry); GUI_W_UsrEntryAdd(&rec, buf_azerty, KBD_BUFF_SIZE, false); rec = GUI_Rect(285, y, hEntry, hEntry); GUI_W_ButtonAdd(&rec, NULL, G_IMG_EDIT); GUI_SetSignal(E_PUSHED_TO_RELEASED, SIG_CREATE_AZERTY); /** * widgets for qwerty */ y += pitch*2; rec = GUI_Rect(15, y, 295, hEntry); GUI_W_TextAdd(&rec, "String input, with QWERTY keyboard"); y += pitch; snprintf(buf_qwerty, KBD_BUFF_SIZE, "qwerty"); rec = GUI_Rect(15, y, 265, hEntry); GUI_W_UsrEntryAdd(&rec, buf_qwerty, KBD_BUFF_SIZE, false); rec = GUI_Rect(285, y, hEntry, hEntry); GUI_W_ButtonAdd(&rec, NULL, G_IMG_EDIT); GUI_SetSignal(E_PUSHED_TO_RELEASED, SIG_CREATE_QWERTY); /** * widgets for num */ y += pitch*2; rec = GUI_Rect(15, y, 295, hEntry); GUI_W_TextAdd(&rec, "String input, with NUM keyboard"); y += pitch; snprintf(buf_num, KBD_BUFF_SIZE, "0"); rec = GUI_Rect(15, y, 265, hEntry); GUI_W_UsrEntryAdd(&rec, buf_num, KBD_BUFF_SIZE, false); rec = GUI_Rect(285, y, hEntry, hEntry); GUI_W_ButtonAdd(&rec, NULL, G_IMG_EDIT); GUI_SetSignal(E_PUSHED_TO_RELEASED, SIG_CREATE_NUM); /* => page creation is now completed. Jump to the page's handler*/ GUI_SetUserTask(Gui_DemoKeyboardHandler); } else { /*critical: salloc error*/ CleanExit(); } }
void prep_s2fishdome(OPTIONS *ioptions) { _s2fd_options = ioptions; texturetop = NULL; texturebottom = NULL; textureleft = NULL; textureright = NULL; mesh = NULL; meshtype = 0; meshnx = meshny = 0; ndometop = ndomebottom = 0; ndomeleft = ndomeright = 0; dometop = domebottom = NULL; domeleft = domeright = NULL; if (_s2fd_options->dometype == WARPMAP) { // Possibly read the mesh file, for warped dome projection if (!ReadBinMesh(_s2fd_options->meshfile)) { if (_s2fd_options->debug) fprintf(stderr,"Creating default mesh\n"); MakeDefaultMesh(); } } else { //fprintf(stderr, "Making a default mesh of course\n"); MakeDefaultMesh(); } if (_s2fd_options->debug) fprintf(stderr,"Creating the dome\n"); if (!DomeCreate()) { fprintf(stderr,"Failed to create dome geometry\n"); CleanExit(); } // Create the texture buffers and default images if (_s2fd_options->debug) fprintf(stderr,"Creating dome textures\n"); DomeTextures(); // Texture IDs for each face if (_s2fd_options->debug) fprintf(stderr,"Binding textures\n"); glGenTextures(4,walltextureid); if (walltextureid[0] < 1 || walltextureid[1] < 1 || walltextureid[2] < 1 || walltextureid[3] < 1) { fprintf(stderr,"Received bad texture id <%d,%d,%d,%d>\n", (int)walltextureid[0],(int)walltextureid[1],(int)walltextureid[2],(int)walltextureid[3]); } int i; for (i=0;i<4;i++) { if (_s2fd_options->debug) fprintf(stderr,"%d\n",i); glBindTexture(GL_TEXTURE_2D,walltextureid[i]); #if defined(S2CYGWIN) glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP); #else glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); #endif glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); switch (i) { case 0: glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEXTURESIZE,TEXTURESIZE,0,GL_RGBA,GL_UNSIGNED_BYTE,texturetop); break; case 1: glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEXTURESIZE,TEXTURESIZE,0,GL_RGBA,GL_UNSIGNED_BYTE,texturebottom); break; case 2: glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEXTURESIZE,TEXTURESIZE,0,GL_RGBA,GL_UNSIGNED_BYTE,textureleft); break; case 3: glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEXTURESIZE,TEXTURESIZE,0,GL_RGBA,GL_UNSIGNED_BYTE,textureright); } } _s2debug("(internal)", "/S2FISH*,/S2TRUNC* device support loaded"); }