gpt4 book ai didi

linux - 如何在/var/run/utmp中写入一项?

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:16 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于linux - 如何在/var/run/utmp中写入一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18978566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com