在linux系统调用中查找用户名 [英] finding the username in a linux system call
问题描述
我向Linux内核添加了一个如下所示的系统调用:
I have added a system call to Linux kernel that looks like this:
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/cred.h>
#include <asm/uaccess.h>
asmlinkage int sys_os_pid_to_uid(int pid, int* puid)
{
struct task_struct* task;
rcu_read_lock();
for_each_process(task)
{
if (task->pid == (pid_t) pid)
{
copy_to_user(puid, &(task->cred->uid.val), sizeof(task->cred->uid.val));
}
}
rcu_read_unlock();
return 0;
}
它获取进程ID(pid
),并确定执行该进程的用户的用户ID(puid
).
It gets a process ID (pid
) and determines the user ID (puid
) of the user who is executing it.
现在,我的问题开始了:我想向该系统调用添加另一个参数,即char *username
,它将包含ID为puid
的用户的用户名.我在cred.h
标头中搜索,但找不到与用户名有关的任何内容.
谁能帮我解决这个问题?
Now, my problem begins: I want to add another argument to this system call, that is, char *username
, which will contain the username of the user whose ID is puid
. I searched in the cred.h
header but was unable to find anything concerned with the username.
Can anyone help me solve this?
推荐答案
之所以没有找到它,是因为其中没有一个:task_struct
,并且通常来说,内核不保留内存中的信息.关于与给定用户ID对应的用户名.这是完全不必要的,会给界面和代码增加混乱,并且会消耗内存,而没有任何额外的好处:与用户有关的所有内容都使用数字用户ID表示和存储.比较容易,并且可以节省内存.
The reason you didn't find it is because there isn't one: task_struct
, and the kernel in general, does not keep in-memory information about the username corresponding to a given user ID. That is utterly unnecessary, would add clutter to the interfaces and code, and would consume memory without any added benefit: everything regarding users is represented and stored with numeric user ids. Comparison is easy and memory is saved.
您不应/不能在系统调用中执行此操作.
You shouldn't / can't do this in a syscall.
我建议您编写一个包装程序用户空间函数,该函数调用您的syscall,然后调用getpwuid(3)
或getpwuid_r(3)
将用户ID转换为用户名.请注意,这是在用户空间而不是内核空间中完成的.
I suggest you write a wrapper user-space function that invokes your syscall and then calls getpwuid(3)
or getpwuid_r(3)
to convert from the user ID to the username. Note that this is to be done in user-space, not in kernel space.
这篇关于在linux系统调用中查找用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!