stop.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <stdlib.h>
  2. #include <signal.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <grass/gis.h>
  7. #include <grass/glocale.h>
  8. #include "proto.h"
  9. static int stop_wx(const char *);
  10. static int stop(const char *);
  11. int stop_mon(const char *name)
  12. {
  13. if (!check_mon(name)) {
  14. G_fatal_error(_("Monitor <%s> is not running"), name);
  15. }
  16. if (strncmp(name, "wx", 2) == 0)
  17. stop_wx(name);
  18. return stop(name);
  19. }
  20. int stop(const char *name)
  21. {
  22. char *mon_path, file_path[GPATH_MAX];
  23. struct dirent *dp;
  24. DIR *dirp;
  25. mon_path = get_path(name, TRUE);
  26. dirp = opendir(mon_path);
  27. while ((dp = readdir(dirp)) != NULL) {
  28. if (!dp->d_name || dp->d_name[0] == '.')
  29. continue;
  30. sprintf(file_path, "%s/%s", mon_path, dp->d_name);
  31. if (unlink(file_path) == -1)
  32. G_warning(_("Unable to delete file <%s>"), file_path);
  33. }
  34. closedir(dirp);
  35. if (rmdir(mon_path) == -1)
  36. G_warning(_("Unable to delete directory <%s>"), mon_path);
  37. G_free(mon_path);
  38. G_unsetenv("MONITOR");
  39. return 0;
  40. }
  41. int stop_wx(const char *name)
  42. {
  43. char *mon_path, *pid;
  44. char pid_file[GPATH_MAX], buf[512];
  45. FILE *fp;
  46. mon_path = get_path(name, FALSE);
  47. G_file_name(pid_file, mon_path, "pid", G_mapset());
  48. fp = fopen(pid_file, "r");
  49. if (!fp) {
  50. G_warning(_("Unable to open file <%s>"), pid_file);
  51. return 1;
  52. }
  53. pid = NULL;
  54. if (G_getl2(buf, sizeof(buf) - 1, fp) != 0)
  55. pid = G_store(buf);
  56. fclose(fp);
  57. if (!pid) {
  58. G_warning(_("Unable to read file <%s>"), pid_file);
  59. return 1;
  60. }
  61. #ifdef __MINGW32__
  62. /* TODO */
  63. #else
  64. if (kill((pid_t) atoi(pid), SIGTERM) != 0) {
  65. /* G_fatal_error(_("Unable to stop monitor <%s>"), name); */
  66. }
  67. #endif
  68. return 0;
  69. }