如何获取linux内核中当前进程的一些信息

时间:2015-04-10 15:28:24

标签: c process linux-kernel

我是内核编码的新手。所以请原谅我问一个非常倾销的问题

我试图掌握内核linux中当前正在运行的进程的所有信息。 这是我尝试过的:

#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/sched.h>
#include "process_ancestors.h"

asmlinkage long sys_prs_anc(int arg){
    //int counter = 0;
    long pid;
    char *process_name;
    long process_state;
    long process_owner_uid;
    long voluntary_context_switches;
    long involuntary_context_switches;
    long num_children;
    long num_siblings;
    struct list_head *pos;

    num_children = 0;
    num_siblings = 0;
    struct list_head children_list;
    struct list_head siblings_list;
    pid = current->pid;
    process_name = current->comm;
    process_state=current->state;
    process_owner_uid = (current->cred)->uid;
    children_list = current->children;
    siblings_list = current->sibling;
    voluntary_context_switches = current->nivcsw;
    involuntary_context_switches = current->nvcsw;

    list_for_each(pos, &children_list){
        num_children ++;
    }
    list_for_each(pos, &siblings_list){
        num_siblings ++;
    }

    return 0;
}

我在构建它时遇到以下错误?

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh
  CHK     include/generated/compile.h
  CC      cs300/process_ancestors.o
cs300/process_ancestors.c: In function ‘sys_process_ancestors’:
cs300/process_ancestors.c:21:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
  struct list_head children_list;
  ^
cs300/process_ancestors.c:26:6: error: incompatible types when assigning to type ‘long int’ from type ‘kuid_t’
  uid = (current->cred)->uid;
      ^
make[1]: *** [cs300/process_ancestors.o] Error 1
make: *** [cs300] Error 2

任何人都能给我一些提示我做错了吗?

1 个答案:

答案 0 :(得分:2)

uid的类型为kuid_t

typedef struct {
    uid_t val;
} kuid_t;

你应该使用process_owner_uid = (current->cred)->uid.val;