int EsifLogFile_Open(EsifLogType type, const char *filename, int append) { int rc=0; char fullpath[MAX_PATH]={0}; char mode[3] = {(append ? 'a' : 'w'), 0, 0}; esif_ccb_write_lock(&g_EsifLogFile[type].lock); if (g_EsifLogFile[type].handle != NULL) esif_ccb_fclose(g_EsifLogFile[type].handle); EsifLogFile_GetFullPath(fullpath, sizeof(fullpath), filename); #ifdef ESIF_ATTR_OS_WINDOWS mode[1] = 'c'; g_EsifLogFile[type].handle = _fsopen(fullpath, mode, _SH_DENYWR); if (g_EsifLogFile[type].handle == NULL) rc = errno; #else rc = esif_ccb_fopen(&g_EsifLogFile[type].handle, fullpath, mode); #endif if (rc == 0) { esif_ccb_free(g_EsifLogFile[type].filename); g_EsifLogFile[type].filename = esif_ccb_strdup((char *)fullpath); } esif_ccb_write_unlock(&g_EsifLogFile[type].lock); return rc; }
// // Write to Transaction Log - Used for debugging only // NOTE: This version is just a dumb text log, not a recoverable transaction log // static void DataVault_WriteLog( DataVaultPtr self, esif_string action, esif_string nameSpace, EsifDataPtr path, esif_flags_t flags, EsifDataPtr value ) { #ifndef ESIF_CONFIG_LOG UNREFERENCED_PARAMETER(self); UNREFERENCED_PARAMETER(action); UNREFERENCED_PARAMETER(nameSpace); UNREFERENCED_PARAMETER(path); UNREFERENCED_PARAMETER(flags); UNREFERENCED_PARAMETER(value); #else struct esif_ccb_file log_file = {0}; FILE *filePtr = NULL; time_t now; char theTime[30]; UNREFERENCED_PARAMETER(self); if (!flags) { return; } esif_build_path(log_file.filename, sizeof(log_file.filename), ESIF_PATHTYPE_DV, nameSpace, ESIFDV_LOGFILEEXT); filePtr = esif_ccb_fopen(log_file.filename, "ab", NULL); if (NULL == filePtr) { return; } time(&now); esif_ccb_ctime(theTime, sizeof(theTime), &now); esif_ccb_fprintf(filePtr, "%.24s: %-6s %s flags=%d", theTime, action, (EsifString)path->buf_ptr, flags); if (NULL != value) { UInt32 ch; esif_ccb_fprintf(filePtr, " type=%d buf_len=%d data_len=%d buf_ptr=%s", value->type, value->buf_len, value->data_len, (value->buf_ptr ? "0x" : "NULL")); for (ch = 0; NULL != value->buf_ptr && ch < value->data_len; ch++) esif_ccb_fprintf(filePtr, "%02X", (int)((UInt8*)(value->buf_ptr))[ch]); } esif_ccb_fprintf(filePtr, "\r\n"); fclose(filePtr); #endif }
static void esif_ws_http_send_401 (int fd) { char buffer[256]; char tmpbuffer[128]; char *fileType = "text/html"; FILE *file_fp = NULL; int len; struct stat st; int ret; char *fileName = "error.html"; printf("fileName :%s\n", fileName); esif_ccb_fopen(&file_fp, fileName, "r"); if (NULL == file_fp) { printf("failed to open file: \n"); return; } if (esif_ccb_stat(fileName, &st) != 0) { printf("Could not stat file descriptor \n"); return; } len = (long)fseek(file_fp, (off_t)0, SEEK_END); (void)fseek(file_fp, (off_t)0, SEEK_SET); (void)esif_ccb_sprintf(256, buffer, (char*)"HTTP/1.1 401 Unauthorized\n" "Server: server/%d.0\nLast-Modified: %s\nDate: %s\n" "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n", VERSION, esif_ws_http_time_stamp(st.st_mtime, tmpbuffer), esif_ws_http_time_stamp(time(0), tmpbuffer), fileType, (long)st.st_size); (void)send(fd, buffer, (int)esif_ccb_strlen(buffer), 0); while ((ret = (int)fread(buffer, 1, 256, file_fp)) > 0) (void)send(fd, buffer, ret, 0); fclose(file_fp); }
void esif_memtrace_exit() { struct memalloc_s *mem = NULL; char tracefile[MAX_PATH] = {0}; FILE *tracelog = NULL; esif_build_path(tracefile, sizeof(tracefile), ESIF_PATHTYPE_LOG, "memtrace.txt", NULL); esif_pathlist_exit(); esif_ccb_write_lock(&g_memtrace.lock); mem = g_memtrace.allocated; g_memtrace.allocated = NULL; esif_ccb_write_unlock(&g_memtrace.lock); CMD_OUT("MemTrace: Allocs=" ATOMIC_FMT " Frees=" ATOMIC_FMT "\n", atomic_read(&g_memtrace.allocs), atomic_read(&g_memtrace.frees)); if (!mem) { goto exit; } CMD_OUT("\n*** MEMORY LEAKS DETECTED ***\nFor details see %s\n", tracefile); esif_ccb_fopen(&tracelog, tracefile, "a"); if (tracelog) { time_t now = time(NULL); char timestamp[MAX_CTIME_LEN] = {0}; esif_ccb_ctime(timestamp, sizeof(timestamp), &now); fprintf(tracelog, "\n*** %.24s: MEMORY LEAKS DETECTED (%s) ***\n", timestamp, ESIF_UF_VERSION); } while (mem) { struct memalloc_s *node = mem; if (tracelog) { fprintf(tracelog, "[%s @%s:%d]: (%lld bytes) %p\n", mem->func, mem->file, mem->line, (long long)mem->size, mem->mem_ptr); } mem = mem->next; native_free(node->mem_ptr); native_free(node); } if (tracelog) { esif_ccb_fclose(tracelog); } exit: esif_ccb_lock_uninit(&g_memtrace.lock); }
static int esif_ws_http_server_static_pages ( char *buffer, char *resource, int fd, ssize_t ret, char *fileType ) { struct stat st; char tmpbuffer[128]; char file_to_open[1000]; FILE *file_fp = NULL; #define MAX_SIZE 200 esif_ccb_memcpy(file_to_open, g_server_root, esif_ccb_strlen(g_server_root, MAX_SIZE)); file_to_open[esif_ccb_strlen(g_server_root, MAX_SIZE)] = '\0'; // printf("file to open after esif_ccb_memcpy: %s\n", file_to_open); // printf("resource : %s\n", resource); #ifdef ESIF_ATTR_OS_WINDOWS strcat_s((esif_string)file_to_open, 1000, resource); esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"rb"); #else strcat((esif_string)file_to_open, resource); esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"r"); #endif printf("file to open: %s\n", file_to_open); printf("file type: %s\n", fileType); if (NULL == file_fp) { printf("failed to open file: %s\n", file_to_open); return 404; } if (esif_ccb_stat(file_to_open, &st) != 0) { printf("Could not stat file descriptor \n"); fclose(file_fp); return 404; } (long)fseek(file_fp, (off_t)0, SEEK_END); (void)fseek(file_fp, (off_t)0, SEEK_SET); if (!strcmp(fileType, "text/xml")) { (void)esif_ccb_sprintf(BUFFER_LENGTH, buffer, (char*)"HTTP/1.1 200 OK\n<?xml version==\"1.0\" encoding=\"utf-8\"?>\n" "Server: server/%d.0\n" "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n", VERSION, fileType, (long)st.st_size); } else { (void)esif_ccb_sprintf(BUFFER_LENGTH, buffer, (char*)"HTTP/1.1 200 OK\n" "Server: server/%d.0\nLast-Modified: %s\nDate: %s\n" "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n", VERSION, esif_ws_http_time_stamp(st.st_mtime, tmpbuffer), esif_ws_http_time_stamp(time(0), tmpbuffer), fileType, (long)st.st_size); } (void)send(fd, buffer, (int)esif_ccb_strlen(buffer, MAX_SIZE), 0); while ((ret = (int)fread(buffer, 1, BUFFER_LENGTH, file_fp)) > 0) (void)send(fd, buffer, ret, 0); fclose(file_fp); return 0; }
static void esif_ws_cgi_redirect_output ( char *script, int fd ) { int current_out; char buffer[BUFFER_LENGTH]; int ret; FILE *dataFile = NULL; #define MAX_SIZE 100 printf("esif_ws_cgi_redirect_output-> full_script_name: %s\n", script); current_out = esif_ccb_dup(1); if (current_out == -1) { perror("_dup( 1 ) failure"); exit(1); } esif_ccb_fopen(&dataFile, "data", "w"); if (NULL == dataFile) { printf("failed to open file in writing mode \n"); return; } if (-1 == esif_ccb_dup2(dataFile, 1)) { perror("Can't _dup2 stdout"); exit(1); } printf("Execute the script\n"); system(script); fflush(stdout); fclose(dataFile); esif_ccb_dup3(current_out, 1); esif_ccb_flushall(); esif_ccb_fopen(&dataFile, "data", "r"); if (NULL == dataFile) { printf("failed to open file in readig mode: \n"); return; } (long)fseek(dataFile, (off_t)0, SEEK_END); (void)fseek(dataFile, (off_t)0, SEEK_SET); (void)esif_ccb_sprintf(BUFFER_LENGTH, buffer, (char*)"HTTP/1.1 200 OK\n"); (void)send(fd, buffer, (int)esif_ccb_strlen(buffer, MAX_SIZE), 0); while ((ret = (int)fread(buffer, 1, BUFFER_LENGTH, dataFile)) > 0) (void)send(fd, buffer, ret, 0); fclose(dataFile); esif_ws_server_initialize_clients(); esif_ws_http_set_login_requested(1); esif_ws_http_set_authenticated(1); }
// Initialize Pathname List enum esif_rc esif_pathlist_init(esif_string paths) { static esif_pathmap pathmap[] = { { "HOME", ESIF_PATHTYPE_HOME }, { "TEMP", ESIF_PATHTYPE_TEMP }, { "DV", ESIF_PATHTYPE_DV }, { "LOG", ESIF_PATHTYPE_LOG }, { "BIN", ESIF_PATHTYPE_BIN }, { "LOCK", ESIF_PATHTYPE_LOCK }, { "EXE", ESIF_PATHTYPE_EXE }, { "DLL", ESIF_PATHTYPE_DLL }, { "DPTF", ESIF_PATHTYPE_DPTF }, { "DSP", ESIF_PATHTYPE_DSP }, { "CMD", ESIF_PATHTYPE_CMD }, { "UI", ESIF_PATHTYPE_UI }, { NULL } }; static int num_paths = (sizeof(pathmap)/sizeof(*pathmap)) - 1; esif_string *pathlist = NULL; enum esif_rc rc = ESIF_OK; if (!g_pathlist.initialized) { char *buffer = NULL; char *filename = ESIF_PATHLIST_FILENAME; FILE *fp = NULL; struct stat st={0}; // Use pathlist file, if it exists if (esif_ccb_stat(filename, &st) == 0 && esif_ccb_fopen(&fp, filename, "rb") == 0) { if ((buffer = (char *) esif_ccb_malloc(st.st_size + 1)) != NULL) { if (esif_ccb_fread(buffer, st.st_size, sizeof(char), st.st_size, fp) != (size_t)st.st_size) { rc = ESIF_E_IO_ERROR; } } esif_ccb_fclose(fp); } // Otherwise, use default pathlist else if (buffer == NULL && paths != NULL) { buffer = esif_ccb_strdup(paths); } if ((buffer == NULL) || ((pathlist = (esif_string *)esif_ccb_malloc(num_paths * sizeof(esif_string))) == NULL)) { esif_ccb_free(buffer); buffer = NULL; rc = ESIF_E_NO_MEMORY; } // Parse key/value pairs into pathlist if (rc == ESIF_OK && buffer != NULL) { char *ctxt = 0; char *keypair = esif_ccb_strtok(buffer, "\r\n", &ctxt); char *value = 0; int id=0; g_pathlist.initialized = 1; g_pathlist.pathmap = pathmap; g_pathlist.num_paths = num_paths; g_pathlist.pathlist = pathlist; while (keypair != NULL) { value = esif_ccb_strchr(keypair, '='); if (value) { *value++ = 0; for (id = 0; id < g_pathlist.num_paths && g_pathlist.pathmap[id].name; id++) { if (esif_ccb_stricmp(keypair, g_pathlist.pathmap[id].name) == 0) { esif_pathlist_set(g_pathlist.pathmap[id].type, value); break; } } } keypair = esif_ccb_strtok(NULL, "\r\n", &ctxt); } } esif_ccb_free(buffer); } return rc; }
static int esif_ws_http_process_static_pages ( ClientRecordPtr connection, char *buffer, char *resource, ssize_t ret, char *fileType ) { struct stat st={0}; char tmpbuffer[128]={0}; char file_to_open[MAX_PATH]={0}; char content_disposition[MAX_PATH]={0}; FILE *file_fp = NULL; esif_build_path(file_to_open, sizeof(file_to_open), ESIF_PATHTYPE_UI, resource, NULL); esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"rb"); // Log file workaround: If not found in HTML folder, look in LOG folder if (NULL == file_fp) { char logpath[MAX_PATH]={0}; esif_build_path(logpath, sizeof(logpath), ESIF_PATHTYPE_LOG, resource, NULL); esif_ccb_fopen(&file_fp, (esif_string)logpath, (esif_string)"rb"); if (NULL != file_fp) esif_ccb_strcpy(file_to_open, logpath, sizeof(file_to_open)); } ESIF_TRACE_DEBUG("HTTP: file=%s, type=%s\n", file_to_open, fileType); if (NULL == file_fp) { ESIF_TRACE_DEBUG("failed to open file: %s\n", file_to_open); return 404; } if (esif_ccb_stat(file_to_open, &st) != 0) { ESIF_TRACE_DEBUG("Could not stat file descriptor \n"); fclose(file_fp); return 404; } fseek(file_fp, (off_t)0, SEEK_END); fseek(file_fp, (off_t)0, SEEK_SET); // Add Content-Disposition header to prompt user with Save-As Dialog if unknown file type if (esif_ccb_strcmp(fileType, UNKNOWN_MIME_TYPE) == 0) { esif_ccb_sprintf(sizeof(content_disposition), content_disposition, "Content-Disposition: attachment; filename=\"%s\";\n", resource); } esif_ccb_sprintf(WS_BUFFER_LENGTH, buffer, "HTTP/1.1 200 OK\n" "Server: ESIF_UF/%s\n" "Last-Modified: %s\n" "Date: %s\n" "Content-Type: %s\n" "Content-Length: %ld\n" "%s" "Connection: close\n" "\n", ESIF_UF_VERSION, esif_ws_http_time_stamp(st.st_mtime, tmpbuffer), esif_ws_http_time_stamp(time(0), tmpbuffer), fileType, (long)st.st_size, content_disposition); send(connection->socket, buffer, (int)esif_ccb_strlen(buffer, WS_BUFFER_LENGTH), ESIF_WS_SEND_FLAGS); while ((ret = (int)fread(buffer, 1, WS_BUFFER_LENGTH, file_fp)) > 0) { send(connection->socket, buffer, (int)ret, ESIF_WS_SEND_FLAGS); } fclose(file_fp); return 0; }