Skip to content

Commit

Permalink
#51: process:c add get_process_tty()
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdope committed Feb 24, 2021
1 parent 4a65f7f commit 521f34b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include "process.h"

/**
Expand Down Expand Up @@ -73,4 +74,35 @@ void get_process_parent_id(const pid_t pid, pid_t * ppid) {
}
fclose(fp);
}
}

/**
* Get TTY for process from its PID.
* @param pid PID of the process
* @param name Will receive TTY of the process
*
* @author Tobias Bäumer <tobiasbaeumer@gmail.com>
*/
void get_process_tty(const pid_t pid, char * name) {
DIR *dir;
struct dirent *entry;
char procdir[BUFSIZ];
sprintf(procdir, "/proc/%d/fd", pid);

if (!(dir = opendir(procdir))) {
return;
}

while ((entry = readdir(dir)))
{
if (entry->d_type == DT_LNK && strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) {
if (strstr(entry->d_name, "/dev/") != NULL) {
realpath(entry->d_name, name);

break;
}
}
}

closedir(dir);
}
2 changes: 2 additions & 0 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ void get_process_name(const pid_t pid, char * name);

void get_process_parent_id(const pid_t pid, pid_t * ppid);

void get_process_tty(const pid_t pid, char * name);

#endif /* !PUSB_PROCESS_H_ */

0 comments on commit 521f34b

Please sign in to comment.