int alder_smartall_test() { char *cp, *ra; #ifdef OLD_unix malloc_debug(2); #endif /* Allocate and chain together storage that's subject to the orphaned buffer check. */ ec(malloc(120)); ec(alloc(200)); ec(calloc(10, 4)); ra = alloc(60); strcpy(ra + 8, "Hello, there. This is data."); ra = realloc(ra, 100); ra = realloc(ra, 100); ra = realloc(ra, 2048); ec(realloc(ra, 55)); /* Allocate and chain some storage for which checking is disabled by the sm_static mechanism. */ sm_static(1); ra = malloc(10); ra = alloc(20); ra = calloc(30, sizeof(short)); ra = realloc(ra, 40); sm_static(0); /* Test the "actually" variants. */ ra = actuallymalloc(100); ra = actuallycalloc(10, sizeof(double)); ra = actuallyrealloc(ra, 15 * sizeof(double)); actuallyfree(ra); /* Produce orphan buffer listing. */ sm_dump(0); // sm_dump(Dumparam); /* Release chained buffers. */ while (bchain != NULL) { dc(); } /* Now verify that all have been released. */ #ifdef SMARTALLOC fprintf(stderr, "No orphaned buffer messages should follow this line.\n"); #endif sm_dump(1); return 0; }
/* Cleanup and then exit */ void terminate_dird(int sig) { static bool already_here = false; if (already_here) { /* avoid recursive temination problems */ bmicrosleep(2, 0); /* yield */ exit(1); } already_here = true; debug_level = 0; /* turn off debug */ stop_watchdog(); db_sql_pool_destroy(); db_flush_backends(); unload_dir_plugins(); write_state_file(me->working_directory, "bareos-dir", get_first_port_host_order(me->DIRaddrs)); delete_pid_file(me->pid_directory, "bareos-dir", get_first_port_host_order(me->DIRaddrs)); term_scheduler(); term_job_server(); if (runjob) { free(runjob); } if (configfile != NULL) { free(configfile); } if (debug_level > 5) { print_memory_pool_stats(); } if (my_config) { my_config->free_resources(); free(my_config); my_config = NULL; } stop_UA_server(); term_msg(); /* terminate message handler */ cleanup_crypto(); close_memory_pool(); /* release free memory in pool */ lmgr_cleanup_main(); sm_dump(false); exit(sig); }
int main() { FILESET *fileset; char buf[30]; alist *mlist; fileset = (FILESET *)malloc(sizeof(FILESET)); memset(fileset, 0, sizeof(FILESET)); fileset->mylist.init(); printf("Manual allocation/destruction of list:\n"); for (int i=0; i<20; i++) { sprintf(buf, "This is item %d", i); fileset->mylist.append(bstrdup(buf)); } for (int i=0; i< fileset->mylist.size(); i++) { printf("Item %d = %s\n", i, (char *)fileset->mylist[i]); } fileset->mylist.destroy(); free(fileset); printf("Allocation/destruction using new delete\n"); mlist = new alist(10); for (int i=0; i<20; i++) { sprintf(buf, "This is item %d", i); mlist->append(bstrdup(buf)); } for (int i=0; i< mlist->size(); i++) { printf("Item %d = %s\n", i, (char *)mlist->get(i)); } delete mlist; sm_dump(false); /* test program */ }
int main() { char buf[30]; rblist *jcr_chain; MYJCR *jcr = NULL; MYJCR *jcr1; /* Now do a binary insert for the tree */ jcr_chain = New(rblist()); #define CNT 26 printf("append %d items\n", CNT*CNT*CNT); strcpy(buf, "ZZZ"); int count = 0; for (int i=0; i<CNT; i++) { for (int j=0; j<CNT; j++) { for (int k=0; k<CNT; k++) { count++; if ((count & 0x3FF) == 0) { Dmsg1(000, "At %d\n", count); } jcr = (MYJCR *)malloc(sizeof(MYJCR)); memset(jcr, 0, sizeof(MYJCR)); jcr->buf = bstrdup(buf); // printf("buf=%p %s\n", jcr, jcr->buf); jcr1 = (MYJCR *)jcr_chain->insert((void *)jcr, my_compare); if (jcr != jcr1) { Dmsg2(000, "Insert of %s vs %s failed.\n", jcr->buf, jcr1->buf); } buf[1]--; } buf[1] = 'Z'; buf[2]--; } buf[2] = 'Z'; buf[0]--; } printf("%d items appended\n", CNT*CNT*CNT); printf("num_items=%d\n", jcr_chain->size()); jcr = (MYJCR *)malloc(sizeof(MYJCR)); memset(jcr, 0, sizeof(MYJCR)); jcr->buf = bstrdup("a"); if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) { printf("One less failed!!!! Got: %s\n", jcr1->buf); } else { printf("One less: OK\n"); } free(jcr->buf); jcr->buf = bstrdup("ZZZZZZZZZZZZZZZZ"); if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) { printf("One greater failed!!!! Got:%s\n", jcr1->buf); } else { printf("One greater: OK\n"); } free(jcr->buf); jcr->buf = bstrdup("AAA"); if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) { printf("Search for AAA got %s\n", jcr1->buf); } else { printf("Search for AAA not found\n"); } free(jcr->buf); jcr->buf = bstrdup("ZZZ"); if ((jcr1 = (MYJCR *)jcr_chain->search((void *)jcr, my_compare))) { printf("Search for ZZZ got %s\n", jcr1->buf); } else { printf("Search for ZZZ not found\n"); } free(jcr->buf); free(jcr); printf("Find each of %d items in tree.\n", count); for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) { // printf("Got: %s\n", jcr->buf); if (!jcr_chain->search((void *)jcr, my_compare)) { printf("rblist binary_search item not found = %s\n", jcr->buf); } } printf("Free each of %d items in tree.\n", count); for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) { // printf("Free: %p %s\n", jcr, jcr->buf); free(jcr->buf); jcr->buf = NULL; } printf("num_items=%d\n", jcr_chain->size()); delete jcr_chain; sm_dump(true); /* unit test */ }
/* * .die (seg fault) * .dump (sm_dump) * .exit (no arg => .quit) */ bool dot_admin_cmds(UAContext *ua, const char *cmd) { pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; STORERES *store = NULL; CLIENTRES *client = NULL; bool dir=false; bool do_deadlock=false; const char *remote_cmd; int i; JCR *jcr = NULL; int a; if (strncmp(ua->argk[0], ".die", 4) == 0) { if (find_arg(ua, "deadlock") > 0) { do_deadlock = true; remote_cmd = ".die deadlock"; } else { remote_cmd = ".die"; } } else if (strncmp(ua->argk[0], ".dump", 5) == 0) { remote_cmd = "sm_dump"; } else if (strncmp(ua->argk[0], ".exit", 5) == 0) { remote_cmd = "exit"; } else { ua->error_msg(_("Unknown command: %s\n"), ua->argk[0]); return true; } /* General debug? */ for (i = 1; i < ua->argc; i++) { if (bstrcasecmp(ua->argk[i], "dir") || bstrcasecmp(ua->argk[i], "director")) { dir = true; } if (bstrcasecmp(ua->argk[i], "client") || bstrcasecmp(ua->argk[i], "fd")) { client = NULL; if (ua->argv[i]) { client = (CLIENTRES *)GetResWithName(R_CLIENT, ua->argv[i]); } if (!client) { client = select_client_resource(ua); } } if (bstrcasecmp(ua->argk[i], NT_("store")) || bstrcasecmp(ua->argk[i], NT_("storage")) || bstrcasecmp(ua->argk[i], NT_("sd"))) { store = NULL; if (ua->argv[i]) { store = (STORERES *)GetResWithName(R_STORAGE, ua->argv[i]); } if (!store) { store = get_storage_resource(ua); } } } if (!dir && !store && !client) { /* * We didn't find an appropriate keyword above, so * prompt the user. */ start_prompt(ua, _("Available daemons are: \n")); add_prompt(ua, _("Director")); add_prompt(ua, _("Storage")); add_prompt(ua, _("Client")); switch(do_prompt(ua, "", _("Select daemon type to make die"), NULL, 0)) { case 0: /* Director */ dir=true; break; case 1: store = get_storage_resource(ua); break; case 2: client = select_client_resource(ua); break; default: break; } } if (store) { switch (store->Protocol) { case APT_NDMPV2: case APT_NDMPV3: case APT_NDMPV4: ua->warning_msg(_("Storage has non-native protocol.\n")); break; default: do_storage_cmd(ua, store, remote_cmd); break; } } if (client) { do_client_cmd(ua, client, remote_cmd); } if (dir) { if (strncmp(remote_cmd, ".die", 4) == 0) { if (do_deadlock) { ua->send_msg(_("The Director will generate a deadlock.\n")); P(mutex); P(mutex); } ua->send_msg(_("The Director will segment fault.\n")); a = jcr->JobId; /* ref NULL pointer */ jcr->JobId = 1000; /* another ref NULL pointer */ jcr->JobId = a; } else if (strncmp(remote_cmd, ".dump", 5) == 0) { sm_dump(false, true); } else if (strncmp(remote_cmd, ".exit", 5) == 0) { quit_cmd(ua, cmd); } } return true; }
int main(int argc, char *const *argv) { FF_PKT *ff; char name[1000]; bool quiet = false; int i, ch; char *inc = NULL; char *exc = NULL; FILE *fd; setlocale(LC_ALL, ""); bindtextdomain("bacula", LOCALEDIR); textdomain("bacula"); lmgr_init_thread(); while ((ch = getopt(argc, argv, "ad:e:i:q?")) != -1) { switch (ch) { case 'a': /* print extended attributes *debug* */ attrs = 1; break; case 'd': /* set debug level */ if (*optarg == 't') { dbg_timestamp = true; } else { debug_level = atoi(optarg); if (debug_level <= 0) { debug_level = 1; } } break; case 'e': /* exclude patterns */ exc = optarg; break; case 'i': /* include patterns */ inc = optarg; break; case 'q': quiet = true; break; case '?': default: usage(); } } argc -= optind; argv += optind; jcr = new_jcr(sizeof(JCR), NULL); ff = init_find_files(); if (argc == 0 && !inc) { add_fname_to_include_list(ff, 0, "/"); /* default to / */ } else { for (i=0; i < argc; i++) { if (strcmp(argv[i], "-") == 0) { while (fgets(name, sizeof(name)-1, stdin)) { strip_trailing_junk(name); add_fname_to_include_list(ff, 0, name); } continue; } add_fname_to_include_list(ff, 0, argv[i]); } } if (inc) { fd = fopen(inc, "rb"); if (!fd) { printf(_("Could not open include file: %s\n"), inc); exit(1); } while (fgets(name, sizeof(name)-1, fd)) { strip_trailing_junk(name); add_fname_to_include_list(ff, 0, name); } fclose(fd); } if (exc) { fd = fopen(exc, "rb"); if (!fd) { printf(_("Could not open exclude file: %s\n"), exc); exit(1); } while (fgets(name, sizeof(name)-1, fd)) { strip_trailing_junk(name); add_fname_to_exclude_list(ff, name); } fclose(fd); } if (quiet) { match_files(jcr, ff, count_files); } else { match_files(jcr, ff, print_file); } printf(_("Files seen = %d\n"), num_files); term_include_exclude_files(ff); term_find_files(ff); free_jcr(jcr); term_last_jobs_list(); /* free jcr chain */ close_memory_pool(); lmgr_cleanup_main(); sm_dump(false); exit(0); }