init.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* $Id: init.c,v 1.26 2001/05/04 14:36:09 kilobug Exp $ */
  2. #include "mclient.h"
  3. #include <sys/wait.h>
  4. #include <time.h>
  5. #include <locale.h>
  6. #define MYSQL_PASSWORD "pr1L0g1N"
  7. #define MYSQL_LOGIN "bitmap"
  8. #define MYSQL_PORT 3306
  9. #define MYSQL_HOST "drizzt"
  10. #define MYSQL_DB "Finale"
  11. /*
  12. #define MYSQL_HOST "pantera.prologin"
  13. #define MYSQL_DB "finale"
  14. */
  15. #define GFX_CLIENT "gtk-client"
  16. #define SERVER "server"
  17. void byebye(GtkWidget *widget, void *data)
  18. {
  19. gtk_main_quit();
  20. }
  21. static void sigchild()
  22. {
  23. wait(NULL);
  24. }
  25. static void clean_abort()
  26. {
  27. exit(2);
  28. }
  29. static void clean_quit()
  30. {
  31. set_my_status(STATUT_ABSENT);
  32. }
  33. void set_my_status(int stat)
  34. {
  35. char buf[MYSQL_BUFFER_SIZE];
  36. g_snprintf(buf, MYSQL_BUFFER_SIZE,
  37. "UPDATE Bitmaps SET statut=%d, machine='%s' WHERE id=%d",
  38. stat, gl_config->hostname, getuid());
  39. mysql_query(gl_config->mysql, buf);
  40. }
  41. static void get_args(int argc, char **argv, conf_t *res)
  42. {
  43. int i;
  44. res->db = MYSQL_DB;
  45. res->host = NULL;
  46. for (i = 1; i < argc - 1; i++)
  47. {
  48. if (!strcmp(argv[i], "-r"))
  49. res->host = argv[++i];
  50. else
  51. if (!strcmp(argv[i], "-d"))
  52. res->db = argv[++i];
  53. else
  54. fprintf(stderr, "Paramètre inconnu: %s\n", argv[i]);
  55. }
  56. }
  57. void init(int argc, char **argv)
  58. {
  59. conf_t *res;
  60. char *passwd, c;
  61. int i;
  62. srandom(time(NULL));
  63. res = g_malloc(sizeof(*res));
  64. res->hostname = g_malloc(MYSQL_BUFFER_SIZE);
  65. gethostname(res->hostname, MYSQL_BUFFER_SIZE);
  66. res->bindir = g_dirname(argv[0]);
  67. if (g_path_is_absolute(SERVER) || (res->bindir[0] != argv[0][0]))
  68. res->server = SERVER;
  69. else
  70. res->server = g_strconcat(res->bindir, G_DIR_SEPARATOR_S, SERVER,
  71. NULL);
  72. if (g_path_is_absolute(GFX_CLIENT) || (res->bindir[0] != argv[0][0]))
  73. res->gfx_client = GFX_CLIENT;
  74. else
  75. res->gfx_client = g_strconcat(res->bindir, G_DIR_SEPARATOR_S, GFX_CLIENT,
  76. NULL);
  77. get_args(argc, argv, res);
  78. res->mysql = NULL;
  79. res->matl = res->bmpl = res->mapl = res->prgl = res->result = NULL;
  80. res->mapa = res->prga = NULL;
  81. res->window = NULL;
  82. for (i = 0; i < NOTEBOOK_PAGE_NUM; i++)
  83. res->menunb[i] = NULL;
  84. res->mysql = mysql_init(NULL);
  85. mysql_options(res->mysql, MYSQL_READ_DEFAULT_GROUP, "meta-client");
  86. passwd = g_strdup(MYSQL_PASSWORD);
  87. for (i = 0; i < strlen(passwd); i += 2)
  88. {
  89. c = passwd[i];
  90. passwd[i] = passwd[i + 1];
  91. passwd[i + 1] = c;
  92. }
  93. if (mysql_real_connect(res->mysql, MYSQL_HOST, MYSQL_LOGIN, passwd,
  94. res->db, MYSQL_PORT, NULL, 0) == NULL)
  95. {
  96. fprintf(stderr, "Failed to connect to database: Error: %s\n",
  97. mysql_error(res->mysql));
  98. exit(1);
  99. }
  100. gl_config = res;
  101. set_my_status(STATUT_PRESENT);
  102. i = mysql_get_int(gl_config->mysql,
  103. "SELECT id FROM Bitmaps WHERE id = %d",
  104. getuid());
  105. if (i != getuid())
  106. {
  107. fprintf(stderr, "You're nobody. Go away.\n");
  108. exit(1);
  109. }
  110. res->staff = mysql_get_int(gl_config->mysql,
  111. "SELECT staff FROM Bitmaps WHERE id = %d",
  112. getuid());
  113. atexit(clean_quit);
  114. signal(SIGINT, clean_abort);
  115. signal(SIGHUP, clean_abort);
  116. signal(SIGQUIT, clean_abort);
  117. signal(SIGABRT, clean_abort);
  118. signal(SIGTERM, clean_abort);
  119. signal(SIGCHLD, sigchild);
  120. setlocale(LC_ALL, "fr_FR");
  121. gtk_set_locale();
  122. setlocale(LC_NUMERIC, "C");
  123. gtk_init(&argc, &argv);
  124. }