Guest User

Untitled

a guest
Jan 14th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <memory.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <ctype.h>
  8. int exit_signal;
  9. char *killing_name;
  10.  
  11. void fail(char *str)
  12. {
  13. free(killing_name);
  14. fprintf(stderr, "killall: process is ended: %s\n", str);
  15. exit(EXIT_FAILURE);
  16. }
  17. void cant_kill(char *str, int pid)
  18. {
  19. fprintf(stderr, "Can't kill process %s with pid = %d\n", str, pid);
  20. }
  21. void *safe_malloc(int size)
  22. {
  23. void* res = malloc(size);
  24. if (res == NULL)
  25. fail("Out of memory");
  26. return res;
  27. }
  28. void *safe_realloc(void* adress, int new_size)
  29. {
  30. void* temp = realloc(adress, new_size);
  31. if (temp == NULL)
  32. fail("Out of memory");
  33. return temp;
  34. }
  35. void parser(int argc, char **argv)
  36. {
  37. if (argc != 3)
  38. fail("Wrong params");
  39. int i = 0;
  40. char *end;
  41. exit_signal = strtoul(argv[1], &end, 10);
  42. if (*end == 0)
  43. fail("Wrong params");
  44. sprintf(killing_name, "(%s)", argv[2]);
  45. }
  46. int max(int a, int b)
  47. {
  48. return a > b? a: b;
  49. }
  50. char is_process(char str[256])
  51. {
  52. int i = 0;
  53. while (i != 256 && str[i] != '\0')
  54. {
  55. if (!isdigit(str[i]))
  56. return 0;
  57. i++;
  58. }
  59. return 1;
  60. }
  61. char *get_stat(char* process_name)
  62. {
  63. char *result = safe_malloc(strlen(process_name) + 15);
  64. sprintf(result, "/proc/%s/stat", process_name);
  65. return result;
  66. }
  67. void killall(int code, char* name)
  68. {
  69. DIR *dir = opendir("/proc");
  70. FILE* file;
  71. struct dirent *curr;
  72. char *file_name, *buf;
  73. char process_name[256];
  74. int pid;
  75. char result_of_killing;
  76. while ((curr = readdir(dir)) != NULL)
  77. {
  78. if (is_process(curr->d_name))
  79. {
  80. file_name = get_stat(curr->d_name);
  81. //getting pid and name
  82. file = fopen(file_name, "r");
  83. if (file != NULL)
  84. {
  85. fscanf(file, "%d\n%s\n", &pid, process_name);
  86. if (strcmp(process_name, killing_name) == 0)
  87. {
  88. result_of_killing = kill(pid, exit_signal);
  89. if (result_of_killing == 0)
  90. cant_kill(process_name, pid);
  91. }
  92. fclose(file);
  93. }
  94. }
  95. }
  96. }
  97. void allocating_memory(int argc, char **argv)
  98. {
  99. int i, j, size = 0;
  100. for (i = 1; i < argc; i++)
  101. {
  102. j = 0;
  103. while (argv[i][j] != '\0')
  104. j++;
  105. size = max(size, j);
  106. }
  107. killing_name = safe_malloc(size * sizeof(char) + 3);// +'(' + ')' + '\0'
  108.  
  109. }
  110. int main(int argc, char **argv)
  111. {
  112. allocating_memory(argc, argv);
  113. parser(argc, argv);
  114. killall(exit_signal, killing_name);
  115. free(killing_name);
  116. return EXIT_SUCCESS;
  117. }
Add Comment
Please, Sign In to add comment