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

时间:2013-09-23 14:48:28

标签: linux

基于这个bug,我正在尝试在/ 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);
       }

1 个答案:

答案 0 :(得分: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);
}