Linux系统调用始末续...

在上次的getpid系统调用中,发现getpid函数只能第一次执行进入系统调用,后面的就直接执行,似乎没利用系统调用。

先查一下直接利用int $0x80的系统调用流程。

函数如下:

int GetpidAsm(int argc, char **argv)
{
    pid_t pid;
    asm volatile(
    "mov $20, %%eax\n\t"
    "int $0x80\n\t"
    "mov %%eax, %0\n\t"
    :"=m"(pid)
    );
    printf("current process's pid(ASM):%d\n",pid);
    return 0;
}

在系统调用执行的时候,函数就停在了设置的断点sys_getpid处,如下图:

图中的SYSCALL_DEFINE宏甚是显眼,有资料解释如下:

It is used (obviously) to define the given block of code as a system call. For example, fs/ioctl.c has the following code :

SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
{
/* do freaky ioctl stuff */
}

Such a definition means that the ioctl syscall is declared and takes three arguments. The number next to the SYSCALL_DEFINE means the number of arguments. For example, in the case of getpid(void), declared in kernel/timer.c, we have the following code :

SYSCALL_DEFINE0(getpid)
{
        return task_tgid_vnr(current);
}

只不过getpid(void)现在的位置挪到了kernel/sys.c中。

  • 逻辑似乎清晰了些,不妨去查查SYSCALL_DEFINE的水表。

Linux/include/linux/syscalls.h这个目录下,我们可以看到

Linux系统调用之SYSCALL_DEFINE已经有前辈解释了这样做的目的,以及为什么要这样。不得不服其中的精妙。

这就是为什么,在我们使用getpid()这个函数的时候,我们并不知道系统究竟做了什么,因为系统里面并没有这个函数的直接实现。而是通过一堆宏定义在预处理的时候展开。

那么索性也来做一次展开,对getpid的展开。
系统停在断点sys_time的时候,代码停在了这个位置:

   │816     SYSCALL_DEFINE0(getpid)                                       
b+>│817     {                                                             
   │818             return task_tgid_vnr(current);                        
   │819     }    

宏展开规则如下:

175 #define SYSCALL_METADATA(sname, nb, ...)
...
178 #define SYSCALL_DEFINE0(sname)                                  \
179         SYSCALL_METADATA(_##sname, 0);                          \
180         asmlinkage long sys_##sname(void)

使用的宏为SYSCALL_DEFINE0(getpid) ->asmlinkage long sys_getpid(void);
显然,展开之后的函数变为:

   │816     asmlinkage long sys_getpid(void)                                       
b+>│817     {                                                             
   │818             return task_tgid_vnr(current);                        
   │819     }    

这个时候,对sys_getpid()的调用一目了然。
那么,显然,接下来的调用是传入的参数current,不是很明白。

   │10      DECLARE_PER_CPU(struct task_struct *, current_task);          
   │11                                                                    
   │12      static __always_inline struct task_struct *get_current(void)  
   │13      {                                                             
  >│14              return this_cpu_read_stable(current_task);            
   │15      }                                                             
   │16                                                                    
   │17      #define current get_current() 

#define this_cpu_read_stable(var) percpu_from_op("mov", var, "p" (&(var)))只是一个宏,在单CPU上,应该没有效果。

  • 接下来执行到task_tgid_vnr
   │1770    static inline pid_t task_tgid_vnr(struct task_struct *tsk)    
   │1771    {                                                             
   │1772            return pid_vnr(task_tgid(tsk));                       
   │1773    }                                                             
  • 处理传入的参数task_tgid,实际上返回了一个结构体pid。
   │1708    static inline struct pid *task_tgid(struct task_struct *task) 
   │1709    {                                                             
  >│1710            return task->group_leader->pids[PIDTYPE_PID].pid;     
   │1711    }                                                             
  • upid、pid、pid_link定义如下:
 44 /*
 45  * struct upid is used to get the id of the struct pid, as it is
 46  * seen in particular namespace. Later the struct pid is found with
 47  * find_pid_ns() using the int nr and struct pid_namespace *ns.
 48  */
 49 
 50 struct upid {
 51         /* Try to keep pid_chain in the same cacheline as nr for find_vpid */
 52         int nr;
 53         struct pid_namespace *ns;
 54         struct hlist_node pid_chain;
 55 };
 56 
 57 struct pid
 58 {
 59         atomic_t count;
 60         unsigned int level;
 61         /* lists of tasks that use this pid */
 62         struct hlist_head tasks[PIDTYPE_MAX];
 63         struct rcu_head rcu;
 64         struct upid numbers[1];
 65 };
 66 
 67 extern struct pid init_struct_pid;
 68 
 69 struct pid_link
 70 {
 71         struct hlist_node node;
 72         struct pid *pid;
 73 };
  • pid_vnr实际上调用了pid_nr_ns,传入了一个task_active_pid_ns来获取namespace,不太懂。。。
    其实最后返回的就是pid_nr_ns返回的nr;
   │497     pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)    
   │498     {                                                             
   │499             struct upid *upid;                                    
   │500             pid_t nr = 0;                                         
   │501                                                                   
   │502             if (pid && ns->level <= pid->level) {                 
   │503                     upid = &pid->numbers[ns->level];              
   │504                     if (upid->ns == ns)                           
   │505                             nr = upid->nr;                        
   │506             }                                                     
   │507             return nr;                                            
   │508     }                                                             
   │509     EXPORT_SYMBOL_GPL(pid_nr_ns);                                    
   │510                                                                  
   │511     pid_t pid_vnr(struct pid *pid)                                
  >│512     {                                                             
   │513             return pid_nr_ns(pid, task_active_pid_ns(current));   
   │514     }  
  • 获取namespace
   │542     struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
   │543     {                                                             
  >│544             return ns_of_pid(task_pid(tsk));                      
   │545     }                                                             
   │546     EXPORT_SYMBOL_GPL(task_active_pid_ns);                        
   │124     /*                                                            
   │125      * ns_of_pid() returns the pid namespace in which the specifie
   │126      * allocated.                                                 
   │127      *                                                            
   │128      * NOTE:                                                      
   │129      *      ns_of_pid() is expected to be called for a process (task) that has
   │130      *      an attached 'struct pid' (see attach_pid(), detach_pid()) i.e @pid
   │131      *      is expected to be non-NULL. If @pid is NULL, caller should handle
   │132      *      the resulting NULL pid-ns.                            
   │133      */    
   │134     static inline struct pid_namespace *ns_of_pid(struct pid *pid)
   │135     {                                                             
   │136             struct pid_namespace *ns = NULL;                      
  >│137             if (pid)                                              
   │138                     ns = pid->numbers[pid->level].ns;             
   │139             return ns;                                            
   │140     }                                                                                                                    
  • 数据结构太复杂,有些关键的地方并不理解什么意思。从字面上理解,分析到这里的时候,并没有发现有猫腻。

