I have a user level program which will take a PID as an input. Before proceeding with executing the program, I need to check if I own that process or not.
I am new to linux, I was wondering if there is a way to check if I own the process..?
Probably the easiest way is to check the owner of the pseudo-directory /proc/PID
.
You can get the owner of a file or directory with the stat()
function. And you can compare it with your own that you get with geteuid()
or getuid()
.
Something along the lines of:
char name[50];
struct stat st;
sprintf(name, "/proc/%d", pid);
stat(name, &st);
printf("uid: %d gid: %d\n", st.st_uid, st.st_gid);
However, you should reconsider why you need that in the first place. If for example you want to send a signal (kill()
) that process, you should not check if you will be able to. You just try it and then consider the case when the operation fails.