Linux System Calls' Forum, #2:(第199,201,200,202,224號系統服務) sys_getuid, sys_geteuid, sys_getgid, sys_getegid, sys_gettid

jollen 發表於 October 12, 2006 12:57 PM


199 sys_getuid linux/kernel/timer.c
類別:Kernel Timer & Process
原型宣告:long sys_getuid(void);
用途說明:取得目前 process 的 real user ID (UID)。
Kernel (2.6.11 or above) 實作:
asmlinkage long sys_getuid(void)
{
        /* Only we change this so SMP safe */
        return current->uid;
}

201 sys_geteuid linux/kernel/timer.c
類別:Kernel Timer & Process
原型宣告:long sys_geteuid(void);
用途說明:取得目前 process 的 effective real user ID (file ID bit)。
Kernel (2.6.11 or above) 實作:
asmlinkage long sys_geteuid(void)
{
        /* Only we change this so SMP safe */
        return current->euid;
}

Jollen 的說明

struct task_struct 裡紀錄了 task (process) 的所有資訊,其中我們最為熟悉的 process ID 與 user ID (and group ID) 都可以由這個資料結構裡取得。讓我們來看看 wrapper function 'getuid' 的用法 (from man):

NAME
       getuid, geteuid - get user identity
                                                                                
SYNOPSIS
       #include 
       #include 
 
       uid_t getuid(void);
       uid_t geteuid(void);
 
DESCRIPTION
       getuid returns the real user ID of the current process.
 
       geteuid returns the effective user ID of the current process.
 
       The  real  ID corresponds to the ID of the calling process.  The effec-
       tive ID corresponds to the set ID bit on the file being executed.

'getuid' 所對應的 system call service 為 sys_getuid,讓我們來看一下 Linux 的實作:

asmlinkage long sys_getuid(void)
{
        /* Only we change this so SMP safe */
        return current->uid;
}

嗯,果然是非常的好懂,只要懂 current 巨集的用途即可!另外,要提醒的是,'current->uid' 並非直接 return 給 user-space 的 wrapper function (getuid in our example)。(那是 return 給誰?)

在 Linux 裡有許多類似 getuid 的 system call 實作,比如 sys_geteuid 的程式碼也是直接回傳 struct task_struct 裡的 field,並不難懂。

以下是 sys_getgidsys_getegid 的整理。

200 sys_getgid linux/kernel/timer.c
類別:Kernel Timer & Process
原型宣告:long sys_getgid(void);
用途說明:取得目前 process 的 real group ID (GID)。
Kernel (2.6.11 or above) 實作:
asmlinkage long sys_getgid(void)
{
        /* Only we change this so SMP safe */
        return current->gid;
}

202 sys_getegid linux/kernel/timer.c
類別:Kernel Timer & Process
原型宣告:long sys_getegid(void);
用途說明:取得目前 process 的 effective group ID (EGID)。
Kernel (2.6.11 or above) 實作:
asmlinkage long sys_getegid(void)
{
        /* Only we change this so SMP safe */
        return  current->egid;
}

這 4 個 Linux system call 都非常的容易了解,由 Linux System Calls' Forum #1,#2 我們了解到 2 個重要的 Kernel 議題:

  • 1. struct task_struct;
  • 2. The current macro.

最後還有一個 sys_gettid 的實作。

224 sys_gettid linux/kernel/timer.c
類別:Kernel Timer & Process
原型宣告:long sys_gettid(void);
用途說明:取得 task 的 "pid"。
Kernel (2.6.11 or above) 實作:
/* Thread ID - the internal kernel "pid" */
asmlinkage long sys_gettid(void)
{
        return current->pid;
}

Jollen's Blog 使用 Github issues 與讀者交流討論。請點擊上方的文章專屬 issue,或 open a new issue

您可透過電子郵件 jollen@jollen.org,或是 Linkedin 與我連絡。更歡迎使用微信,請搜尋 WeChat ID:jollentw