stop.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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", name);
  26. env_file = G__getenv(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", name);
  38. pid = G__getenv(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. const char *env_prefix = "MONITOR_";
  57. const char *env;
  58. int env_prefix_len;
  59. char **tokens;
  60. env_prefix_len = strlen(env_prefix);
  61. tokens = NULL;
  62. for (i = 0; (env = G__env_name(i)); i++) {
  63. if (strncmp(env_prefix, env, env_prefix_len) != 0)
  64. continue;
  65. tokens = G_tokenize(env, "_");
  66. if (G_number_of_tokens(tokens) != 3 ||
  67. strcmp(tokens[1], name) != 0)
  68. continue;
  69. G_unsetenv(env);
  70. i--; /* env has been removed for the list */
  71. G_free_tokens(tokens);
  72. tokens = NULL;
  73. }
  74. G_unsetenv("MONITOR");
  75. }