/bin/ej-contests-control.c

https://github.com/blackav/ejudge · C · 180 lines · 126 code · 30 blank · 24 comment · 22 complexity · 573b2b4e5c5eda8d3f063361efdb98c9 MD5 · raw file

  1. /* -*- mode: c -*- */
  2. /* Copyright (C) 2006-2015 Alexander Chernov <cher@ejudge.ru> */
  3. /*
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include "ejudge/config.h"
  15. #include "ejudge/ej_types.h"
  16. #include "ejudge/version.h"
  17. #include "ejudge/ejudge_cfg.h"
  18. #include "ejudge/new_server_proto.h"
  19. #include "ejudge/new_server_clnt.h"
  20. #include "ejudge/startstop.h"
  21. #include "ejudge/xalloc.h"
  22. #include "ejudge/osdeps.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. /*
  29. * usage: ej-contests-control COMMAND CONFIG
  30. * COMMAND is one of `stop', `restart', `status'
  31. */
  32. static const unsigned char *program_name = "";
  33. static void startup_error(const char *format, ...)
  34. __attribute__((format(printf, 1, 2), noreturn));
  35. static void
  36. startup_error(const char *format, ...)
  37. {
  38. va_list args;
  39. char buf[1024];
  40. va_start(args, format);
  41. vsnprintf(buf, sizeof(buf), format, args);
  42. va_end(args);
  43. fprintf(stderr, "%s: %s\n Use --help option for help.\n", program_name,
  44. buf);
  45. exit(1);
  46. }
  47. static void op_error(const char *format, ...)
  48. __attribute__((format(printf, 1, 2), noreturn));
  49. static void
  50. op_error(const char *format, ...)
  51. {
  52. va_list args;
  53. char buf[1024];
  54. va_start(args, format);
  55. vsnprintf(buf, sizeof(buf), format, args);
  56. va_end(args);
  57. fprintf(stderr, "%s: %s\n", program_name, buf);
  58. exit(1);
  59. }
  60. static void write_help(void) __attribute__((noreturn));
  61. static void
  62. write_help(void)
  63. {
  64. printf("%s: ej-contests control utility\n"
  65. "Usage: %s [OPTIONS] COMMAND [EJUDGE-XML-PATH]\n"
  66. " OPTIONS:\n"
  67. " --help write message and exit\n"
  68. " --version report version and exit\n"
  69. " COMMAND:\n"
  70. " stop stop the ej-contests\n"
  71. " restart restart the ej-contests\n"
  72. /*" status report the ej-contests status\n"*/,
  73. program_name, program_name);
  74. exit(0);
  75. }
  76. static void write_version(void) __attribute__((noreturn));
  77. static void
  78. write_version(void)
  79. {
  80. printf("%s %s, compiled %s\n", program_name, compile_version, compile_date);
  81. exit(0);
  82. }
  83. int
  84. main(int argc, char *argv[])
  85. {
  86. int i = 1, r = 0;
  87. const char *command = 0;
  88. const char *ejudge_xml_path = 0;
  89. struct ejudge_cfg *config = 0;
  90. int cmd = 0;
  91. new_server_conn_t conn = 0;
  92. int pid;
  93. int signum = 0;
  94. const unsigned char *signame = "";
  95. program_name = os_GetBasename(argv[0]);
  96. if (argc < 2) startup_error("not enough parameters");
  97. if (!strcmp(argv[i], "--help")) {
  98. write_help();
  99. } else if (!strcmp(argv[i], "--version")) {
  100. write_version();
  101. }
  102. command = argv[i];
  103. i++;
  104. if (i < argc) {
  105. ejudge_xml_path = argv[i];
  106. i++;
  107. }
  108. if (i < argc) startup_error("too many parameters");
  109. #if defined EJUDGE_XML_PATH
  110. if (!ejudge_xml_path) ejudge_xml_path = EJUDGE_XML_PATH;
  111. #endif /* EJUDGE_XML_PATH */
  112. if (!ejudge_xml_path) startup_error("ejudge.xml path is not specified");
  113. if (!(config = ejudge_cfg_parse(ejudge_xml_path, 0))) return 1;
  114. #if defined EJUDGE_NEW_SERVER_SOCKET
  115. if (!config->new_server_socket)
  116. config->new_server_socket = xstrdup(EJUDGE_NEW_SERVER_SOCKET);
  117. #endif
  118. if (!config->new_server_socket)
  119. config->new_server_socket = xstrdup(EJUDGE_NEW_SERVER_SOCKET_DEFAULT);
  120. if (!strcmp(command, "stop")) {
  121. cmd = NEW_SRV_CMD_STOP;
  122. signum = START_STOP;
  123. signame = "TERM";
  124. } else if (!strcmp(command, "restart")) {
  125. cmd = NEW_SRV_CMD_RESTART;
  126. signum = START_RESTART;
  127. signame = "HUP";
  128. } else {
  129. startup_error("invalid command");
  130. }
  131. (void) signame;
  132. (void) signum;
  133. if (!(pid = start_find_process("ej-contests", 0))) {
  134. op_error("ej-contests is not running");
  135. } else if (pid > 0) {
  136. /*
  137. fprintf(stderr, "%s: ej-contests is running as pid %d\n",
  138. program_name, pid);
  139. fprintf(stderr, "%s: sending it the %s signal\n", program_name, signame);
  140. if (start_kill(pid, signum) < 0) op_error("failed: %s", os_ErrorMsg());
  141. return 0;
  142. */
  143. }
  144. r = new_server_clnt_open(config->new_server_socket, &conn);
  145. if (r == -NEW_SRV_ERR_CONNECT_FAILED) op_error("ej-contests is not running");
  146. if (r < 0) op_error("%s", ns_strerror(-r));
  147. r = new_server_clnt_control(conn, cmd);
  148. if (r < 0) op_error("%s", ns_strerror(-r));
  149. return 0;
  150. }