| 1 | /********************************************************************** |
|---|
| 2 | * Copyright (c) 2005-2008 David Carter <dcarter@arm4.org> and others. |
|---|
| 3 | * All rights reserved. This program and the accompanying materials |
|---|
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
|---|
| 5 | * which accompanies this distribution, and is available at |
|---|
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
|---|
| 7 | * |
|---|
| 8 | * Contributors: |
|---|
| 9 | * David Carter - Initial API and implementation |
|---|
| 10 | **********************************************************************/ |
|---|
| 11 | |
|---|
| 12 | #include "config.h" |
|---|
| 13 | |
|---|
| 14 | #include <sys/types.h> |
|---|
| 15 | #include <sys/time.h> |
|---|
| 16 | #include <errno.h> |
|---|
| 17 | |
|---|
| 18 | #include <stddef.h> |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <stdlib.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | #include <math.h> |
|---|
| 23 | |
|---|
| 24 | #ifdef HAVE_GETOPT_H |
|---|
| 25 | #include <getopt.h> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include <db.h> |
|---|
| 29 | #include "arm4.h" |
|---|
| 30 | #include "Arm4db.h" |
|---|
| 31 | #include "Logger.h" |
|---|
| 32 | #include "Arm4dbSharedMemory.h" |
|---|
| 33 | #include "arm4agent.h" |
|---|
| 34 | |
|---|
| 35 | static void |
|---|
| 36 | initialize_configuration (const char *archive_directory, const char *config_filename) |
|---|
| 37 | { |
|---|
| 38 | // Set the database configuration |
|---|
| 39 | Arm4dbConfig::setConfig (config_filename); |
|---|
| 40 | |
|---|
| 41 | if (strlen (archive_directory) > 0) |
|---|
| 42 | Arm4dbConfig::setDBHome (archive_directory); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | static void |
|---|
| 46 | version (void) |
|---|
| 47 | { |
|---|
| 48 | printf ("Package %s\n", PACKAGE); |
|---|
| 49 | printf ("Version %s\n", VERSION); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | static void |
|---|
| 53 | usage (void) |
|---|
| 54 | { |
|---|
| 55 | printf ("Usage:\n"); |
|---|
| 56 | #ifdef HAVE_GETOPT_LONG |
|---|
| 57 | printf ("\tarm4_dump --help|-h\n"); |
|---|
| 58 | printf ("\t\tShow this help message\n"); |
|---|
| 59 | printf ("\tarm4_dump --version|-V\n"); |
|---|
| 60 | printf ("\t\tDisplay implementation version\n"); |
|---|
| 61 | printf ("\tarm4_dump [--config|-C config_file] [--archive|-a archive_directory]\n"); |
|---|
| 62 | printf ("\t\t--config specifies a path to the configuration file. The default is /etc/arm4.conf\n"); |
|---|
| 63 | printf ("\t\t--archive specifies a path to the archive directory\n"); |
|---|
| 64 | #else |
|---|
| 65 | printf ("\tarm4_dump -h\n"); |
|---|
| 66 | printf ("\t\tShow this help message\n"); |
|---|
| 67 | printf ("\tarm4_dump -V\n"); |
|---|
| 68 | printf ("\t\tDisplay implementation version\n"); |
|---|
| 69 | printf ("\tarm4_dump [-C config_file] [-a archive_directory]\n"); |
|---|
| 70 | printf ("\t\t-C specifies a path to the configuration file. The default is /etc/arm4.conf\n"); |
|---|
| 71 | printf ("\t\t-a specifies a path to the archive directory\n"); |
|---|
| 72 | #endif |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | int |
|---|
| 76 | main (int argc, char *argv[]) |
|---|
| 77 | { |
|---|
| 78 | int status; |
|---|
| 79 | int c; |
|---|
| 80 | char config_filename [1024]; |
|---|
| 81 | char archive_directory [1024]; |
|---|
| 82 | |
|---|
| 83 | // Initialize the logger |
|---|
| 84 | Logger::getLogger().setName ("arm4_dump"); |
|---|
| 85 | Logger::getLogger().setFile (stderr); |
|---|
| 86 | |
|---|
| 87 | /* Set the default config file */ |
|---|
| 88 | strcpy (config_filename, "/etc/arm4.conf"); |
|---|
| 89 | archive_directory [0] = '\0'; |
|---|
| 90 | |
|---|
| 91 | while (1) |
|---|
| 92 | { |
|---|
| 93 | #ifdef HAVE_GETOPT_LONG |
|---|
| 94 | static struct option long_options[] = |
|---|
| 95 | { |
|---|
| 96 | /* These options set a flag. */ |
|---|
| 97 | /* These options don't set a flag. We distinguish them by their indices. */ |
|---|
| 98 | {"help", no_argument, 0, 'h'}, |
|---|
| 99 | {"version", no_argument, 0, 'V'}, |
|---|
| 100 | {"config", required_argument, 0, 'C'}, |
|---|
| 101 | {"archive", required_argument, 0, 'a'}, |
|---|
| 102 | |
|---|
| 103 | {0, 0, 0, 0} |
|---|
| 104 | }; |
|---|
| 105 | |
|---|
| 106 | /* getopt_long stores the option index here. */ |
|---|
| 107 | int option_index = 0; |
|---|
| 108 | |
|---|
| 109 | c = getopt_long (argc, argv, "a:hVC:", |
|---|
| 110 | long_options, &option_index); |
|---|
| 111 | #else |
|---|
| 112 | c = getopt (argc, argv, "a:hVC:"); |
|---|
| 113 | #endif |
|---|
| 114 | /* Detect the end of the options. */ |
|---|
| 115 | if (c == -1) |
|---|
| 116 | break; |
|---|
| 117 | |
|---|
| 118 | switch (c) |
|---|
| 119 | { |
|---|
| 120 | case 0: |
|---|
| 121 | /* If this option set a flag, do nothing else now. */ |
|---|
| 122 | #ifdef HAVE_GETOPT_LONG |
|---|
| 123 | if (long_options[option_index].flag != 0) |
|---|
| 124 | break; |
|---|
| 125 | |
|---|
| 126 | printf ("option %s", long_options[option_index].name); |
|---|
| 127 | if (optarg) |
|---|
| 128 | printf (" with arg %s", optarg); |
|---|
| 129 | printf ("\n"); |
|---|
| 130 | #endif |
|---|
| 131 | break; |
|---|
| 132 | |
|---|
| 133 | case '?': |
|---|
| 134 | /* getopt_long already printed an error message. */ |
|---|
| 135 | break; |
|---|
| 136 | |
|---|
| 137 | case 'a': |
|---|
| 138 | strncpy (archive_directory, optarg, sizeof(archive_directory)); |
|---|
| 139 | break; |
|---|
| 140 | |
|---|
| 141 | case 'h': |
|---|
| 142 | usage (); |
|---|
| 143 | exit (0); |
|---|
| 144 | |
|---|
| 145 | case 'V': |
|---|
| 146 | version (); |
|---|
| 147 | exit (0); |
|---|
| 148 | |
|---|
| 149 | case 'C': |
|---|
| 150 | strncpy (config_filename, optarg, sizeof(config_filename)); |
|---|
| 151 | break; |
|---|
| 152 | |
|---|
| 153 | default: |
|---|
| 154 | usage (); |
|---|
| 155 | abort (); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | initialize_configuration (archive_directory, config_filename); |
|---|
| 160 | |
|---|
| 161 | Arm4dbUtilityLock lock; |
|---|
| 162 | |
|---|
| 163 | Arm4db *arm4db_ptr = Arm4db::getArm4db (); |
|---|
| 164 | status = arm4db_ptr->openDatabasesReadOnly (); |
|---|
| 165 | if (status != ARM_SUCCESS) |
|---|
| 166 | { |
|---|
| 167 | lock.unlock (); |
|---|
| 168 | return 1; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | arm4db_ptr->dump (); |
|---|
| 172 | |
|---|
| 173 | arm4db_ptr->closeDatabases (); |
|---|
| 174 | |
|---|
| 175 | lock.unlock (); |
|---|
| 176 | |
|---|
| 177 | return 0; |
|---|
| 178 | } |
|---|
| 179 | |
|---|