Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pid_t proc_find(const char* name)
- {
- DIR* dir;
- struct dirent* ent;
- char* endptr;
- char buf[512];
- if (!(dir = opendir("/proc"))) {
- perror("can't open /proc");
- return -1;
- }
- while((ent = readdir(dir)) != NULL) {
- /* if endptr is not a null character, the directory is not
- * entirely numeric, so ignore it */
- long lpid = strtol(ent->d_name, &endptr, 10);
- if (*endptr != '\0') {
- continue;
- }
- /* try to open the cmdline file */
- snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid);
- FILE* fp = fopen(buf, "r");
- if (fp) {
- if (fgets(buf, sizeof(buf), fp) != NULL) {
- /* check the first token in the file, the program name */
- char* first = strtok(buf, " ");
- if (!strcmp(first, name)) {
- fclose(fp);
- closedir(dir);
- return (pid_t)lpid;
- }
- }
- fclose(fp);
- }
- }
- closedir(dir);
- return -1;
- }
- char* getexename(pid_t pid)
- {
- char *buf[128];
- char linkname[64]; /* /proc/<pid>/exe */
- pid_t pid;
- int ret;
- if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0)
- {
- /* This should only happen on large word systems. I'm not sure
- what the proper response is here.
- Since it really is an assert-like condition, aborting the
- program seems to be in order. */
- return NULL;
- }
- /* Now read the symbolic link */
- ret = readlink(linkname, buf, size);
- /* In case of an error, leave the handling up to the caller */
- if (ret == -1)
- return NULL;
- /* Report insufficient buffer size */
- if (ret >= size)
- {
- errno = ERANGE;
- return NULL;
- }
- /* Ensure proper NUL termination */
- buf[ret] = 0;
- return buf;
- }
- void botkill() {
- int i, status;
- pid_t pid = -1;
- char *botLocation[128];
- char *command[137];
- for (i = 0; i < NUMITEMS(Bot_Killer_Binarys); i++) {
- printf("Scanning for bot %s\n", Bot_Killer_Binarys[i]);
- pid = proc_find(argv[i]);
- if (pid == -1) {
- printf("%s: not found\n", argv[i]);
- } else {
- printf("Killing bot %s PID %d\n", Bot_Killer_Binarys[i], PID);
- kill(PID, 9);
- botLocation = getexename(PID);
- if(botLocation != NULL) {
- printf("%s found at %s. Deleting...\n", Bot_Killer_Binarys[i], botLocation);
- status = remove(botLocation);
- if( status == 0 )
- printf("Bot %s deleted successfully.\n", botLocation);
- } else {
- printf("Unable to delete bot %s.\n", botLocation);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment