Linux 如何在/var/run/utmp中写入一个条目?

Linux 如何在/var/run/utmp中写入一个条目?,linux,Linux,基于这个bug,我试图在/var/run/utmp中编写一个条目来模拟这个问题,我尝试使用取自手册页的代码,但没有成功 #include <string.h> #include <stdlib.h> #include <pwd.h> #include <unistd.h> #include <utmp.h> int main(int argc, char *argv[]) { struct utm

基于这个bug,我试图在/var/run/utmp中编写一个条目来模拟这个问题,我尝试使用取自手册页的代码,但没有成功

#include <string.h>
#include <stdlib.h>
#include <pwd.h>
   #include <unistd.h>
   #include <utmp.h>

  int
  main(int argc, char *argv[])
  {
       struct utmp entry;

       system("echo before adding entry:;who");

       entry.ut_type = USER_PROCESS;
       entry.ut_pid = getpid();
       strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
       /* only correct for ptys named /dev/tty[pqr][0-9a-z] */
       strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
       time(&entry.ut_time);
       strcpy(entry.ut_user, getpwuid(getuid())->pw_name);
       memset(entry.ut_host, 0, UT_HOSTSIZE);
       entry.ut_addr = 0;
       setutent();
       pututline(&entry);

       system("echo after adding entry:;who");
       exit(EXIT_SUCCESS);
       }

我用这个代码解决了我的问题

#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <utmp.h>

int
main(int argc, char *argv[])
{
       struct utmp entry;

       system("echo before adding entry:;who");

       entry.ut_type = USER_PROCESS;
       entry.ut_pid = getpid();
       strcpy(entry.ut_line, ttyname(STDIN_FILENO) + strlen("/dev/"));
       /* only correct for ptys named /dev/tty[pqr][0-9a-z] */
       strcpy(entry.ut_id, ttyname(STDIN_FILENO) + strlen("/dev/tty"));
       time(&entry.ut_time);
       //strcpy(entry.ut_user, getpwuid(getuid())->pw_name);
       strcpy(entry.ut_user, "pippo");
       memset(entry.ut_host, 0, UT_HOSTSIZE);
       entry.ut_addr = 0;
       setutent();
       pututline(&entry);

       system("echo after adding entry:;who");
       exit(EXIT_SUCCESS);
}