1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/*
*
* Copyright (c) International Business Machines Corp., 2001
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module: main.c
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <getopt.h>
#include <pthread.h>
#include <frontend.h>
#include <gtk/gtk.h>
#include "support.h"
#include "pixmap.h"
#include "main_window.h"
#include "logging.h"
#include "message.h"
#include "views.h"
#include "main.h"
static pid_t main_thread_pid;
static engine_mode_t open_mode = ENGINE_READWRITE;
static debug_level_t debug_level = DEFAULT;
static gchar *log_name = NULL;
static GtkWidget *startup_window = NULL;
static void show_help (char **argv)
{
printf ("\nEnterprise Volume Management System %s\n", VERSION);
printf ("International Business Machines %s\n\n", DATE);
printf ("Usage: %s [OPTION...] \n\n", argv[0]);
printf (" -d, --log-level=LEVEL\t\tLogging output level where LEVEL is one of the following:\n");
printf (" \t\t\t\t\tcritical | serious | error | warning | default |\n");
printf (" \t\t\t\t\tdetails | debug | extra | entry-exit | everything\n");
printf (" -?, --help\t\t\tShow this help message\n");
printf (" -m, --mode=ENGINE-MODE\tEngine mode where ENGINE-MODE is one of the following:\n");
printf (" \t\t\t\t\treadwrite | readonly\n");
printf (" -l, --log-name=FILENAME\tFile to send logging output to\n\n");
}
/*
*
* static gint parse_options (int argc, char **argv)
*
* Description:
* This routine parses the command line options. It then sets the
* appropriate program variables accordingly.
*
* Entry:
* argc - count of command line arguments
* argv - array of program command line argument strings
*
* Exit:
* Returns 0 is parsing was successful or -1 if a problem was encountered.
*
*/
static gint parse_options (int argc, char **argv)
{
int c;
gint rc=SUCCESS;
char *short_opts = "h?d:m:l:";
struct option long_opts[] =
{
{"help", no_argument, NULL, 'h'},
{"log-level", required_argument, NULL, 'd'},
{"mode", required_argument, NULL, 'm'},
{"log-name", required_argument, NULL, 'l'},
{NULL, 0, NULL, 0 }
};
while (rc == SUCCESS && (c = getopt_long (argc, argv, short_opts, long_opts, NULL)) != EOF)
{
switch (c)
{
case 'm':
if (g_strcasecmp (optarg, "readwrite") == 0)
open_mode = ENGINE_READWRITE;
else if (g_strcasecmp (optarg, "readonly") == 0)
open_mode = ENGINE_READONLY;
else
{
g_warning (_("Unrecognized mode argument -- \"%s\"\n\n"), optarg);
rc = -1;
}
break;
case 'd':
if (g_strcasecmp (optarg, "critical") == 0)
debug_level = CRITICAL;
else if (g_strcasecmp (optarg, "serious") == 0)
debug_level = SERIOUS;
else if (g_strcasecmp (optarg, "error") == 0)
debug_level = ERROR;
else if (g_strcasecmp (optarg, "warning") == 0)
debug_level = WARNING;
else if (g_strcasecmp (optarg, "default") == 0)
debug_level = DEFAULT;
else if (g_strcasecmp (optarg, "details") == 0)
debug_level = DETAILS;
else if (g_strcasecmp (optarg, "debug") == 0)
debug_level = DEBUG;
else if (g_strcasecmp (optarg, "extra") == 0)
debug_level = EXTRA;
else if (g_strcasecmp (optarg, "entry-exit") == 0)
debug_level = ENTRY_EXIT;
else if (g_strcasecmp (optarg, "everything") == 0)
debug_level = EVERYTHING;
else
{
g_warning (_("Unrecognized debug level argument -- \"%s\"\n\n"), optarg);
rc = -1;
}
break;
case 'l':
log_name = g_strdup (optarg);
break;
default:
g_warning (_("Unrecognized option -- \"%c\"\n\n"), c);
case 'h':
case '?':
rc = -1;
break;
}
}
if (rc != SUCCESS)
show_help (argv);
return rc;
}
/*
*
* void display_startup_window (void)
*
* Description:
* This routine simply displays a window without decorations that
* contains a label with text that indicates that EVMS is coming up.
* threads.
*
* Entry:
* Nothing
*
* Exit:
* Returns nothing
*
*/
void display_startup_window (void)
{
GtkWidget *label;
startup_window = gtk_window_new (GTK_WINDOW_DIALOG);
gtk_window_set_position (GTK_WINDOW (startup_window), GTK_WIN_POS_CENTER);
label = gtk_label_new (_("EVMS is examining your system. Please wait..."));
gtk_container_add (GTK_CONTAINER (startup_window), label);
gtk_misc_set_padding (GTK_MISC (label), 10, 10);
gtk_widget_realize (startup_window);
gdk_window_set_decorations (startup_window->window, 0);
gtk_widget_show (label);
gtk_widget_show (startup_window);
/*
* Since this is our first window, take advantage of it to
* initialize the standard pixmaps before any other windows
* such as message windows use them.
*/
create_standard_pixmaps (startup_window);
}
/*
*
* inline void destroy_startup_window (void)
*
* Description:
* This routine destroys the startup window.
*
* Entry:
* Nothing
*
* Exit:
* Returns nothing
*
*/
inline void destroy_startup_window (void)
{
if (startup_window)
{
gtk_widget_destroy (startup_window);
startup_window = NULL;
}
}
/*
*
* void* main_event_loop_thread (void *)
*
* Description:
* This routine executes the gtk_main () function to process
* the GTK main event loop. Having the event loop on its own
* thread allows us to inject events while we are asynchronously
* processing other things in the open engine and commit changes
* threads.
*
* Entry:
* not_used - just like it sounds
*
* Exit:
* Returns nothing
*
*/
void* main_event_loop_thread (void *not_used)
{
main_thread_pid = getpid ();
gdk_threads_enter ();
display_startup_window ();
gtk_main ();
gdk_threads_leave ();
return NULL;
}
/*
*
* gint check_engine_version (pthread_t)
*
* Description:
* This routine checks to see that we are running a version
* of the engine that we like. If not, we display a message
* and wait for the user to acknowledge before exiting.
*
* Entry:
* tid - thread id of main event loop thread
*
* Exit:
* Returns SUCCESS if we are OK with the version. Otherwise,
* we'll return EPERM.
*
*/
gint check_engine_version (pthread_t tid)
{
gint rc=EPERM;
evms_version_t version;
evms_get_api_version (&version);
if (REQUIRED_ENGINE_VER_MAJOR == version.major &&
((version.minor > REQUIRED_ENGINE_VER_MINOR) ||
(REQUIRED_ENGINE_VER_MINOR == version.minor && REQUIRED_ENGINE_VER_PATCHLEVEL <= version.patchlevel)))
{
rc = SUCCESS;
}
else
{
gchar *error_message;
GtkWidget *window;
sleep (1);
error_message = g_strdup_printf (_("Version %u.%u.%u or later of the EVMS Engine API required. Detected version %u.%u.%u."),
REQUIRED_ENGINE_VER_MAJOR, REQUIRED_ENGINE_VER_MINOR,
REQUIRED_ENGINE_VER_PATCHLEVEL,
version.major, version.minor, version.patchlevel
);
gdk_threads_enter ();
destroy_startup_window ();
window = create_informational_message_window (error_message, FALSE);
gtk_widget_show (window);
gdk_threads_leave ();
g_free (error_message);
/*
* Wait for main loop thread to exit before returning.
*/
pthread_join (tid, NULL);
}
return rc;
}
/*
*
* gint open_engine (pthread_t)
*
* Description:
* This routine allows the opening of the engine to get processed
* on the initial thread while we have a separate thread sit in the
* GTK main event loop. This is in case we receive message callbacks
* and need to display messages during the discovery phase.
*
* Once the engine discovery is complete, we block this thread until
* the main event loop thread exits. Blocking this thread allows the
* VFS to have a valid pid registered for the lock on the EVMS block
* device.
*
* Entry:
* tid - thread id of main event loop thread
*
* Exit:
* Returns return code from evms_open_engine
*
*/
gint open_engine (pthread_t tid)
{
gint rc;
GtkWidget *window;
rc = evms_open_engine (open_mode, get_message_function_table (), debug_level, log_name);
if (rc != SUCCESS)
{
gchar *error_message;
log_error (_("%s: evms_open_engine() failed with return code %d.\n"), __FUNCTION__, rc);
log_error ("%s: %s\n", __FUNCTION__, g_strerror (ABS (rc)));
error_message = g_strdup_printf (_("Received the following error opening EVMS engine: %s"),
g_strerror (ABS (rc)));
/*
* Give any scheduled idle function a chance to run before displaying the last message
*/
sleep (1);
gdk_threads_enter ();
destroy_startup_window ();
window = create_informational_message_window (error_message, FALSE);
gtk_widget_show (window);
gdk_threads_leave ();
g_free (error_message);
}
else
{
gchar hostname[MAX_HOSTNAME+1];
gdk_threads_enter ();
destroy_startup_window ();
window = create_main_window ();
if (gethostname (hostname, MAX_HOSTNAME) == 0)
{
gchar *title_with_hostname;
title_with_hostname = g_strdup_printf (_("EVMS Administration Utility (%s)"), hostname);
gtk_window_set_title (GTK_WINDOW (window), title_with_hostname);
g_free (title_with_hostname);
}
set_main_window_id (window);
set_status_bar_id (gtk_object_get_data (GTK_OBJECT (window), "main_status_bar"));
set_status_bar_message (_("Ready."));
set_refresh_views_pixmap (window);
gtk_widget_show (window);
set_message_window_transient_for (GTK_WINDOW (window));
gdk_threads_leave ();
}
pthread_join (tid, NULL);
if (rc == SUCCESS)
evms_close_engine ();
return rc;
}
/*
*
* int main (int, char **)
*
* Description:
* This routine is the main routine. It determines if the user has root
* privileges. If the user does not have root privileges then we prompt
* for the root password in order to run as root. It creates the main
* window and common pixmaps and enters the main event loop.
*
* Entry:
* argc - count of command line arguments
* argv - array of program command line argument strings
*
* Exit:
* Returns -1 when command line usage is displayed.
*
*/
int main (int argc, char *argv[])
{
gint rc;
if (geteuid () == 0)
{
g_thread_init (NULL);
gtk_set_locale ();
gtk_init (&argc, &argv);
rc = parse_options (argc, argv);
if (rc == SUCCESS)
{
pthread_t tid;
rc = pthread_create (&tid, NULL, main_event_loop_thread, NULL);
if (rc == SUCCESS)
{
rc = check_engine_version (tid);
if (rc == SUCCESS)
rc = open_engine (tid);
}
else
log_error ("%s: Unable to start main event loop thread. rc == %d.\n",
__FUNCTION__, rc);
}
}
else
{
rc = EPERM;
g_warning (_("This command must be run with root privilege.\n"));
}
return rc;
}
/*
* Getter function that returns pid of main thread/lightweight process.
*/
pid_t get_main_thread_id (void)
{
return main_thread_pid;
}
|