突然有个想法,进程本身并没有有局部变量或者全局变量来保存这个pid的值,因为没必要(进程结束回收后,进程号直接作废了,每次启动的时候都会分配不同的pid)。

那么会不会是编译器的原因,这个值放在了寄存器中了?毕竟是1号进程,这个值以后也不再会变动了,编译器发现1号进程的pid不会变化,把这个值缓存起来了?每次需要读取的时候,直接从这里拿?

在我们使用标准API的时候,一般都会包含unistd.h这个头文件。
我们需要的信息都隐藏在这里面。

unistd.h 中所定义的接口通常都是大量针对 系统调用的封装(英语:wrapper functions),如 fork、pipe 以及各种 I/O 原语(read、write、close 等等)

还没有好的思路,下次再写。。


stackoverflow上一个大牛的回答,还没有完全理解。
What is better “int 0x80” or “syscall”?

My answer here covers your question.
In practice, recent kernels are implementing a VDSO, notably to dynamically optimize system calls (the kernel sets the VDSO to some code best for the current processor). So you should use the VDSO, and you'll better use, for existing syscalls, the interface provided by the libc.
Notice that, AFAIK, a significant part of the cost of simple syscalls is going from user-space to kernel and back. Hence, for some syscalls (probably gettimeofday, getpid...) the VDSO might avoid even that (and technically might avoid doing a real syscall). For most syscalls (like open, read, send, mmap ....) the kernel cost of the syscall is large enough to make any improvement of the user-space to kernel space transition (e.g. using SYSENTER or SYSCALL machine instructions instead of INT) insignificant.

  • 注意这一句:
    **Hence, for some syscalls (probably gettimeofday, getpid...) the VDSO might avoid even that (and technically might avoid doing a real syscall). **

  • 大牛的回答,要看这么多东西,给跪了。

System calls Implementation

It is explained in Linux Assembly Howto. And you should read wikipedia syscall page (and also about VDSO), and also intro(2) & syscalls(2) man pages. See also this answer and this one. Look also inside Gnu Libc & musl-libc source code. Learn also to use strace
to find out which syscalls are made by a given command or process.
See also the calling conventions and Application Binary Interface specification relevant to your system. For x86-64 it is here.

  • 又见一出资料,显示,getpid 缓存了pids

C library/kernel differences
Since glibc version 2.3.4, the glibc wrapper function for getpid() caches PIDs, so as to avoid additional system calls when a process calls getpid() repeatedly. Normally this caching is invisible, but its correct operation relies on support in the wrapper functions for fork(2), vfork(2), and clone(2): if an application bypasses the glibc wrappers for these system calls by using syscall(2), then a call to getpid() in the child will return the wrong value (to be precise: it will return the PID of the parent process). See also clone(2) for discussion of a case where getpid() may return the wrong value even when invoking clone(2) via the glibc wrapper function.

思路断掉了,不知道该怎么捋清楚。

但是在使用API getpid的时候,是如何和联系上系统调用呢?两者是如何对应起来的呢?glibc中有如下的代码:

pid_t getpid(void)
{
pid_t (f)(void);
f = (pid_t (
)(void)) dlsym (RTLD_NEXT, "getpid");
if (f == NULL)
error (EXIT_FAILURE, 0, "dlsym (RTLD_NEXT, "getpid"): %s", dlerror ());
return (pid2 = f()) + 26;
}

这个dlsym的大概意思是说从打开的共享库中找到getpid这个函数的地址,然后直接拿来调用。
之前看到过vdso,难道和这个有关系?
最后返回值加上26是什么意思,又不明白了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容