stop.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdlib.h>
  2. #include <signal.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "proto.h"
  7. static void clean_env(const char *);
  8. static int stop_wx(const char *);
  9. static int stop(const char *);
  10. int stop_mon(const char *name)
  11. {
  12. if (!check_mon(name)) {
  13. clean_env(name);
  14. G_fatal_error(_("Monitor <%s> is not running"), name);
  15. }
  16. if (strncmp(name, "wx", 2) == 0)
  17. return stop_wx(name);
  18. return stop(name);
  19. }
  20. int stop(const char *name)
  21. {
  22. char *env_name;
  23. const char *env_file;
  24. env_name = NULL;
  25. G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name));
  26. env_file = G_getenv_nofatal(env_name);
  27. if (!env_file)
  28. G_warning(_("Env file not found"));
  29. clean_env(name);
  30. return 0;
  31. }
  32. int stop_wx(const char *name)
  33. {
  34. char *env_name;
  35. const char *pid;
  36. env_name = NULL;
  37. G_asprintf(&env_name, "MONITOR_%s_PID", G_store_upper(name));
  38. pid = G_getenv_nofatal(env_name);
  39. if (!pid) {
  40. clean_env(name);
  41. G_fatal_error(_("PID file not found"));
  42. }
  43. #ifdef __MINGW32__
  44. /* TODO */
  45. #else
  46. if (kill((pid_t) atoi(pid), SIGTERM) != 0) {
  47. /* G_fatal_error(_("Unable to stop monitor <%s>"), name); */
  48. }
  49. #endif
  50. clean_env(name);
  51. return 0;
  52. }
  53. void clean_env(const char *name)
  54. {
  55. int i;
  56. char *u_name;
  57. const char *env_prefix = "MONITOR_";
  58. const char *env;
  59. int env_prefix_len;
  60. char **tokens;
  61. u_name = G_store_upper(name);
  62. env_prefix_len = strlen(env_prefix);
  63. tokens = NULL;
  64. for (i = 0; (env = G_get_env_name(i)); i++) {
  65. if (strncmp(env_prefix, env, env_prefix_len) != 0)
  66. continue;
  67. tokens = G_tokenize(env, "_");
  68. if (G_number_of_tokens(tokens) != 3 ||
  69. strcmp(tokens[1], u_name) != 0)
  70. continue;
  71. G_unsetenv(env);
  72. i--; /* env has been removed for the list */
  73. G_free_tokens(tokens);
  74. tokens = NULL;
  75. }
  76. G_unsetenv("MONITOR");
  77. }