cn_proc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * cn_proc.c - process events connector
  3. *
  4. * Copyright (C) Matt Helsley, IBM Corp. 2005
  5. * Based on cn_fork.c by Guillaume Thouvenin <guillaume.thouvenin@bull.net>
  6. * Original copyright notice follows:
  7. * Copyright (C) 2005 BULL SA.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/ktime.h>
  27. #include <linux/init.h>
  28. #include <linux/connector.h>
  29. #include <asm/atomic.h>
  30. #include <asm/unaligned.h>
  31. #include <linux/cn_proc.h>
  32. #define CN_PROC_MSG_SIZE (sizeof(struct cn_msg) + sizeof(struct proc_event))
  33. static atomic_t proc_event_num_listeners = ATOMIC_INIT(0);
  34. static struct cb_id cn_proc_event_id = { CN_IDX_PROC, CN_VAL_PROC };
  35. /* proc_event_counts is used as the sequence number of the netlink message */
  36. static DEFINE_PER_CPU(__u32, proc_event_counts) = { 0 };
  37. static inline void get_seq(__u32 *ts, int *cpu)
  38. {
  39. *ts = get_cpu_var(proc_event_counts)++;
  40. *cpu = smp_processor_id();
  41. put_cpu_var(proc_event_counts);
  42. }
  43. void proc_fork_connector(struct task_struct *task)
  44. {
  45. struct cn_msg *msg;
  46. struct proc_event *ev;
  47. __u8 buffer[CN_PROC_MSG_SIZE];
  48. struct timespec ts;
  49. if (atomic_read(&proc_event_num_listeners) < 1)
  50. return;
  51. msg = (struct cn_msg*)buffer;
  52. ev = (struct proc_event*)msg->data;
  53. get_seq(&msg->seq, &ev->cpu);
  54. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  55. put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
  56. ev->what = PROC_EVENT_FORK;
  57. ev->event_data.fork.parent_pid = task->real_parent->pid;
  58. ev->event_data.fork.parent_tgid = task->real_parent->tgid;
  59. ev->event_data.fork.child_pid = task->pid;
  60. ev->event_data.fork.child_tgid = task->tgid;
  61. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  62. msg->ack = 0; /* not used */
  63. msg->len = sizeof(*ev);
  64. /* If cn_netlink_send() failed, the data is not sent */
  65. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  66. }
  67. void proc_exec_connector(struct task_struct *task)
  68. {
  69. struct cn_msg *msg;
  70. struct proc_event *ev;
  71. struct timespec ts;
  72. __u8 buffer[CN_PROC_MSG_SIZE];
  73. if (atomic_read(&proc_event_num_listeners) < 1)
  74. return;
  75. msg = (struct cn_msg*)buffer;
  76. ev = (struct proc_event*)msg->data;
  77. get_seq(&msg->seq, &ev->cpu);
  78. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  79. put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
  80. ev->what = PROC_EVENT_EXEC;
  81. ev->event_data.exec.process_pid = task->pid;
  82. ev->event_data.exec.process_tgid = task->tgid;
  83. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  84. msg->ack = 0; /* not used */
  85. msg->len = sizeof(*ev);
  86. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  87. }
  88. void proc_id_connector(struct task_struct *task, int which_id)
  89. {
  90. struct cn_msg *msg;
  91. struct proc_event *ev;
  92. __u8 buffer[CN_PROC_MSG_SIZE];
  93. struct timespec ts;
  94. const struct cred *cred;
  95. if (atomic_read(&proc_event_num_listeners) < 1)
  96. return;
  97. msg = (struct cn_msg*)buffer;
  98. ev = (struct proc_event*)msg->data;
  99. ev->what = which_id;
  100. ev->event_data.id.process_pid = task->pid;
  101. ev->event_data.id.process_tgid = task->tgid;
  102. rcu_read_lock();
  103. cred = __task_cred(task);
  104. if (which_id == PROC_EVENT_UID) {
  105. ev->event_data.id.r.ruid = cred->uid;
  106. ev->event_data.id.e.euid = cred->euid;
  107. } else if (which_id == PROC_EVENT_GID) {
  108. ev->event_data.id.r.rgid = cred->gid;
  109. ev->event_data.id.e.egid = cred->egid;
  110. } else {
  111. rcu_read_unlock();
  112. return;
  113. }
  114. rcu_read_unlock();
  115. get_seq(&msg->seq, &ev->cpu);
  116. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  117. put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
  118. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  119. msg->ack = 0; /* not used */
  120. msg->len = sizeof(*ev);
  121. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  122. }
  123. void proc_exit_connector(struct task_struct *task)
  124. {
  125. struct cn_msg *msg;
  126. struct proc_event *ev;
  127. __u8 buffer[CN_PROC_MSG_SIZE];
  128. struct timespec ts;
  129. if (atomic_read(&proc_event_num_listeners) < 1)
  130. return;
  131. msg = (struct cn_msg*)buffer;
  132. ev = (struct proc_event*)msg->data;
  133. get_seq(&msg->seq, &ev->cpu);
  134. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  135. put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
  136. ev->what = PROC_EVENT_EXIT;
  137. ev->event_data.exit.process_pid = task->pid;
  138. ev->event_data.exit.process_tgid = task->tgid;
  139. ev->event_data.exit.exit_code = task->exit_code;
  140. ev->event_data.exit.exit_signal = task->exit_signal;
  141. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  142. msg->ack = 0; /* not used */
  143. msg->len = sizeof(*ev);
  144. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  145. }
  146. /*
  147. * Send an acknowledgement message to userspace
  148. *
  149. * Use 0 for success, EFOO otherwise.
  150. * Note: this is the negative of conventional kernel error
  151. * values because it's not being returned via syscall return
  152. * mechanisms.
  153. */
  154. static void cn_proc_ack(int err, int rcvd_seq, int rcvd_ack)
  155. {
  156. struct cn_msg *msg;
  157. struct proc_event *ev;
  158. __u8 buffer[CN_PROC_MSG_SIZE];
  159. struct timespec ts;
  160. if (atomic_read(&proc_event_num_listeners) < 1)
  161. return;
  162. msg = (struct cn_msg*)buffer;
  163. ev = (struct proc_event*)msg->data;
  164. msg->seq = rcvd_seq;
  165. ktime_get_ts(&ts); /* get high res monotonic timestamp */
  166. put_unaligned(timespec_to_ns(&ts), (__u64 *)&ev->timestamp_ns);
  167. ev->cpu = -1;
  168. ev->what = PROC_EVENT_NONE;
  169. ev->event_data.ack.err = err;
  170. memcpy(&msg->id, &cn_proc_event_id, sizeof(msg->id));
  171. msg->ack = rcvd_ack + 1;
  172. msg->len = sizeof(*ev);
  173. cn_netlink_send(msg, CN_IDX_PROC, GFP_KERNEL);
  174. }
  175. /**
  176. * cn_proc_mcast_ctl
  177. * @data: message sent from userspace via the connector
  178. */
  179. static void cn_proc_mcast_ctl(void *data)
  180. {
  181. struct cn_msg *msg = data;
  182. enum proc_cn_mcast_op *mc_op = NULL;
  183. int err = 0;
  184. if (msg->len != sizeof(*mc_op))
  185. return;
  186. mc_op = (enum proc_cn_mcast_op*)msg->data;
  187. switch (*mc_op) {
  188. case PROC_CN_MCAST_LISTEN:
  189. atomic_inc(&proc_event_num_listeners);
  190. break;
  191. case PROC_CN_MCAST_IGNORE:
  192. atomic_dec(&proc_event_num_listeners);
  193. break;
  194. default:
  195. err = EINVAL;
  196. break;
  197. }
  198. cn_proc_ack(err, msg->seq, msg->ack);
  199. }
  200. /*
  201. * cn_proc_init - initialization entry point
  202. *
  203. * Adds the connector callback to the connector driver.
  204. */
  205. static int __init cn_proc_init(void)
  206. {
  207. int err;
  208. if ((err = cn_add_callback(&cn_proc_event_id, "cn_proc",
  209. &cn_proc_mcast_ctl))) {
  210. printk(KERN_WARNING "cn_proc failed to register\n");
  211. return err;
  212. }
  213. return 0;
  214. }
  215. module_init(cn_proc_init);