简体   繁体   中英

Language C - Get the Process owner

I have to use the EXEC family to find the process owned (the user) and write it in a file.

CODE:

sprintf(buffer, "%d", getpid());

if ((pid = fork()) == -1)
        exit(EXIT_FAILURE);
      else if (pid == 0)
        {
          dup2(fd,STDOUT_FILENO);
          dup2(fd,STDERR_FILENO);

          // function that Write in the file
          fdprintf(fd, "\n%s %s%d secondes %s\n", adornment, toShow, sec, adornment);

          if (execlp("ps", "u", buffer, NULL) == -1)
            show_err("Exelp Error\n");
        }                                                                                                                                                                                                        
      else
        {
          wait(NULL);
          fdprintf(fd, "%s %s%d secondes %s\n", adornment, toShow, sec, adornment);
          close(fd);
        }

any help is appreciated ! Thanks

What you are doing is the thedailywtf equivalent of getuid(2) . If you want to get the login name, you can use getpwuid(3) .

getppid()

get parent pid, check the man pages for more info

pid_t parent_pid = getppid();

最后,我使用EXECVE ...对我来说很好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM