kobject_uevent.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * kernel userspace event delivery
  4. *
  5. * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
  6. * Copyright (C) 2004 Novell, Inc. All rights reserved.
  7. * Copyright (C) 2004 IBM, Inc. All rights reserved.
  8. *
  9. * Authors:
  10. * Robert Love <rml@novell.com>
  11. * Kay Sievers <kay.sievers@vrfy.org>
  12. * Arjan van de Ven <arjanv@redhat.com>
  13. * Greg Kroah-Hartman <greg@kroah.com>
  14. */
  15. #include <linux/spinlock.h>
  16. #include <linux/string.h>
  17. #include <linux/kobject.h>
  18. #include <linux/export.h>
  19. #include <linux/kmod.h>
  20. #include <linux/slab.h>
  21. #include <linux/socket.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netlink.h>
  24. #include <linux/uuid.h>
  25. #include <linux/ctype.h>
  26. #include <net/sock.h>
  27. #include <net/netlink.h>
  28. #include <net/net_namespace.h>
  29. u64 uevent_seqnum;
  30. #ifdef CONFIG_UEVENT_HELPER
  31. char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
  32. #endif
  33. struct uevent_sock {
  34. struct list_head list;
  35. struct sock *sk;
  36. };
  37. #ifdef CONFIG_NET
  38. static LIST_HEAD(uevent_sock_list);
  39. #endif
  40. /* This lock protects uevent_seqnum and uevent_sock_list */
  41. static DEFINE_MUTEX(uevent_sock_mutex);
  42. /* the strings here must match the enum in include/linux/kobject.h */
  43. static const char *kobject_actions[] = {
  44. [KOBJ_ADD] = "add",
  45. [KOBJ_REMOVE] = "remove",
  46. [KOBJ_CHANGE] = "change",
  47. [KOBJ_MOVE] = "move",
  48. [KOBJ_ONLINE] = "online",
  49. [KOBJ_OFFLINE] = "offline",
  50. [KOBJ_BIND] = "bind",
  51. [KOBJ_UNBIND] = "unbind",
  52. };
  53. static int kobject_action_type(const char *buf, size_t count,
  54. enum kobject_action *type,
  55. const char **args)
  56. {
  57. enum kobject_action action;
  58. size_t count_first;
  59. const char *args_start;
  60. int ret = -EINVAL;
  61. if (count && (buf[count-1] == '\n' || buf[count-1] == '\0'))
  62. count--;
  63. if (!count)
  64. goto out;
  65. args_start = strnchr(buf, count, ' ');
  66. if (args_start) {
  67. count_first = args_start - buf;
  68. args_start = args_start + 1;
  69. } else
  70. count_first = count;
  71. for (action = 0; action < ARRAY_SIZE(kobject_actions); action++) {
  72. if (strncmp(kobject_actions[action], buf, count_first) != 0)
  73. continue;
  74. if (kobject_actions[action][count_first] != '\0')
  75. continue;
  76. if (args)
  77. *args = args_start;
  78. *type = action;
  79. ret = 0;
  80. break;
  81. }
  82. out:
  83. return ret;
  84. }
  85. static const char *action_arg_word_end(const char *buf, const char *buf_end,
  86. char delim)
  87. {
  88. const char *next = buf;
  89. while (next <= buf_end && *next != delim)
  90. if (!isalnum(*next++))
  91. return NULL;
  92. if (next == buf)
  93. return NULL;
  94. return next;
  95. }
  96. static int kobject_action_args(const char *buf, size_t count,
  97. struct kobj_uevent_env **ret_env)
  98. {
  99. struct kobj_uevent_env *env = NULL;
  100. const char *next, *buf_end, *key;
  101. int key_len;
  102. int r = -EINVAL;
  103. if (count && (buf[count - 1] == '\n' || buf[count - 1] == '\0'))
  104. count--;
  105. if (!count)
  106. return -EINVAL;
  107. env = kzalloc(sizeof(*env), GFP_KERNEL);
  108. if (!env)
  109. return -ENOMEM;
  110. /* first arg is UUID */
  111. if (count < UUID_STRING_LEN || !uuid_is_valid(buf) ||
  112. add_uevent_var(env, "SYNTH_UUID=%.*s", UUID_STRING_LEN, buf))
  113. goto out;
  114. /*
  115. * the rest are custom environment variables in KEY=VALUE
  116. * format with ' ' delimiter between each KEY=VALUE pair
  117. */
  118. next = buf + UUID_STRING_LEN;
  119. buf_end = buf + count - 1;
  120. while (next <= buf_end) {
  121. if (*next != ' ')
  122. goto out;
  123. /* skip the ' ', key must follow */
  124. key = ++next;
  125. if (key > buf_end)
  126. goto out;
  127. buf = next;
  128. next = action_arg_word_end(buf, buf_end, '=');
  129. if (!next || next > buf_end || *next != '=')
  130. goto out;
  131. key_len = next - buf;
  132. /* skip the '=', value must follow */
  133. if (++next > buf_end)
  134. goto out;
  135. buf = next;
  136. next = action_arg_word_end(buf, buf_end, ' ');
  137. if (!next)
  138. goto out;
  139. if (add_uevent_var(env, "SYNTH_ARG_%.*s=%.*s",
  140. key_len, key, (int) (next - buf), buf))
  141. goto out;
  142. }
  143. r = 0;
  144. out:
  145. if (r)
  146. kfree(env);
  147. else
  148. *ret_env = env;
  149. return r;
  150. }
  151. /**
  152. * kobject_synth_uevent - send synthetic uevent with arguments
  153. *
  154. * @kobj: struct kobject for which synthetic uevent is to be generated
  155. * @buf: buffer containing action type and action args, newline is ignored
  156. * @count: length of buffer
  157. *
  158. * Returns 0 if kobject_synthetic_uevent() is completed with success or the
  159. * corresponding error when it fails.
  160. */
  161. int kobject_synth_uevent(struct kobject *kobj, const char *buf, size_t count)
  162. {
  163. char *no_uuid_envp[] = { "SYNTH_UUID=0", NULL };
  164. enum kobject_action action;
  165. const char *action_args;
  166. struct kobj_uevent_env *env;
  167. const char *msg = NULL, *devpath;
  168. int r;
  169. r = kobject_action_type(buf, count, &action, &action_args);
  170. if (r) {
  171. msg = "unknown uevent action string\n";
  172. goto out;
  173. }
  174. if (!action_args) {
  175. r = kobject_uevent_env(kobj, action, no_uuid_envp);
  176. goto out;
  177. }
  178. r = kobject_action_args(action_args,
  179. count - (action_args - buf), &env);
  180. if (r == -EINVAL) {
  181. msg = "incorrect uevent action arguments\n";
  182. goto out;
  183. }
  184. if (r)
  185. goto out;
  186. r = kobject_uevent_env(kobj, action, env->envp);
  187. kfree(env);
  188. out:
  189. if (r) {
  190. devpath = kobject_get_path(kobj, GFP_KERNEL);
  191. printk(KERN_WARNING "synth uevent: %s: %s",
  192. devpath ?: "unknown device",
  193. msg ?: "failed to send uevent");
  194. kfree(devpath);
  195. }
  196. return r;
  197. }
  198. #ifdef CONFIG_NET
  199. static int kobj_bcast_filter(struct sock *dsk, struct sk_buff *skb, void *data)
  200. {
  201. struct kobject *kobj = data, *ksobj;
  202. const struct kobj_ns_type_operations *ops;
  203. ops = kobj_ns_ops(kobj);
  204. if (!ops && kobj->kset) {
  205. ksobj = &kobj->kset->kobj;
  206. if (ksobj->parent != NULL)
  207. ops = kobj_ns_ops(ksobj->parent);
  208. }
  209. if (ops && ops->netlink_ns && kobj->ktype->namespace) {
  210. const void *sock_ns, *ns;
  211. ns = kobj->ktype->namespace(kobj);
  212. sock_ns = ops->netlink_ns(dsk);
  213. return sock_ns != ns;
  214. }
  215. return 0;
  216. }
  217. #endif
  218. #ifdef CONFIG_UEVENT_HELPER
  219. static int kobj_usermode_filter(struct kobject *kobj)
  220. {
  221. const struct kobj_ns_type_operations *ops;
  222. ops = kobj_ns_ops(kobj);
  223. if (ops) {
  224. const void *init_ns, *ns;
  225. ns = kobj->ktype->namespace(kobj);
  226. init_ns = ops->initial_ns();
  227. return ns != init_ns;
  228. }
  229. return 0;
  230. }
  231. static int init_uevent_argv(struct kobj_uevent_env *env, const char *subsystem)
  232. {
  233. int len;
  234. len = strlcpy(&env->buf[env->buflen], subsystem,
  235. sizeof(env->buf) - env->buflen);
  236. if (len >= (sizeof(env->buf) - env->buflen)) {
  237. WARN(1, KERN_ERR "init_uevent_argv: buffer size too small\n");
  238. return -ENOMEM;
  239. }
  240. env->argv[0] = uevent_helper;
  241. env->argv[1] = &env->buf[env->buflen];
  242. env->argv[2] = NULL;
  243. env->buflen += len + 1;
  244. return 0;
  245. }
  246. static void cleanup_uevent_env(struct subprocess_info *info)
  247. {
  248. kfree(info->data);
  249. }
  250. #endif
  251. static int kobject_uevent_net_broadcast(struct kobject *kobj,
  252. struct kobj_uevent_env *env,
  253. const char *action_string,
  254. const char *devpath)
  255. {
  256. int retval = 0;
  257. #if defined(CONFIG_NET)
  258. struct sk_buff *skb = NULL;
  259. struct uevent_sock *ue_sk;
  260. /* send netlink message */
  261. list_for_each_entry(ue_sk, &uevent_sock_list, list) {
  262. struct sock *uevent_sock = ue_sk->sk;
  263. if (!netlink_has_listeners(uevent_sock, 1))
  264. continue;
  265. if (!skb) {
  266. /* allocate message with the maximum possible size */
  267. size_t len = strlen(action_string) + strlen(devpath) + 2;
  268. char *scratch;
  269. retval = -ENOMEM;
  270. skb = alloc_skb(len + env->buflen, GFP_KERNEL);
  271. if (!skb)
  272. continue;
  273. /* add header */
  274. scratch = skb_put(skb, len);
  275. sprintf(scratch, "%s@%s", action_string, devpath);
  276. skb_put_data(skb, env->buf, env->buflen);
  277. NETLINK_CB(skb).dst_group = 1;
  278. }
  279. retval = netlink_broadcast_filtered(uevent_sock, skb_get(skb),
  280. 0, 1, GFP_KERNEL,
  281. kobj_bcast_filter,
  282. kobj);
  283. /* ENOBUFS should be handled in userspace */
  284. if (retval == -ENOBUFS || retval == -ESRCH)
  285. retval = 0;
  286. }
  287. consume_skb(skb);
  288. #endif
  289. return retval;
  290. }
  291. static void zap_modalias_env(struct kobj_uevent_env *env)
  292. {
  293. static const char modalias_prefix[] = "MODALIAS=";
  294. size_t len;
  295. int i, j;
  296. for (i = 0; i < env->envp_idx;) {
  297. if (strncmp(env->envp[i], modalias_prefix,
  298. sizeof(modalias_prefix) - 1)) {
  299. i++;
  300. continue;
  301. }
  302. len = strlen(env->envp[i]) + 1;
  303. if (i != env->envp_idx - 1) {
  304. memmove(env->envp[i], env->envp[i + 1],
  305. env->buflen - len);
  306. for (j = i; j < env->envp_idx - 1; j++)
  307. env->envp[j] = env->envp[j + 1] - len;
  308. }
  309. env->envp_idx--;
  310. env->buflen -= len;
  311. }
  312. }
  313. /**
  314. * kobject_uevent_env - send an uevent with environmental data
  315. *
  316. * @kobj: struct kobject that the action is happening to
  317. * @action: action that is happening
  318. * @envp_ext: pointer to environmental data
  319. *
  320. * Returns 0 if kobject_uevent_env() is completed with success or the
  321. * corresponding error when it fails.
  322. */
  323. int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
  324. char *envp_ext[])
  325. {
  326. struct kobj_uevent_env *env;
  327. const char *action_string = kobject_actions[action];
  328. const char *devpath = NULL;
  329. const char *subsystem;
  330. struct kobject *top_kobj;
  331. struct kset *kset;
  332. const struct kset_uevent_ops *uevent_ops;
  333. int i = 0;
  334. int retval = 0;
  335. pr_debug("kobject: '%s' (%p): %s\n",
  336. kobject_name(kobj), kobj, __func__);
  337. /* search the kset we belong to */
  338. top_kobj = kobj;
  339. while (!top_kobj->kset && top_kobj->parent)
  340. top_kobj = top_kobj->parent;
  341. if (!top_kobj->kset) {
  342. pr_debug("kobject: '%s' (%p): %s: attempted to send uevent "
  343. "without kset!\n", kobject_name(kobj), kobj,
  344. __func__);
  345. return -EINVAL;
  346. }
  347. kset = top_kobj->kset;
  348. uevent_ops = kset->uevent_ops;
  349. /* skip the event, if uevent_suppress is set*/
  350. if (kobj->uevent_suppress) {
  351. pr_debug("kobject: '%s' (%p): %s: uevent_suppress "
  352. "caused the event to drop!\n",
  353. kobject_name(kobj), kobj, __func__);
  354. return 0;
  355. }
  356. /* skip the event, if the filter returns zero. */
  357. if (uevent_ops && uevent_ops->filter)
  358. if (!uevent_ops->filter(kset, kobj)) {
  359. pr_debug("kobject: '%s' (%p): %s: filter function "
  360. "caused the event to drop!\n",
  361. kobject_name(kobj), kobj, __func__);
  362. return 0;
  363. }
  364. /* originating subsystem */
  365. if (uevent_ops && uevent_ops->name)
  366. subsystem = uevent_ops->name(kset, kobj);
  367. else
  368. subsystem = kobject_name(&kset->kobj);
  369. if (!subsystem) {
  370. pr_debug("kobject: '%s' (%p): %s: unset subsystem caused the "
  371. "event to drop!\n", kobject_name(kobj), kobj,
  372. __func__);
  373. return 0;
  374. }
  375. /* environment buffer */
  376. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  377. if (!env)
  378. return -ENOMEM;
  379. /* complete object path */
  380. devpath = kobject_get_path(kobj, GFP_KERNEL);
  381. if (!devpath) {
  382. retval = -ENOENT;
  383. goto exit;
  384. }
  385. /* default keys */
  386. retval = add_uevent_var(env, "ACTION=%s", action_string);
  387. if (retval)
  388. goto exit;
  389. retval = add_uevent_var(env, "DEVPATH=%s", devpath);
  390. if (retval)
  391. goto exit;
  392. retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
  393. if (retval)
  394. goto exit;
  395. /* keys passed in from the caller */
  396. if (envp_ext) {
  397. for (i = 0; envp_ext[i]; i++) {
  398. retval = add_uevent_var(env, "%s", envp_ext[i]);
  399. if (retval)
  400. goto exit;
  401. }
  402. }
  403. /* let the kset specific function add its stuff */
  404. if (uevent_ops && uevent_ops->uevent) {
  405. retval = uevent_ops->uevent(kset, kobj, env);
  406. if (retval) {
  407. pr_debug("kobject: '%s' (%p): %s: uevent() returned "
  408. "%d\n", kobject_name(kobj), kobj,
  409. __func__, retval);
  410. goto exit;
  411. }
  412. }
  413. switch (action) {
  414. case KOBJ_ADD:
  415. /*
  416. * Mark "add" event so we can make sure we deliver "remove"
  417. * event to userspace during automatic cleanup. If
  418. * the object did send an "add" event, "remove" will
  419. * automatically generated by the core, if not already done
  420. * by the caller.
  421. */
  422. kobj->state_add_uevent_sent = 1;
  423. break;
  424. case KOBJ_REMOVE:
  425. kobj->state_remove_uevent_sent = 1;
  426. break;
  427. case KOBJ_UNBIND:
  428. zap_modalias_env(env);
  429. break;
  430. default:
  431. break;
  432. }
  433. mutex_lock(&uevent_sock_mutex);
  434. /* we will send an event, so request a new sequence number */
  435. retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)++uevent_seqnum);
  436. if (retval) {
  437. mutex_unlock(&uevent_sock_mutex);
  438. goto exit;
  439. }
  440. retval = kobject_uevent_net_broadcast(kobj, env, action_string,
  441. devpath);
  442. mutex_unlock(&uevent_sock_mutex);
  443. #ifdef CONFIG_UEVENT_HELPER
  444. /* call uevent_helper, usually only enabled during early boot */
  445. if (uevent_helper[0] && !kobj_usermode_filter(kobj)) {
  446. struct subprocess_info *info;
  447. retval = add_uevent_var(env, "HOME=/");
  448. if (retval)
  449. goto exit;
  450. retval = add_uevent_var(env,
  451. "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
  452. if (retval)
  453. goto exit;
  454. retval = init_uevent_argv(env, subsystem);
  455. if (retval)
  456. goto exit;
  457. retval = -ENOMEM;
  458. info = call_usermodehelper_setup(env->argv[0], env->argv,
  459. env->envp, GFP_KERNEL,
  460. NULL, cleanup_uevent_env, env);
  461. if (info) {
  462. retval = call_usermodehelper_exec(info, UMH_NO_WAIT);
  463. env = NULL; /* freed by cleanup_uevent_env */
  464. }
  465. }
  466. #endif
  467. exit:
  468. kfree(devpath);
  469. kfree(env);
  470. return retval;
  471. }
  472. EXPORT_SYMBOL_GPL(kobject_uevent_env);
  473. /**
  474. * kobject_uevent - notify userspace by sending an uevent
  475. *
  476. * @kobj: struct kobject that the action is happening to
  477. * @action: action that is happening
  478. *
  479. * Returns 0 if kobject_uevent() is completed with success or the
  480. * corresponding error when it fails.
  481. */
  482. int kobject_uevent(struct kobject *kobj, enum kobject_action action)
  483. {
  484. return kobject_uevent_env(kobj, action, NULL);
  485. }
  486. EXPORT_SYMBOL_GPL(kobject_uevent);
  487. /**
  488. * add_uevent_var - add key value string to the environment buffer
  489. * @env: environment buffer structure
  490. * @format: printf format for the key=value pair
  491. *
  492. * Returns 0 if environment variable was added successfully or -ENOMEM
  493. * if no space was available.
  494. */
  495. int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
  496. {
  497. va_list args;
  498. int len;
  499. if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
  500. WARN(1, KERN_ERR "add_uevent_var: too many keys\n");
  501. return -ENOMEM;
  502. }
  503. va_start(args, format);
  504. len = vsnprintf(&env->buf[env->buflen],
  505. sizeof(env->buf) - env->buflen,
  506. format, args);
  507. va_end(args);
  508. if (len >= (sizeof(env->buf) - env->buflen)) {
  509. WARN(1, KERN_ERR "add_uevent_var: buffer size too small\n");
  510. return -ENOMEM;
  511. }
  512. env->envp[env->envp_idx++] = &env->buf[env->buflen];
  513. env->buflen += len + 1;
  514. return 0;
  515. }
  516. EXPORT_SYMBOL_GPL(add_uevent_var);
  517. #if defined(CONFIG_NET)
  518. static int uevent_net_broadcast(struct sock *usk, struct sk_buff *skb,
  519. struct netlink_ext_ack *extack)
  520. {
  521. /* u64 to chars: 2^64 - 1 = 21 chars */
  522. char buf[sizeof("SEQNUM=") + 21];
  523. struct sk_buff *skbc;
  524. int ret;
  525. /* bump and prepare sequence number */
  526. ret = snprintf(buf, sizeof(buf), "SEQNUM=%llu", ++uevent_seqnum);
  527. if (ret < 0 || (size_t)ret >= sizeof(buf))
  528. return -ENOMEM;
  529. ret++;
  530. /* verify message does not overflow */
  531. if ((skb->len + ret) > UEVENT_BUFFER_SIZE) {
  532. NL_SET_ERR_MSG(extack, "uevent message too big");
  533. return -EINVAL;
  534. }
  535. /* copy skb and extend to accommodate sequence number */
  536. skbc = skb_copy_expand(skb, 0, ret, GFP_KERNEL);
  537. if (!skbc)
  538. return -ENOMEM;
  539. /* append sequence number */
  540. skb_put_data(skbc, buf, ret);
  541. /* remove msg header */
  542. skb_pull(skbc, NLMSG_HDRLEN);
  543. /* set portid 0 to inform userspace message comes from kernel */
  544. NETLINK_CB(skbc).portid = 0;
  545. NETLINK_CB(skbc).dst_group = 1;
  546. ret = netlink_broadcast(usk, skbc, 0, 1, GFP_KERNEL);
  547. /* ENOBUFS should be handled in userspace */
  548. if (ret == -ENOBUFS || ret == -ESRCH)
  549. ret = 0;
  550. return ret;
  551. }
  552. static int uevent_net_rcv_skb(struct sk_buff *skb, struct nlmsghdr *nlh,
  553. struct netlink_ext_ack *extack)
  554. {
  555. struct net *net;
  556. int ret;
  557. if (!nlmsg_data(nlh))
  558. return -EINVAL;
  559. /*
  560. * Verify that we are allowed to send messages to the target
  561. * network namespace. The caller must have CAP_SYS_ADMIN in the
  562. * owning user namespace of the target network namespace.
  563. */
  564. net = sock_net(NETLINK_CB(skb).sk);
  565. if (!netlink_ns_capable(skb, net->user_ns, CAP_SYS_ADMIN)) {
  566. NL_SET_ERR_MSG(extack, "missing CAP_SYS_ADMIN capability");
  567. return -EPERM;
  568. }
  569. mutex_lock(&uevent_sock_mutex);
  570. ret = uevent_net_broadcast(net->uevent_sock->sk, skb, extack);
  571. mutex_unlock(&uevent_sock_mutex);
  572. return ret;
  573. }
  574. static void uevent_net_rcv(struct sk_buff *skb)
  575. {
  576. netlink_rcv_skb(skb, &uevent_net_rcv_skb);
  577. }
  578. static int uevent_net_init(struct net *net)
  579. {
  580. struct uevent_sock *ue_sk;
  581. struct netlink_kernel_cfg cfg = {
  582. .groups = 1,
  583. .input = uevent_net_rcv,
  584. .flags = NL_CFG_F_NONROOT_RECV
  585. };
  586. ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
  587. if (!ue_sk)
  588. return -ENOMEM;
  589. ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, &cfg);
  590. if (!ue_sk->sk) {
  591. printk(KERN_ERR
  592. "kobject_uevent: unable to create netlink socket!\n");
  593. kfree(ue_sk);
  594. return -ENODEV;
  595. }
  596. net->uevent_sock = ue_sk;
  597. mutex_lock(&uevent_sock_mutex);
  598. list_add_tail(&ue_sk->list, &uevent_sock_list);
  599. mutex_unlock(&uevent_sock_mutex);
  600. return 0;
  601. }
  602. static void uevent_net_exit(struct net *net)
  603. {
  604. struct uevent_sock *ue_sk = net->uevent_sock;
  605. mutex_lock(&uevent_sock_mutex);
  606. list_del(&ue_sk->list);
  607. mutex_unlock(&uevent_sock_mutex);
  608. netlink_kernel_release(ue_sk->sk);
  609. kfree(ue_sk);
  610. }
  611. static struct pernet_operations uevent_net_ops = {
  612. .init = uevent_net_init,
  613. .exit = uevent_net_exit,
  614. };
  615. static int __init kobject_uevent_init(void)
  616. {
  617. return register_pernet_subsys(&uevent_net_ops);
  618. }
  619. postcore_initcall(kobject_uevent_init);
  620. #endif