int listAccessPoint() { int pick_count = 0; int iNetIndex; // skip the 0th connection for(iNetIndex = 1; iNetIndex < 100; iNetIndex++) { if(sceUtilityCheckNetParam(iNetIndex) == 0) { sceUtilityGetNetParam(iNetIndex, PSP_NETPARAM_NAME, (void*)AccessPoints[pick_count].pInfo->name); strcpy(AccessPoints[pick_count].pInfo->ip,"Acquiring..."); AccessPoints[pick_count].color = ORANGE; AccessPoints[pick_count].index = iNetIndex; pick_count++; if(pick_count >= MAX_APS) { wifiError = 1; break; } } else break; } if(pick_count == 0) { writeToLogFile("No connections\n"); writeToLogFile("Please try Network Settings\n"); sceKernelDelayThread(1000000); // 1sec to read before exit wifiError = 1; return -1; } return 0; }
// replacement for assert that writes the error to the log file void exitIfFalse(int value, const char *assertion, const char *func, const char *file, int line) { if(!value) { writeToLogFile("\n\n********** An Error Occurred **********" "\n* Shutting Down *\n\n"); writeToLogFile("Assertion `%s' failed in function '%s' at %s:%i.\n", assertion, func, file, line); writeToLogFile("This is an OpenBOR bug. Please report this at www.chronocrash.com.\n\n"); borExit(1); } }
//Releases UpperMalloced memory void UpperFree(void* mem) { #ifndef WIZ int i = (((int)mem) - ((int)UpperMem)); if(i < 0 || i >= 0x2000000) writeToLogFile("UpperFree of not UpperMalloced mem: %p\n", mem); else { if(i % BLOCKSIZE) writeToLogFile("delete error: %p\n", mem); TakenSize[i / BLOCKSIZE] = 0; } #endif }
/* Select && Connect to an Access Point */ int selectAccessPoint(int selected) { int err; int stateLast = -1; int state; /* Connect using the first profile */ err = sceNetApctlConnect(selected); if (err != 0) { writeToLogFile("Error, sceNetApctlConnect returns %08X\n", err); wifiError = 1; return -1; } //printf("Connecting...\n"); while(1) { err = sceNetApctlGetState(&state); if(err != 0) { writeToLogFile("sceNetApctlGetState returns $%x\n", err); wifiError = 1; break; } if(state > stateLast) { //printf("Connection state %d of 4\n", state); stateLast = state; } /* Connected with IP Address*/ if(state == 4) break; /* Wait 50 ms before polling again */ sceKernelDelayThread(50*1000); } /* Now obtain IP Address */ while(1) { if(sceNetApctlGetInfo(8, AccessPoints[selected-1].pInfo) == 0) { AccessPoints[selected-1].color = DARK_GREEN; addPSP("SamuraiX", "11:11:11:11", "22:22:22:22", AccessPoints[selected-1].pInfo->ip, 5060, "88:88:88:88", "99:99:99:99", AccessPoints[selected-1].pInfo->ip, 5060, 0); break; } sceKernelDelayThread(1000 * 1000); } if(err != 0) { wifiError = 1; return -1; } return 1; }
int command(int input, int first, int output) { int status = 0; pid = fork(); if (pid == -1) { int errnum = errno; perror("fork"); writeToLogFile(lineBCKP, "fork", errnum); exit(EXIT_FAILURE); } else if (pid == 0) { if (first == 1 && input == 0 && output == STANDARD_OUTPUT) { // only command } else if (input != 0 && output == STANDARD_OUTPUT) { // last command dup2(input, STDIN_FILENO); close(input); } else if (input == 0 && output != STANDARD_OUTPUT) { dup2(output, STDOUT_FILENO); close(output); } else if (input != 0 && output != STANDARD_OUTPUT) { dup2(input, STDIN_FILENO); dup2(output, STDOUT_FILENO); close(input); close(output); } //closeFile(); if (execvp( args[0], args) == -1) { int errnum = errno; perror("execvp failed"); writeToLogFile(lineBCKP, "execvp failed", errnum); exit(EXIT_FAILURE); // If child fails } } else { int w = waitpid(pid, &status, 0 ); if(w == -1) { int errnum = errno; perror("Child process terminated with errors"); writeToLogFile(lineBCKP, "Child process terminated with errors", errnum); exit(EXIT_FAILURE); } } closeFile(); if (input != 0) close(input); if (output != 1) close(output); return STANDARD_INPUT; }
void loggerServer(int level, char *s1, char *s2, char* clientIp) { char *levelString; switch (level) { case LOG_EMERG: levelString = "emergency"; break; case LOG_ALERT: levelString = "alert"; break; case LOG_CRIT: levelString = "critical"; break; case LOG_ERR: levelString = "error"; break; case LOG_WARNING: levelString = "warning"; break; case LOG_NOTICE: levelString = "notice"; break; case LOG_INFO: levelString = "info"; break; case LOG_DEBUG: levelString = "debug"; break; } char *logMessage; getDateTimeLog(dateTimeLog, SIZEOFDATETIMELOG); //if server error if (clientIp == NULL) { asprintf(&logMessage, "[%s] [%s] %s %s\n", dateTimeLog, levelString, s1, s2); } //else client error else { asprintf(&logMessage, "[%s] [%s] [client %s] %s %s\n", dateTimeLog, levelString, clientIp, s1, s2); } //if is syslog if (sc.customLog == NULL) { syslog(level, "%s", logMessage); } else { //if error has occured, or any important notification, look above codes from syslog if (level <= 3) writeToLogFile(sc.customLog, logMessage, 1); else writeToLogFile(sc.customLog, logMessage, 0); } free(logMessage); }
void *checkAlloc(void *ptr, size_t size, const char *func, const char *file, int line) { if (size > 0 && ptr == NULL) { writeToLogFile("\n\n********** An Error Occurred **********" "\n* Shutting Down *\n\n"); writeToLogFile("Out of memory!\n"); writeToLogFile("Allocation of size %i failed in function '%s' at %s:%i.\n", size, func, file, line); #ifndef WIN writeToLogFile("Memory usage at exit: %u\n", mallinfo().arena); #endif borExit(2); } return ptr; }
void * UpperMalloc(int size) { #ifndef WIZ int i = 0; int j = 1; ReDo: for(; TakenSize[i]; i += TakenSize[i]); if(i >= 0x2000000 / BLOCKSIZE) { writeToLogFile("UpperMalloc out of mem!"); return NULL; } int BSize = (size - 1) / BLOCKSIZE + 1; for(j = 1; j < BSize; j++) { if(TakenSize[i + j]) { i += j; goto ReDo; //OMG Goto, kill me. } } TakenSize[i] = BSize; void* mem = ((char*)UpperMem) + i * BLOCKSIZE; memset(mem, 0, size); return mem; #else return NULL; #endif }
int InitMemPool() { int uRam = 1; //Try to apply MMU hack. int mmufd = open("/dev/mmuhack", O_RDWR); if(mmufd < 0) { system("/sbin/insmod mmuhack.o"); mmufd = open("/dev/mmuhack", O_RDWR); } if(mmufd < 0) { writeToLogFile("MMU hack failed"); return 0; } else close(mmufd); Uppermemfd = open("/dev/mem", O_RDWR); if((UpperMem = mmap(0, 0x2000000, PROT_READ | PROT_WRITE, MAP_SHARED, Uppermemfd, 0x2000000))) uRam = 2; memset(TakenSize, 0, sizeof(TakenSize)); SetTaken(0x3000000, 0x80000); // Video decoder (you could overwrite this, but if you // don't need the memory then be nice and don't) SetTaken(0x3101000, 153600); // Primary frame buffer SetTaken(0x3381000, 153600); // Secondary frame buffer (if you don't use it, uncomment) SetTaken(0x3600000, 0x8000); // Sound buffer return uRam; }
void LogMgr::writeEndLine() { std::string endString; endString = "######### ---------------------- #########"; endString += "\n"; endString += "\n"; writeToLogFile(endString); }
// IMPORTANT: The two places this function is called from wrap the code in the tag critical section (m_pTagCriticalSection), // so that makes this call thread safe. If you call this from anywhere else, make sure you wrap it in that critical section. //------------------------------------------------------------------------------------------------------------------------------------ void LogMgr::outputFinalBufferToLogs(const std::string& finalBuffer, const std::string& tag, unsigned char flags) { if ((flags && LOGFLAG_WRITE_TO_LOG_FILE) > 0) writeToLogFile(finalBuffer); if ((flags && LOGFLAG_WRITE_TO_DEBUGGER) > 0) ::OutputDebugStringA(finalBuffer.c_str()); if ((flags && LOGFLAG_WRITE_TO_CONSOLE) > 0) writeToConsole(tag, finalBuffer); }
/* Simple user thread to do the real work */ int startWifi() { NetThid = sceKernelCreateThread("net_thread", net_thread, 0x12, 0x20000, PSP_THREAD_ATTR_USER, NULL); if(NetThid < 0) { writeToLogFile("Error, could not create thread\n"); wifiError = 1; return -1; } sceKernelStartThread(NetThid, 0, NULL); return 1; }
//Returns the size of a UpperMalloced block. int GetUpperSize(void* mem) { #ifndef WIZ int i = (((int)mem) - ((int)UpperMem)); if(i < 0 || i >= 0x2000000) { writeToLogFile("GetUpperSize of not UpperMalloced mem: %p\n", mem); return -1; } return TakenSize[i / BLOCKSIZE] * BLOCKSIZE; #else return 0; #endif }
void loggerSuccess(char* method, Client *client) { //date time int Common logInfo format for logger getDateTimeCLF(dateTimeCLF, SIZEOFDATETIMECLF); if (sc.customLog == NULL) { syslog(LOG_INFO, "%s - - [%s] \"%s %s %s\" %s %lu\n", client->httpRes.IPAddress, dateTimeCLF, client->httpReq.method, client->httpReq.uri, client->httpReq.httpVersion, method, client->httpRes.contentLength); } else { char *logMessage; //creates log message in CLF (Common logFile format) asprintf(&logMessage, "%s - - [%s] \"%s %s %s\" %s %lu\n", client->httpRes.IPAddress, dateTimeCLF, client->httpReq.method, client->httpReq.uri, client->httpReq.httpVersion, method, client->httpRes.contentLength); writeToLogFile(sc.customLog, logMessage, 0); //free memory due of use asprintf function free(logMessage); } }
void openFile() { if(redir_num == OUTPUT_REDIRECTION_NUM) { fp = fopen(fileName, "w"); fd = fileno(fp); } else if(redir_num == APPEND_OUTPUT_REDIRECTION_NUM) { fp = fopen(fileName, "a"); fd = fileno(fp); } else if(redir_num == INPUT_REDIRECTION_NUM) { fp = fopen(fileName, "r"); if(fp == NULL) { int errnum = errno; perror("Error opening file"); writeToLogFile(lineBCKP, fileName, errnum); exit(EXIT_FAILURE); } fd = fileno(fp); } }
void LogMgr::init(const char* loggingConfigFilename) { std::string iniString; iniString = "######### ------ XTEN LOG ------ #########"; iniString += "\n"; writeToLogFile(iniString); if (loggingConfigFilename) { TiXmlDocument loggingConfigFile(loggingConfigFilename); if (loggingConfigFile.LoadFile()) { TiXmlElement* pRoot = loggingConfigFile.RootElement(); if (!pRoot) return; for (TiXmlElement* pNode = pRoot->FirstChildElement(); pNode; pNode = pNode->NextSiblingElement()) { unsigned char flags = 0; std::string tag(pNode->Attribute("tag")); int debugger = 0; pNode->Attribute("debugger", &debugger); if (debugger) flags |= LOGFLAG_WRITE_TO_DEBUGGER; int logFile = 0; pNode->Attribute("file", &logFile); if (logFile) flags |= LOGFLAG_WRITE_TO_LOG_FILE; int console = 0; pNode->Attribute("console", &console); if (console) flags |= LOGFLAG_WRITE_TO_CONSOLE; setDisplayFLags(tag, flags); } } } }
void startServer() { int i, sock; struct sockaddr_in server; struct sockaddr_in client; char buffer[BUFFER_SIZE]; unsigned int buffer_len = 0; unsigned int client_len = 0; unsigned int server_len = 0; int bytesTX = 0; int bytesRX = 0; uint16_t port; if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) goto Error; port = SERVER_PORT; /* Create sockaddr structure */ memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(port); /* Bind the socket */ server_len = sizeof(server); if(bind(sock, (struct sockaddr *) &server, server_len) < 0) goto Error; /* Run until cancelled */ while(wifiMode) { /* Keeps Wifi from turning off */ scePowerTick(1); /* Receive datagram from client */ client_len = sizeof(client); bytesRX = recvfrom(sock, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &client, (void*) &client_len); if(bytesRX < 0) { writeToLogFile("Critical Error failed to receive message from client!\n"); wifiError = 1; } //if(bytesRX > 0) pspList[1].buttons = atol(buffer); //if(bytesRX == 3) //{ // strcat(buffer,"\n\nAppended!!!"); // strcat(buffer,"\n\nI"); // strcat(buffer,"\n\nLove My Muffin and My Chloe!!!"); // strcat(buffer,"\n\nXoXoXoXoXoXoX"); //} //sprintf(buffer, "%lu", getPad(0)); buffer_len = strlen(buffer); if(buffer_len > 0) writeToLogFile("Buffer: %lu\n",buffer); bytesTX = sendto(sock, buffer, buffer_len, 0, (struct sockaddr *) &client, sizeof(client)); if(bytesTX < buffer_len) { writeToLogFile("Data Sent: %d\nBuffer Size: %d\nBuffer Data: %s\n", bytesTX, buffer_len, buffer); wifiError = 1; } for(i=0; i<buffer_len; i++) buffer[i] = 0; } Error: wifiError = 1; return; }
int main(int argc,char *argv[]) { const int BUF_SIZE=1024; const int CMDLEN=50; int sockfd;//atoi(argv[1]); struct FTS fts; char command[BUF_SIZE]; int index; struct COMMAND cmd; char user[CMDLEN]; char arg[BUF_SIZE]; const char* prompt="FTS> "; int numlogs,logfilesize; struct CONFIG_READER cfg; struct LOGGER lg; char ip[CMDLEN]; llg=≶ signal(SIGKILL,handle); createConfigReader(&cfg,"config.cfg"); //printf("after config reader\n"); getConfigValue("NLOGFILES",arg,&cfg); numlogs=atoi(arg); getConfigValue("LOGFILESIZE",arg,&cfg); logfilesize=atoi(arg); destroyConfigReader(&cfg); //printf("after destroy config reader\n"); createLogger(&lg,"client_log","./client_logs",numlogs,logfilesize); //printf("after create logger\n"); strncpy(arg,argv[1],BUF_SIZE); //printf("arg: %s\n",arg); strncpy(user,strtok(arg,"@"),BUF_SIZE); //printf("user: %s\n",user); strncpy(ip,strtok(NULL,"@"),BUF_SIZE); //printf("before connections\n"); fts.sockfd=sockfd=connectToServer(ip); fts.mode=FTS_MODE_ASCII; //printf("sock: %d\n",sockfd); //printf("user: %s\n",user); write(sockfd,user,strlen(user)); read(sockfd,&index,sizeof(index)); //printf("%d\n",index); if(index==-1) { puts("user not found"); exit(0); } puts("connected successfully"); while(1) { printf("%s",prompt); fflush(stdout); fgets(command,BUF_SIZE,stdin); while(command[0]=='\0' || command[0]==' ' || command[0]=='\n') fgets(command,BUF_SIZE,stdin); if(command[strlen(command)-1]=='\n') command[strlen(command)-1]='\0'; //printf("cmd: %s\n",command); writeToLogFile(command,&lg); fillCommandObject(command,&cmd); index=getCommandIndex(&cmd,(struct CMD_MAP *)&cmd_map); if(index < 0) { puts("Command not found"); continue; } cmd_map[index].handler(&cmd,&fts); destroyCommandObject (&cmd); } return (0); }
int pipeCommand(int input, int first, int last) { int pipettes[2]; /* Invoke pipe */ pipe( pipettes ); pid = fork(); if (pid == -1) { int errnum = errno; perror("fork"); writeToLogFile(lineBCKP, "fork", errnum); exit(EXIT_FAILURE); } else if (pid == 0) { if (first == 1 && last == 0 && input == 0) { // First command dup2( pipettes[PIPE_WRITE], STDOUT_FILENO ); close(pipettes[PIPE_WRITE]); } else if (first == 1 && last == 0 && input != 0) { // First command dup2(input, STDIN_FILENO); dup2( pipettes[PIPE_WRITE], STDOUT_FILENO ); close(input); close(pipettes[PIPE_WRITE]); } else if (first == 0 && last == 0 && input != 0) { // Middle command with input redirection dup2(input, STDIN_FILENO); dup2(pipettes[PIPE_WRITE], STDOUT_FILENO); close(input); close(pipettes[PIPE_WRITE]); } else if (first == 0 && last == 0 && input == 0) { // Middle command without input redirection //dup2(input, STDIN_FILENO); dup2(pipettes[PIPE_WRITE], STDOUT_FILENO); close(pipettes[PIPE_WRITE]); } else { // Last command dup2( input, STDIN_FILENO ); close(input); } //closeFile(); if (execvp( args[0], args) == -1) { int errnum = errno; perror("execvp failed"); writeToLogFile(lineBCKP, "execvp failed", errnum); exit(EXIT_FAILURE); // If child fails } } else { int w = waitpid(pid, &status, WNOHANG ); if(w == -1) { int errnum = errno; perror("Child process terminated with errors"); writeToLogFile(lineBCKP, "Child process terminated with errors", errnum); exit(EXIT_FAILURE); } } closeFile(); if (input != 0) close(input); // Nothing more needs to be written close(pipettes[PIPE_WRITE]); // If it's the last command, nothing more needs to be read if (last == 1) close(pipettes[PIPE_READ]); return pipettes[PIPE_READ]; }
int main(int argc, char *argv[]){ char line[1024] = "string"; char line2[1024] = "string"; int n = 0; /* number of calls to 'command' */ int i = 0; pid_t pid = 0; /* install SIGCHLD handler */ signal(SIGCHLD, sigchld_handler); //signal(SIGCHLD, SIG_IGN); // prehaps use this? signal(SIGINT, SIG_IGN); // ignore CTRL - C printf("Press t to test or any other key to continue...\n"); printf("$ "); if(!fgets(line, 1024, stdin)){ printf("\n"); exit(0); } while(1) { if(line[0] == 't'&& line[1] == '\n'){ //strcpy(line ,"kate & ls | grep l\n"); //check mixed commands //check_all_errorcheck(); //printf("\n"); //check_parse(); //check_run(); //printf("\n"); printf("No tests chosen in source file\n"); break; } else { clearString(line); clearString(line2); fflush(stdin); fflush(stdout); printf("$ "); if(!fgets(line, 1024, stdin)){ printf("\n"); break; } strcpy(line2, line); int c = chck_general_error(line2); if(c == -1){ perror("invalid input"); writeToLogFile(line, "invalid input", EIO); printf("\n"); continue; } } if(strcmp(EXITN,line) == 0){ break; } strcpy ( lineBCKP, line); int input = 0; int first = 1; int trailingAmp = 0; int status = 0; int delimitFlag = 0; char* cmd = line; char* next = strpbrk(cmd, DELIMITERS); /* Find first '|' or '&' */ while (next != NULL) { /* 'next' points to '|' or '&' */ if(*next == '|') delimitFlag = PIPE_NUM; if(*next == '&'){ delimitFlag = AMPERSAND_NUM; if(*(next + 1) == '\n'){ trailingAmp = 1; //printf("trailing amp\n"); } } *next = '\0'; input = run(cmd, input, first, 0, &n, delimitFlag); cmd = next + 1; next = strpbrk(cmd, DELIMITERS); /* Find next '|' or '&' */ first = 0; } if(!trailingAmp) input = run(cmd, input, first, 1, &n, NO_BLOCK_ENDING); //cleanup(n); n = 0; } }