Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66428
  • 博文数量: 11
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 120
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-06 15:04
文章分类
文章存档

2018年(11)

我的朋友

分类: LINUX

2018-03-11 16:23:56


点击(此处)折叠或打开

  1. static void get_task_from_sock(struct sock *sk)
  2. {
  3.     struct socket *sock = NULL;
  4.     struct file *file = NULL;
  5.     struct task_struct *task;
  6.     int i, j = 0;
  7.     struct fdtable *fdt;
  8.     int found = 0;
  9.     if (sk == NULL)
  10.         return;
  11.         
  12.     sock = sk->sk_socket;
  13.     if (sock == NULL)
  14.         return;
  15.         
  16.     file = sock->file;

  17.     rcu_read_lock();
  18.     for_each_process(task) {
  19.         fdt = task->files->fdt;
  20.         if (fdt) {
  21.             j = fdt->max_fds;
  22.             for (i = 0; i < j; i++) {
  23.                 if (NULL != fdt->fd[i]) {
  24.                     if (fdt->fd[i] == file) {
  25.                         printk("task name %s bind %d\n", task->comm, inet_sk(sk)->inet_num);
  26.                         found = 1;
  27.                         break;
  28.                     }
  29.                 }
  30.             }
  31.         }
  32.         if (found == 1)
  33.             break;
  34.     }
  35.     rcu_read_unlock();
  36.     
  37.     return;
  38. }

  39. static void get_listen_task(void)
  40. {
  41.     int i = 0;
  42.     char port_str[8] = {0};
  43.     
  44.     struct sock *sk;
  45.     struct hlist_nulls_node *node;
  46.     struct inet_listen_hashbucket *ilb;
  47.     
  48.     rcu_read_lock();
  49.     for (i = 0; i < INET_LHTABLE_SIZE; i++) {
  50.         ilb = &tcp_hashinfo.listening_hash[i];
  51.         sk_nulls_for_each_rcu(sk, node, &ilb->head) {
  52.             if (sk) {
  53.                     if (sk->sk_family != AF_INET)
  54.                         continue;
  55.                         
  56.                     memset(port_str, 0, sizeof(port_str));
  57.                 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
  58.                     snprintf(port_str, sizeof(port_str), "%d", inet_sk(sk)->num);
  59.                     get_task_from_sock(sk);
  60.                 #else
  61.                     snprintf(port_str, sizeof(port_str), "%d", inet_sk(sk)->inet_num);
  62.                     get_task_from_sock(sk);
  63.                 #endif
  64.             }
  65.         }
  66.     }
  67.     rcu_read_unlock();
  68.     
  69.     return;
  70. }

阅读(1264) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:从bprm获取命令行参数

给主人留下些什么吧!~~