static void berkely_registry_free(void *self) { g_debug("Shutting down berkeley DB"); DB *dbp = get_db(self); if (dbp != NULL) { list_db(dbp); dbp->close(dbp, 0); } }
/* main function */ int main(int argc, char ** argv){ if(argc == 2) config_file[0] = argv[1]; else config_file[0] = "opencp.conf"; if(!udp_init_socket()) exit(0); signal(SIGHUP, reconfigure); reconfigure(SIGHUP); list_db(ms_db->lisp_db4); list_db(ms_db->lisp_db6); list_site(site_db); plumb(); exit(EXIT_SUCCESS); }
int main(int argc, char ** argv){ if(argc !=2){ fprintf(stderr, "usage: %s <config_file>\n", argv[0]); exit(EXIT_FAILURE); } config_file[0] = argv[1]; signal(SIGHUP, reconfigure); reconfigure(SIGHUP); list_db(ms_db->lisp_db4); list_db(ms_db->lisp_db6); list_site(site_db); plumb(); exit(EXIT_SUCCESS); }
int deal_db (alpm_db_t *db) { switch (config.op) { case OP_LIST_REPO: case OP_LIST_REPO_S: return list_db (db, targets); case OP_INFO: case OP_INFO_P: return search_pkg_by_name (db, &targets); case OP_SEARCH: return search_pkg (db, targets); case OP_LIST_GROUP: return list_grp (db, targets); case OP_QUERY: return search_pkg_by_type (db, &targets, config.query); default: return 0; } }
main () { char choice; init_db (); /* initialize "database" */ while (1) { choice = menu_sel (); switch (choice) { case 1: add_entry (); break; case 2: del_entry (); break; case 3: list_db (); break; case 4: exit (0); /* exit the program */ } /* switch */ } /* while */ } /* main */
int main(int argc, char **argv) { u_char print_version = 0; char hostname[DCC_MAXDOMAINLEN]; int file_num; DCC_CK_TYPES type; char tbuf[80]; const char *cp, *cp0; struct timeval tv1, tv2; int us; struct tm tm; char *p; u_long l; int i; dcc_syslog_init(0, argv[0], 0); while ((i = getopt(argc, argv, "vVHDG:h:s:C:I:A:L:P:T:")) != -1) { switch (i) { case 'v': ++verbose; break; case 'V': dcc_version_print(); print_version = 1; break; case 'G': if (!strcasecmp(optarg, "on")) { grey_on = 1; } else if (!strcasecmp(optarg, "off")) { grey_on = 0; } else { usage(); } break; case 'h': homedir = optarg; break; case 's': l = strtoul(optarg, &p, 10); if ((*p != '\0' && *p != ',') || !DCC_ID_SRVR_NORMAL(l)) dcc_logbad(EX_USAGE, "invalid DCC ID \"-s %s\"", optarg); srvr.clnt_id = l; if (*p != '\0') { ++p; p += strspn(p, DCC_WHITESPACE); } hostname[0] = '\0'; srvr.port = 0; if (*p == '\0') break; cp = dcc_parse_nm_port(dcc_emsg, p, srvr.port, hostname, sizeof(hostname), &srvr.port, 0, 0, 0, 0); if (!cp) dcc_logbad(EX_USAGE, "%s", dcc_emsg); cp += strspn(cp, DCC_WHITESPACE); if (*cp != '\0') dcc_logbad(EX_USAGE, "unrecognized port number in" "\"-s %s\"", optarg); if (hostname[0] != '\0') BUFCPY(srvr.hostname, hostname); break; case 'H': no_hash = 1; break; case 'D': no_data = 1; break; case 'C': if (num_search_cksums >= DIM(search_cksums)) { dcc_error_msg("too many -C checksums"); break; } matching = 1; cp0 = optarg; /* separate checksum type and checksum in cp and tbuf */ cp = dcc_parse_word(0, tbuf, sizeof(tbuf), optarg, "checksum type", 0, 0); if (!cp) exit(1); if (!strcasecmp(tbuf, "hex")) { /* ignore "hex" */ cp0 = cp; cp = dcc_parse_word(0, tbuf, sizeof(tbuf), cp, "checksum type", 0, 0); if (!cp) dcc_logbad(EX_USAGE, "unrecognized checksum" " \"-C %s\"", optarg); } if (*cp == '\0') { /* allow bare checksum type */ type = dcc_str2type_del(tbuf, -1); if (type == DCC_CK_INVALID) dcc_logbad(EX_USAGE, "unrecognized checksum type" " \"-C %s\"", optarg); search_cksums[num_search_cksums].type = type; memset(&search_cksums[num_search_cksums].sum, 0, sizeof(DCC_SUM)); search_cksums[num_search_cksums].type_only = 1; ++num_search_cksums; break; } /* allow missing checksum type */ l = strtoul(tbuf, &p, 16); if (*p == '\0') { if (0 >= dcc_parse_hex_ck(dcc_emsg, 0, "-", DCC_CK_FLOD_PATH, cp0, 0, save_cksum)) dcc_logbad(EX_USAGE, "%s", dcc_emsg); break; } type = dcc_str2type_del(tbuf, -1); if (type == DCC_CK_FLOD_PATH) dcc_logbad(EX_USAGE, "unrecognized checksum type" " \"-C %s\"", optarg); if (1 <= dcc_parse_hex_ck(dcc_emsg, 0, tbuf, type, cp, 0, save_cksum)) break; /* allow strings for server-IDs */ if (type == DCC_CK_SRVR_ID && (i = strlen(cp)) <= ISZ(DCC_SUM)) { DCC_SUM name; memset(&name, 0, sizeof(name)); memcpy(&name, cp, i); save_cksum(0, 0, type, &name, 0); break; } dcc_logbad(EX_USAGE, "%s", dcc_emsg); break; case 'I': if (num_search_ids >= DIM(search_ids)) { dcc_error_msg("too many -I IDs"); break; } search_ids[num_search_ids] = strtoul(optarg, &p, 10); if (search_ids[num_search_ids] > DCC_SRVR_ID_MAX || *p != '\0') dcc_logbad(EX_USAGE, "invalid server-ID \"-I %s\"", optarg); ++num_search_ids; matching = 1; break; case 'A': dbaddr = strtoul(optarg, &p, 16); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid database address \"%s\"", optarg); matching = 1; break; case 'L': max_pathlen = strtoul(optarg, &p, 10); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid path length \"%s\"", optarg); matching = 1; break; case 'P': page_offset = strtoul(optarg, &p, 10); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid number of pages \"%s\"", optarg); matching = 1; break; case 'T': if (num_search_ts >= DIM(search_ts)) { dcc_error_msg("too many -T timestamps"); break; } memset(&tm, 0, sizeof(tm)); i = sscanf(optarg, "%d/%d/%d %d:%d:%d.%d%c", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &us, tbuf); if (i < 6 || i > 7 || tm.tm_mon <= 0) dcc_logbad(EX_USAGE,"bad timestamp \"%s\"", optarg); --tm.tm_mon; tm.tm_year += 100; tv1.tv_sec = DCC_TIMEGM(&tm); if (tv1.tv_sec < 0) dcc_logbad(EX_USAGE, "invalid timestamp \"%s\"", optarg); tv2.tv_sec = tv1.tv_sec; if (i == 7) { if (us >= DCC_US) dcc_logbad(EX_USAGE, "invalid microseconds" " in \"%s\"", optarg); tv1.tv_usec = us; tv2.tv_usec = us; } else { tv1.tv_usec = 0; tv2.tv_usec = DCC_US-1; } timeval2ts(&search_ts[num_search_ts].lo, &tv1, 0); timeval2ts(&search_ts[num_search_ts].hi, &tv2, 0); ++num_search_ts; matching = 1; break; default: usage(); } } argc -= optind; argv += optind; def_argv[0] = grey_on ? grey_db_nm : dcc_db_nm; if (argc == 0) { if (print_version) exit(EX_OK); argv = def_argv; argc = 1; } dcc_clnt_unthread_init(); if (!dcc_cdhome(dcc_emsg, homedir, 1)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); flod_mmap_path_set(); if (matching) { if (no_data && no_hash) dcc_logbad(EX_USAGE, "patterns need data or hash table"); if (!no_data && !no_hash) no_hash = 1; } if (dbaddr != 0 && page_offset != 0) dcc_logbad(EX_USAGE, "-P and -A are incompatible"); if (srvr.clnt_id != 0) { if (argc != 1) dcc_logbad(EX_USAGE, "lock only one file"); i = load_ids(dcc_emsg, srvr.clnt_id, &srvr_clnt_tbl, 1, verbose > 4); if (!srvr_clnt_tbl) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); if (i <= 0) dcc_error_msg("%s", dcc_emsg); memcpy(srvr.passwd, srvr_clnt_tbl->cur_passwd, sizeof(srvr.passwd)); if (srvr.hostname[0] == '\0') BUFCPY(srvr.hostname, "127.0.0.1"); if (srvr.port == 0) srvr.port = DCC_GREY2PORT(grey_on); i = DCC_CLNT_FG_SLOW; if (grey_on) i |= DCC_CLNT_FG_GREY; ctxt = dcc_tmp_clnt_init(dcc_emsg, 0, &srvr, 0, 0, i, 0); if (!ctxt) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); if (!lock_dbclean(dcc_emsg, *argv)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s: dbclean running?", dcc_emsg); atexit(rel_db); signal(SIGALRM, sigterm); signal(SIGHUP, sigterm); signal(SIGTERM, sigterm); signal(SIGINT, sigterm); if (!dcc_aop_persist(dcc_emsg, ctxt, grey_on ? DCC_CLNT_FG_GREY : 0, verbose != 0, DCC_AOP_DB_UNLOAD, 0, 60*5, &aop_resp)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); } for (file_num = 1; *argv != 0; ++argv, ++file_num) { if (fd_db >= 0) close(fd_db); if (fd_hash >= 0) close(fd_hash); BUFCPY(db_nm, *argv); snprintf(hash_nm, sizeof(hash_nm), "%s"DB_HASH_SUFFIX, db_nm); if (file_num != 1) fputc('\n', stdout); if (verbose || argc > 1) printf(" %s\n", db_nm); /* try to open the hash table and the database * fail only if we cannot open the database */ open_hash(); if (!open_db()) continue; /* print the header of the database followed by its contents */ list_db(); list_hash(); } exit(EX_OK); }
void * cli_start_communication(void *context) { char line[2048]; char *params[100]; char *token; uint32_t rid; int i = 0; while (fgets(line, sizeof line, stdin) != NULL) { line[strlen(line)-1] = '\0'; token = strtok (line, " "); while (token != NULL) { params[i++] = token; token = strtok (NULL, " ,"); } if (!i) continue; /* == GET a mapping */ /* Map-Request <eid> <nonce> */ if (strcasecmp("map-request", params[0]) == 0 && i == 3) { rid = cli_request_add(params[1], (uint64_t)atoi(params[2])); generic_process_request(rid, &cli_fct); } /* == Stop the CLI */ /* quit */ if (strcasecmp("quit", params[0]) == 0) { cli_fct.stop_communication(NULL); return (NULL); } /* == Register a new mapping */ /* example: ======= map-register 6.6.6.6/24 version 65 A true TTL 56 -rloc address 127.0.0.1 priority 1 weight 100 m_priority 255 m_priority 0 reachable false -rloc address6 fe80::226:bbff:fe0e:882c priority 2 weight 100 m_priority 255 m_priority 0 reachable true map-register 1.2.3.4/32 ACT 2 TTL 5 A true map-request 6.6.6.6 123567 map-request 1.2.3.4 098765 */ /* Map-Register <eid> */ if (strcasecmp("map-register", params[0]) == 0) { void *_mapping; struct prefix p1; struct mapping_flags *mflags; struct map_entry *entry = NULL; int j = 1; printf("p1:%s\n", params[j]); str2prefix (params[j], &p1); apply_mask(&p1); _mapping = generic_mapping_new(&p1); j++; int prev = 0; int count = 0; void *ptr = NULL; mflags = calloc(1, sizeof(struct mapping_flags)); while (j < i - 1) { if (0 == strcasecmp(params[j], "-rloc")) { j++; if (prev && count > 0) { printf("ADD RLOC\n"); assert(entry != NULL); generic_mapping_add_rloc(_mapping, entry); prev = 0; } printf("new rloc\n"); entry = calloc(1, sizeof(struct map_entry)); printf("%p\n", entry); continue; }else if (0 == strcasecmp(params[j], "priority")) { entry->priority = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "m_priority")) { entry->m_priority = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "weight")) { entry->weight = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "m_weight")) { entry->m_weight = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "reachable")) { entry->r = (strcasecmp(params[j+1], "true")==0); }else if (0 == strcasecmp(params[j], "local")) { entry->L = (strcasecmp(params[j+1], "true")==0); }else if (0 == strcasecmp(params[j], "rloc-probing")) { entry->p = (strcasecmp(params[j+1], "true")==0); }else if (0 == strcasecmp(params[j], "address")) { entry->rloc.sa.sa_family = AF_INET; ptr = &(entry->rloc.sin.sin_addr); }else if (0 == strcasecmp(params[j], "address6")) { entry->rloc.sa.sa_family = AF_INET6; ptr = &entry->rloc.sin6.sin6_addr; } /* mapping flags*/ else if (0 == strcasecmp(params[j], "act")) { mflags->act = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "a")) { mflags->A = (strcasecmp(params[j+1], "true")==0); }else if (0 == strcasecmp(params[j], "version")) { mflags->version = atoi(params[j+1]); }else if (0 == strcasecmp(params[j], "ttl")) { mflags->ttl = atoi(params[j+1]); } /* an RLOC */ if (ptr) { count++; inet_pton(entry->rloc.sa.sa_family, params[j+1], ptr); ptr = NULL; } //printf("%s -> %s\n", params[j], params[j+1]); j = j+2; prev = 1; } if (prev && count > 0) { printf("ADD RLOC\n"); generic_mapping_add_rloc(_mapping, entry); prev = 0; } generic_mapping_set_flags(_mapping, mflags); } if (strcasecmp("map-database", params[0]) == 0) { assert(ms_db->lisp_db4); assert(ms_db->lisp_db6); list_db(ms_db->lisp_db4); list_db(ms_db->lisp_db6); } if (strcasecmp("reload", params[0]) == 0) { reconfigure(); } if (strcasecmp("help", params[0]) == 0) { printf("""\t•map-database\n"); printf("\t•map-register\n"); printf("\t\tExample:\n \t\t\t map-register 6.6.6.6/24 version 65 A true TTL 56 \\ \n \t\t\t-rloc address 127.0.0.1 priority 1 weight 100 m_priority 255 m_priority 0 reachable false \\ \n \t\t\t -rloc address6 fe80::226:bbff:fe0e:882c priority 2 weight 100 m_priority 255 m_priority 0 reachable true\n"); printf("\t•map-request\n"); printf("\t\ttexample:\n \t\t\tmap-request 6.6.6.6 123567\n"); printf("\t•reload\n"); } } return (NULL); }
/* Основная функция */ int main() { # define ACTION(str) if (!strcmp(#str, action)) setlocale(LC_ALL, "Ru"); BookDB* db = NULL; char filename[MAX_STRING_LENGTH] = "\0"; char action[MAX_STRING_LENGTH]; print_hello(); /* Цикл обработки событий */ forever { printf("<! "); scanf("%s", action); ACTION(help) { print_help(); continue; } ACTION(exit) { if (db) { close_db(db, filename); db = NULL; filename[0] = '\0'; } break; } ACTION(close) { if (db) { close_db(db, filename); db = NULL; filename[0] = '\0'; output("База данных закрыта!\n"); } else db_not_loaded_error(); continue; } ACTION(create) { if (db) { close_db(db, filename); filename[0] = '\0'; db = NULL; } int n = 0; output("Предполагаемая размерность Базы Данных\n"); input("%d", &n); db = create_db(n); output("База данных для %i элементов создана!\n", n); continue; } ACTION(load) { if (db) { close_db(db, filename); filename[0] = '\0'; db = NULL; } do { output("Имя файла базы данных\n"); input("%s", filename); } while (strlen(filename) < 1); db = load_db(filename); if (!db) { output("Неверное имя файла!\n"); filename[0] = '\0'; } else { output(">> База данных успешно загружена!\n"); } continue; } ACTION(save) { if (db) { save_db_dialog(db, filename); output(">> База данных успешно сохранена!\n"); } else db_not_loaded_error(); continue; } ACTION(list) { if (db != 0) { list_db(db); } else db_not_loaded_error(); continue; } ACTION(find) { if (db) { find_dialog(db); } else db_not_loaded_error(); continue; } ACTION(new) { if (db) { new_dialog(db); } else db_not_loaded_error(); continue; } ACTION(delete) { if (db) { delete_dialog(db); } else db_not_loaded_error(); continue; } ACTION(size) { if (db) { output("Книг в базе данных - %d\n", db->count); } else db_not_loaded_error(); continue; } ACTION(sort) { if (db) { sort_dialog(db); } else db_not_loaded_error(); continue; } printf("!! Неизвестная команда \"%s\"\n", action); } # undef ACTION return 0; }
int main(int argc, char ** argv){ config_file[0] = "opencp.conf"; int c; opterr = 0; _daemon = 0; while ((c = getopt (argc, argv, "df:")) != -1){ switch (c){ case 'd': _daemon = 1; break; case 'f': config_file[0] = optarg; break; case '?': if (optopt == 'f') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: abort (); } } if(_daemon){ pid_t pid; /* Clone child from ourselves */ pid = fork(); if (pid < 0) { exit(EXIT_FAILURE); } /* pid > 0, we are the parent, exit */ if (pid > 0) { exit(EXIT_SUCCESS); } /* now I'm child */ FILE *fpid; pid = getpid(); printf("my pid is %d\n",pid); fpid = fopen("/var/run/opencp.pid", "w"); fprintf(fpid,"%d",pid); fclose(fpid); } if(_daemon){ flog = fopen("/var/log/opencp.log","a"); signal(SIGUSR1, reopenlog); }else{ flog = NULL; } //signal(SIGHUP, reconfigure); reconfigure(SIGHUP); if(!udp_init_socket()) exit(0); if(_daemon){ fflush(stdout); //close(STDOUT_FILENO); //close(STDIN_FILENO); //close(STDERR_FILENO); } list_db(ms_db->lisp_db4); list_db(ms_db->lisp_db6); list_site(site_db); plumb(); fclose(flog); exit(EXIT_SUCCESS); }