1

基于这个错误,我试图在 /var/run/utmp 中编写一个条目来模拟问题http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=528060,我尝试使用此代码取自联机帮助页,但没有用

#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);
       }
4

1 回答 1

0

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

#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);
}
于 2013-09-23T15:37:40.723 回答