main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Server main function
  3. *
  4. * Copyright (C) 1998 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <fcntl.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sys/time.h>
  28. #include <unistd.h>
  29. #ifdef HAVE_GETOPT_H
  30. # include <getopt.h>
  31. #endif
  32. #include "object.h"
  33. #include "file.h"
  34. #include "thread.h"
  35. #include "request.h"
  36. #include "esync.h"
  37. /* command-line options */
  38. int debug_level = 0;
  39. int foreground = 0;
  40. timeout_t master_socket_timeout = 3 * -TICKS_PER_SEC; /* master socket timeout, default is 3 seconds */
  41. const char *server_argv0;
  42. /* parse-line args */
  43. static void usage( FILE *fh )
  44. {
  45. fprintf(fh, "Usage: %s [options]\n\n", server_argv0);
  46. fprintf(fh, "Options:\n");
  47. fprintf(fh, " -d[n], --debug[=n] set debug level to n or +1 if n not specified\n");
  48. fprintf(fh, " -f, --foreground remain in the foreground for debugging\n");
  49. fprintf(fh, " -h, --help display this help message\n");
  50. fprintf(fh, " -k[n], --kill[=n] kill the current wineserver, optionally with signal n\n");
  51. fprintf(fh, " -p[n], --persistent[=n] make server persistent, optionally for n seconds\n");
  52. fprintf(fh, " -v, --version display version information and exit\n");
  53. fprintf(fh, " -w, --wait wait until the current wineserver terminates\n");
  54. fprintf(fh, "\n");
  55. }
  56. static void parse_args( int argc, char *argv[] )
  57. {
  58. int ret, optc;
  59. static struct option long_options[] =
  60. {
  61. {"debug", 2, NULL, 'd'},
  62. {"foreground", 0, NULL, 'f'},
  63. {"help", 0, NULL, 'h'},
  64. {"kill", 2, NULL, 'k'},
  65. {"persistent", 2, NULL, 'p'},
  66. {"version", 0, NULL, 'v'},
  67. {"wait", 0, NULL, 'w'},
  68. { NULL, 0, NULL, 0}
  69. };
  70. server_argv0 = argv[0];
  71. while ((optc = getopt_long( argc, argv, "d::fhk::p::vw", long_options, NULL )) != -1)
  72. {
  73. switch(optc)
  74. {
  75. case 'd':
  76. if (optarg && isdigit(*optarg))
  77. debug_level = atoi( optarg );
  78. else
  79. debug_level++;
  80. break;
  81. case 'f':
  82. foreground = 1;
  83. break;
  84. case 'h':
  85. usage(stdout);
  86. exit(0);
  87. break;
  88. case 'k':
  89. if (optarg && isdigit(*optarg))
  90. ret = kill_lock_owner( atoi( optarg ) );
  91. else
  92. ret = kill_lock_owner(-1);
  93. exit( !ret );
  94. case 'p':
  95. if (optarg && isdigit(*optarg))
  96. master_socket_timeout = (timeout_t)atoi( optarg ) * -TICKS_PER_SEC;
  97. else
  98. master_socket_timeout = TIMEOUT_INFINITE;
  99. break;
  100. case 'v':
  101. fprintf( stderr, "%s\n", PACKAGE_STRING );
  102. exit(0);
  103. case 'w':
  104. wait_for_lock();
  105. exit(0);
  106. default:
  107. usage(stderr);
  108. exit(1);
  109. }
  110. }
  111. }
  112. static void sigterm_handler( int signum )
  113. {
  114. exit(1); /* make sure atexit functions get called */
  115. }
  116. int main( int argc, char *argv[] )
  117. {
  118. setvbuf( stderr, NULL, _IOLBF, 0 );
  119. parse_args( argc, argv );
  120. /* setup temporary handlers before the real signal initialization is done */
  121. signal( SIGPIPE, SIG_IGN );
  122. signal( SIGHUP, sigterm_handler );
  123. signal( SIGINT, sigterm_handler );
  124. signal( SIGQUIT, sigterm_handler );
  125. signal( SIGTERM, sigterm_handler );
  126. signal( SIGABRT, sigterm_handler );
  127. sock_init();
  128. open_master_socket();
  129. if (do_esync())
  130. esync_init();
  131. if (debug_level) fprintf( stderr, "wineserver: starting (pid=%ld)\n", (long) getpid() );
  132. init_scheduler();
  133. init_signals();
  134. init_directories();
  135. init_registry();
  136. init_shared_memory();
  137. init_types();
  138. main_loop();
  139. return 0;
  140. }