int main(int argc, char *argv[]) { struct code_chunks *code; code = get_code_chunks(); if (code == NULL) return -1; dump_chunks(code); do_test_run(code); cleanup_chunk(code); return 0; }
static void remove_chunk(void *p, char *cl) { int i; if (!debug_memory) return; for (i = 0; i < CHUNKS_MAX; i++) if (chunks[i].p == p) break; if (i == CHUNKS_MAX) { mrlog("Can't find chunk %p (%s)", p, cl); dump_chunks(); mrlog("Continuing even though I can't find chunk %p (%s)", p, cl); } if (check_chunk(i)) mrexit("remove_chunk: check not ok", EXIT_FAILURE); if (debug >= 3) { mrlog("Removing chunk %p (%s) from slot %d", p, chunks[i].cl, i); } chunks[i].p = NULL; }
static void store_chunk(void *p, size_t n, char *cl) { int i; if (!debug_memory) return; for (i = 0; i < CHUNKS_MAX; i++) if (chunks[i].p == NULL) break; if (i == CHUNKS_MAX) { mrlog("No empty chunk slot for %p (%s), exiting", p, cl); dump_chunks(); mrexit("store_chunk is out of slots", EXIT_FAILURE); } if (debug >= 3) mrlog("Storing chunk %p (%s) of %ld bytes in slot %d", p, cl, (long)n, i); chunks[i].p = p; chunks[i].n = n; mrlog("In store_chunk: i = %d", i); strlcpy(chunks[i].cl, cl, 20); mrlog("In store_chunk: i = %d", i); memcpy(p+n-PATTERN_SIZE, big_pattern, PATTERN_SIZE); }
/* Restart download from incomplete ___ARESTRA___ file. This will fail if the * file is not found/corrupt/etc. */ as_bool as_download_restart (ASDownload *dl, const char *path) { if (dl->state != DOWNLOAD_NEW) { assert (dl->state == DOWNLOAD_NEW); return FALSE; } if (!path) { assert (path); return FALSE; } /* copy file path */ dl->path = strdup (path); /* set filename after __ARESTRA__prefix */ dl->filename = as_get_filename (dl->path); if (strncmp (dl->filename, AS_DOWNLOAD_INCOMPLETE_PREFIX, strlen (AS_DOWNLOAD_INCOMPLETE_PREFIX)) == 0) dl->filename += strlen (AS_DOWNLOAD_INCOMPLETE_PREFIX); /* make sure the file exists */ if (!as_file_exists (dl->path)) { AS_ERR_1 ("Incomplete file \"%s\" does not exist.", dl->path); free (dl->path); dl->path = NULL; dl->filename = NULL; return FALSE; } /* open file */ if (!(dl->fp = fopen (dl->path, "r+b"))) { AS_ERR_1 ("Unable to open download file \"%s\" for writing", dl->path); free (dl->path); dl->path = NULL; dl->filename = NULL; return FALSE; } /* read download state from file */ if (!as_downstate_load (dl)) { AS_ERR_1 ("Unable to load state for incomplete download file \"%s\"", dl->path); fclose (dl->fp); dl->fp = NULL; free (dl->path); dl->path = NULL; dl->filename = NULL; return FALSE; } AS_HEAVY_DBG_3 ("Loaded state for \"%s\", size: %u, received: %u", dl->filename, dl->size, dl->received); #ifdef HEAVY_DEBUG assert (verify_chunks (dl)); AS_HEAVY_DBG ("Chunk state after restoring download:"); dump_chunks (dl); AS_HEAVY_DBG ("Connection state after restoring download:"); dump_connections (dl); #endif /* raise callback with state set by as_downstate_load */ if (!download_set_state (dl, dl->state, TRUE)) return FALSE; /* start things off if we are in active state */ if (dl->state == DOWNLOAD_ACTIVE) download_maintain (dl); return TRUE; }
/* * The heart of the download system. It is called whenever there are new * sources, finished chunks, etc. * Merges complete chunks and tries to assign sources to inactive ones. If * there are more sources than chunks the chunks are split up. */ static void download_maintain (ASDownload *dl) { if (dl->state != DOWNLOAD_ACTIVE) { /* Must not happen. */ assert (dl->state == DOWNLOAD_ACTIVE); return; } #ifdef VERIFY_CONN_LIST /* Verify integrity of connection list */ verify_connections (dl); #endif /* Verify integrity of chunk list. */ if (!verify_chunks (dl)) { AS_ERR_1 ("Corrupted chunk list detected for \"%s\"", dl->filename); /* Fail download */ download_failed (dl); assert (0); return; } /* Clean up chunks. */ if (!consolidate_chunks (dl)) { AS_ERR_1 ("Consolidating chunks failed for \"%s\"", dl->filename); /* Fail download */ download_failed (dl); assert (0); return; } #ifdef CHUNK_DEBUG AS_HEAVY_DBG ("Chunk state after consolidating:"); dump_chunks (dl); AS_HEAVY_DBG ("Connection state after consolidating:"); dump_connections (dl); #endif /* Is the download complete? */ if (((ASDownChunk *)dl->chunks->data)->received == dl->size) { /* Download complete */ download_finished (dl); return; } /* Download not complete. Start more chunk downloads. */ if (!start_chunks (dl)) { /* This should be harmless. */ AS_WARN_1 ("Starting chunks failed for \"%s\"", dl->filename); } /* Check if we need more sources */ if (dl->conns == NULL) { /* TODO: start source search */ AS_ERR_1 ("FIXME: No more sources for \"%s\". Make me find more.", dl->filename); } }
void mrbig(void) { char *p; time_t t, lastrun; int sleeptime, i; char hostname[256]; DWORD hostsize; /* * install exception logging/stacktrace handler. * We need to resolve the symbol at run time because windows 2000 * does not have it. */ do { HMODULE dll; PVOID (*ptr) (ULONG First,PVECTORED_EXCEPTION_HANDLER Handler) = NULL; dll = LoadLibraryEx("kernel32.dll", NULL, 0); if (dll == NULL) break; ptr = (typeof(ptr))GetProcAddress(dll, "AddVectoredExceptionHandler"); if (ptr == NULL) break; ptr(1, VectoredExceptionHandler); mrlog("VectoredExceptionHandler handler has been installed"); } while(0); if (debug) { mrlog("mrbig()"); } for (i = 0; _environ[i]; i++) { startup_log("%s", _environ[i]); } for (;;) { if (debug) mrlog("main loop"); read_cfg("mrbig", cfgfile); readcfg(); t = time(NULL); strlcpy(now, ctime(&t), sizeof now); p = strchr(now, '\n'); if (p) *p = '\0'; hostsize = sizeof hostname; if (GetComputerName(hostname, &hostsize)) { for (i = 0; hostname[i]; i++) hostname[i] = tolower(hostname[i]); snprcat(now, sizeof now, " [%s]", hostname); } cpu(); check_chunks("after cpu test"); disk(); check_chunks("after disk test"); memory(); check_chunks("after memory test"); msgs(); check_chunks("after msgs test"); procs(); check_chunks("after procs test"); svcs(); check_chunks("after svcs test"); wmi(); check_chunks("after wmi test"); if (pickupdir[0]) ext_tests(); lastrun = t; t = time(NULL); if (t < lastrun) { mrlog("mainloop: timewarp detected, sleep for %d", mrloop); sleeptime = mrloop; } else { sleeptime = mrloop-(t-lastrun); } if (sleeptime < SLEEP_MIN) sleeptime = SLEEP_MIN; if (debug) mrlog("started at %d, finished at %d, sleep for %d", (int)lastrun, (int)t, sleeptime); clear_cfg(); if (debug) { dump_chunks(); check_chunks("after main loop"); } Sleep(sleeptime*1000); } }
int main(int argc, char **argv) { int i; char *p; startup_log("main()"); dirsep = '\\'; GetModuleFileName(NULL, cfgdir, sizeof cfgdir); startup_log("cfgdir = '%s'", cfgdir); p = strrchr(cfgdir, dirsep); if (p) *p = '\0'; cfgfile[0] = '\0'; snprcat(cfgfile, sizeof cfgfile, "%s%c%s", cfgdir, dirsep, "mrbig.cfg"); startup_log("cfgfile = '%s'", cfgfile); startup_log("SystemRoot = '%s'", getenv("SystemRoot")); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-c")) { i++; if (argv[i] == NULL) { fprintf(stderr, "No cfg file\n"); return EXIT_FAILURE; } } else if (!strcmp(argv[i], "-d")) { debug++; } else if (!strcmp(argv[i], "-m")) { debug_memory = 1; } else if (!strncmp(argv[i], "-i", 2)) { if (argv[i][2] == '\0') { install_service("MrBig", "Mr Big Monitoring Agent"); } else { install_service(argv[i]+2, argv[i]+2); } return 0; } else if (!strncmp(argv[i], "-u", 2)) { if (argv[i][2] == '\0') { delete_service("MrBig"); } else { delete_service(argv[i]+2); } return 0; } else if (!strcmp(argv[i], "-t")) { standalone = 1; } else { fprintf(stderr, "Bogus option '%s'\n", argv[i]); usage(); } } if (standalone) { mrbig(); return 0; } startup_log("We want to become a service"); service_main(argc, argv); dump_chunks(); check_chunks("just before exit"); dump_files(); return 0; }