miscdev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2008 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/hash.h>
  23. #include <linux/random.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/wait.h>
  28. #include <linux/module.h>
  29. #include "ecryptfs_kernel.h"
  30. static atomic_t ecryptfs_num_miscdev_opens;
  31. /**
  32. * ecryptfs_miscdev_poll
  33. * @file: dev file (ignored)
  34. * @pt: dev poll table (ignored)
  35. *
  36. * Returns the poll mask
  37. */
  38. static unsigned int
  39. ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
  40. {
  41. struct ecryptfs_daemon *daemon;
  42. unsigned int mask = 0;
  43. uid_t euid = current_euid();
  44. int rc;
  45. mutex_lock(&ecryptfs_daemon_hash_mux);
  46. /* TODO: Just use file->private_data? */
  47. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  48. BUG_ON(rc || !daemon);
  49. mutex_lock(&daemon->mux);
  50. mutex_unlock(&ecryptfs_daemon_hash_mux);
  51. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  52. printk(KERN_WARNING "%s: Attempt to poll on zombified "
  53. "daemon\n", __func__);
  54. goto out_unlock_daemon;
  55. }
  56. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  57. goto out_unlock_daemon;
  58. if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
  59. goto out_unlock_daemon;
  60. daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
  61. mutex_unlock(&daemon->mux);
  62. poll_wait(file, &daemon->wait, pt);
  63. mutex_lock(&daemon->mux);
  64. if (!list_empty(&daemon->msg_ctx_out_queue))
  65. mask |= POLLIN | POLLRDNORM;
  66. out_unlock_daemon:
  67. daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
  68. mutex_unlock(&daemon->mux);
  69. return mask;
  70. }
  71. /**
  72. * ecryptfs_miscdev_open
  73. * @inode: inode of miscdev handle (ignored)
  74. * @file: file for miscdev handle (ignored)
  75. *
  76. * Returns zero on success; non-zero otherwise
  77. */
  78. static int
  79. ecryptfs_miscdev_open(struct inode *inode, struct file *file)
  80. {
  81. struct ecryptfs_daemon *daemon = NULL;
  82. uid_t euid = current_euid();
  83. int rc;
  84. mutex_lock(&ecryptfs_daemon_hash_mux);
  85. rc = try_module_get(THIS_MODULE);
  86. if (rc == 0) {
  87. rc = -EIO;
  88. printk(KERN_ERR "%s: Error attempting to increment module use "
  89. "count; rc = [%d]\n", __func__, rc);
  90. goto out_unlock_daemon_list;
  91. }
  92. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  93. if (rc || !daemon) {
  94. rc = ecryptfs_spawn_daemon(&daemon, euid, current_user_ns(),
  95. task_pid(current));
  96. if (rc) {
  97. printk(KERN_ERR "%s: Error attempting to spawn daemon; "
  98. "rc = [%d]\n", __func__, rc);
  99. goto out_module_put_unlock_daemon_list;
  100. }
  101. }
  102. mutex_lock(&daemon->mux);
  103. if (daemon->pid != task_pid(current)) {
  104. rc = -EINVAL;
  105. printk(KERN_ERR "%s: pid [0x%p] has registered with euid [%d], "
  106. "but pid [0x%p] has attempted to open the handle "
  107. "instead\n", __func__, daemon->pid, daemon->euid,
  108. task_pid(current));
  109. goto out_unlock_daemon;
  110. }
  111. if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
  112. rc = -EBUSY;
  113. printk(KERN_ERR "%s: Miscellaneous device handle may only be "
  114. "opened once per daemon; pid [0x%p] already has this "
  115. "handle open\n", __func__, daemon->pid);
  116. goto out_unlock_daemon;
  117. }
  118. daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
  119. atomic_inc(&ecryptfs_num_miscdev_opens);
  120. out_unlock_daemon:
  121. mutex_unlock(&daemon->mux);
  122. out_module_put_unlock_daemon_list:
  123. if (rc)
  124. module_put(THIS_MODULE);
  125. out_unlock_daemon_list:
  126. mutex_unlock(&ecryptfs_daemon_hash_mux);
  127. return rc;
  128. }
  129. /**
  130. * ecryptfs_miscdev_release
  131. * @inode: inode of fs/ecryptfs/euid handle (ignored)
  132. * @file: file for fs/ecryptfs/euid handle (ignored)
  133. *
  134. * This keeps the daemon registered until the daemon sends another
  135. * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
  136. *
  137. * Returns zero on success; non-zero otherwise
  138. */
  139. static int
  140. ecryptfs_miscdev_release(struct inode *inode, struct file *file)
  141. {
  142. struct ecryptfs_daemon *daemon = NULL;
  143. uid_t euid = current_euid();
  144. int rc;
  145. mutex_lock(&ecryptfs_daemon_hash_mux);
  146. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  147. BUG_ON(rc || !daemon);
  148. mutex_lock(&daemon->mux);
  149. BUG_ON(daemon->pid != task_pid(current));
  150. BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
  151. daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
  152. atomic_dec(&ecryptfs_num_miscdev_opens);
  153. mutex_unlock(&daemon->mux);
  154. rc = ecryptfs_exorcise_daemon(daemon);
  155. if (rc) {
  156. printk(KERN_CRIT "%s: Fatal error whilst attempting to "
  157. "shut down daemon; rc = [%d]. Please report this "
  158. "bug.\n", __func__, rc);
  159. BUG();
  160. }
  161. module_put(THIS_MODULE);
  162. mutex_unlock(&ecryptfs_daemon_hash_mux);
  163. return rc;
  164. }
  165. /**
  166. * ecryptfs_send_miscdev
  167. * @data: Data to send to daemon; may be NULL
  168. * @data_size: Amount of data to send to daemon
  169. * @msg_ctx: Message context, which is used to handle the reply. If
  170. * this is NULL, then we do not expect a reply.
  171. * @msg_type: Type of message
  172. * @msg_flags: Flags for message
  173. * @daemon: eCryptfs daemon object
  174. *
  175. * Add msg_ctx to queue and then, if it exists, notify the blocked
  176. * miscdevess about the data being available. Must be called with
  177. * ecryptfs_daemon_hash_mux held.
  178. *
  179. * Returns zero on success; non-zero otherwise
  180. */
  181. int ecryptfs_send_miscdev(char *data, size_t data_size,
  182. struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
  183. u16 msg_flags, struct ecryptfs_daemon *daemon)
  184. {
  185. struct ecryptfs_message *msg;
  186. msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
  187. if (!msg) {
  188. printk(KERN_ERR "%s: Out of memory whilst attempting "
  189. "to kmalloc(%zd, GFP_KERNEL)\n", __func__,
  190. (sizeof(*msg) + data_size));
  191. return -ENOMEM;
  192. }
  193. mutex_lock(&msg_ctx->mux);
  194. msg_ctx->msg = msg;
  195. msg_ctx->msg->index = msg_ctx->index;
  196. msg_ctx->msg->data_len = data_size;
  197. msg_ctx->type = msg_type;
  198. memcpy(msg_ctx->msg->data, data, data_size);
  199. msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
  200. list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
  201. mutex_unlock(&msg_ctx->mux);
  202. mutex_lock(&daemon->mux);
  203. daemon->num_queued_msg_ctx++;
  204. wake_up_interruptible(&daemon->wait);
  205. mutex_unlock(&daemon->mux);
  206. return 0;
  207. }
  208. /*
  209. * miscdevfs packet format:
  210. * Octet 0: Type
  211. * Octets 1-4: network byte order msg_ctx->counter
  212. * Octets 5-N0: Size of struct ecryptfs_message to follow
  213. * Octets N0-N1: struct ecryptfs_message (including data)
  214. *
  215. * Octets 5-N1 not written if the packet type does not include a message
  216. */
  217. #define PKT_TYPE_SIZE 1
  218. #define PKT_CTR_SIZE 4
  219. #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  220. #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  221. + ECRYPTFS_MIN_PKT_LEN_SIZE)
  222. /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
  223. #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  224. + ECRYPTFS_MAX_PKT_LEN_SIZE \
  225. + sizeof(struct ecryptfs_message) \
  226. + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
  227. #define PKT_TYPE_OFFSET 0
  228. #define PKT_CTR_OFFSET PKT_TYPE_SIZE
  229. #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  230. /**
  231. * ecryptfs_miscdev_read - format and send message from queue
  232. * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
  233. * @buf: User buffer into which to copy the next message on the daemon queue
  234. * @count: Amount of space available in @buf
  235. * @ppos: Offset in file (ignored)
  236. *
  237. * Pulls the most recent message from the daemon queue, formats it for
  238. * being sent via a miscdevfs handle, and copies it into @buf
  239. *
  240. * Returns the number of bytes copied into the user buffer
  241. */
  242. static ssize_t
  243. ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
  244. loff_t *ppos)
  245. {
  246. struct ecryptfs_daemon *daemon;
  247. struct ecryptfs_msg_ctx *msg_ctx;
  248. size_t packet_length_size;
  249. char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
  250. size_t i;
  251. size_t total_length;
  252. uid_t euid = current_euid();
  253. int rc;
  254. mutex_lock(&ecryptfs_daemon_hash_mux);
  255. /* TODO: Just use file->private_data? */
  256. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  257. BUG_ON(rc || !daemon);
  258. mutex_lock(&daemon->mux);
  259. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  260. rc = 0;
  261. mutex_unlock(&ecryptfs_daemon_hash_mux);
  262. printk(KERN_WARNING "%s: Attempt to read from zombified "
  263. "daemon\n", __func__);
  264. goto out_unlock_daemon;
  265. }
  266. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
  267. rc = 0;
  268. mutex_unlock(&ecryptfs_daemon_hash_mux);
  269. goto out_unlock_daemon;
  270. }
  271. /* This daemon will not go away so long as this flag is set */
  272. daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
  273. mutex_unlock(&ecryptfs_daemon_hash_mux);
  274. check_list:
  275. if (list_empty(&daemon->msg_ctx_out_queue)) {
  276. mutex_unlock(&daemon->mux);
  277. rc = wait_event_interruptible(
  278. daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
  279. mutex_lock(&daemon->mux);
  280. if (rc < 0) {
  281. rc = 0;
  282. goto out_unlock_daemon;
  283. }
  284. }
  285. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  286. rc = 0;
  287. goto out_unlock_daemon;
  288. }
  289. if (list_empty(&daemon->msg_ctx_out_queue)) {
  290. /* Something else jumped in since the
  291. * wait_event_interruptable() and removed the
  292. * message from the queue; try again */
  293. goto check_list;
  294. }
  295. BUG_ON(euid != daemon->euid);
  296. BUG_ON(current_user_ns() != daemon->user_ns);
  297. BUG_ON(task_pid(current) != daemon->pid);
  298. msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
  299. struct ecryptfs_msg_ctx, daemon_out_list);
  300. BUG_ON(!msg_ctx);
  301. mutex_lock(&msg_ctx->mux);
  302. if (msg_ctx->msg) {
  303. rc = ecryptfs_write_packet_length(packet_length,
  304. msg_ctx->msg_size,
  305. &packet_length_size);
  306. if (rc) {
  307. rc = 0;
  308. printk(KERN_WARNING "%s: Error writing packet length; "
  309. "rc = [%d]\n", __func__, rc);
  310. goto out_unlock_msg_ctx;
  311. }
  312. } else {
  313. packet_length_size = 0;
  314. msg_ctx->msg_size = 0;
  315. }
  316. total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
  317. + msg_ctx->msg_size);
  318. if (count < total_length) {
  319. rc = 0;
  320. printk(KERN_WARNING "%s: Only given user buffer of "
  321. "size [%zd], but we need [%zd] to read the "
  322. "pending message\n", __func__, count, total_length);
  323. goto out_unlock_msg_ctx;
  324. }
  325. rc = -EFAULT;
  326. if (put_user(msg_ctx->type, buf))
  327. goto out_unlock_msg_ctx;
  328. if (put_user(cpu_to_be32(msg_ctx->counter),
  329. (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
  330. goto out_unlock_msg_ctx;
  331. i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
  332. if (msg_ctx->msg) {
  333. if (copy_to_user(&buf[i], packet_length, packet_length_size))
  334. goto out_unlock_msg_ctx;
  335. i += packet_length_size;
  336. if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
  337. goto out_unlock_msg_ctx;
  338. i += msg_ctx->msg_size;
  339. }
  340. rc = i;
  341. list_del(&msg_ctx->daemon_out_list);
  342. kfree(msg_ctx->msg);
  343. msg_ctx->msg = NULL;
  344. /* We do not expect a reply from the userspace daemon for any
  345. * message type other than ECRYPTFS_MSG_REQUEST */
  346. if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
  347. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  348. out_unlock_msg_ctx:
  349. mutex_unlock(&msg_ctx->mux);
  350. out_unlock_daemon:
  351. daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
  352. mutex_unlock(&daemon->mux);
  353. return rc;
  354. }
  355. /**
  356. * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
  357. * @data: Bytes comprising struct ecryptfs_message
  358. * @data_size: sizeof(struct ecryptfs_message) + data len
  359. * @euid: Effective user id of miscdevess sending the miscdev response
  360. * @user_ns: The namespace in which @euid applies
  361. * @pid: Miscdevess id of miscdevess sending the miscdev response
  362. * @seq: Sequence number for miscdev response packet
  363. *
  364. * Returns zero on success; non-zero otherwise
  365. */
  366. static int ecryptfs_miscdev_response(char *data, size_t data_size,
  367. uid_t euid, struct user_namespace *user_ns,
  368. struct pid *pid, u32 seq)
  369. {
  370. struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
  371. int rc;
  372. if ((sizeof(*msg) + msg->data_len) != data_size) {
  373. printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
  374. "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
  375. (sizeof(*msg) + msg->data_len), data_size);
  376. rc = -EINVAL;
  377. goto out;
  378. }
  379. rc = ecryptfs_process_response(msg, euid, user_ns, pid, seq);
  380. if (rc)
  381. printk(KERN_ERR
  382. "Error processing response message; rc = [%d]\n", rc);
  383. out:
  384. return rc;
  385. }
  386. /**
  387. * ecryptfs_miscdev_write - handle write to daemon miscdev handle
  388. * @file: File for misc dev handle (ignored)
  389. * @buf: Buffer containing user data
  390. * @count: Amount of data in @buf
  391. * @ppos: Pointer to offset in file (ignored)
  392. *
  393. * Returns the number of bytes read from @buf
  394. */
  395. static ssize_t
  396. ecryptfs_miscdev_write(struct file *file, const char __user *buf,
  397. size_t count, loff_t *ppos)
  398. {
  399. __be32 counter_nbo;
  400. u32 seq;
  401. size_t packet_size, packet_size_length;
  402. char *data;
  403. uid_t euid = current_euid();
  404. unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
  405. ssize_t rc;
  406. if (count == 0) {
  407. return 0;
  408. } else if (count == MIN_NON_MSG_PKT_SIZE) {
  409. /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
  410. goto memdup;
  411. } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
  412. printk(KERN_WARNING "%s: Acceptable packet size range is "
  413. "[%d-%zu], but amount of data written is [%zu].",
  414. __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
  415. return -EINVAL;
  416. }
  417. if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
  418. sizeof(packet_size_peek))) {
  419. printk(KERN_WARNING "%s: Error while inspecting packet size\n",
  420. __func__);
  421. return -EFAULT;
  422. }
  423. rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
  424. &packet_size_length);
  425. if (rc) {
  426. printk(KERN_WARNING "%s: Error parsing packet length; "
  427. "rc = [%zd]\n", __func__, rc);
  428. return rc;
  429. }
  430. if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
  431. != count) {
  432. printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
  433. packet_size);
  434. return -EINVAL;
  435. }
  436. memdup:
  437. data = memdup_user(buf, count);
  438. if (IS_ERR(data)) {
  439. printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
  440. __func__, PTR_ERR(data));
  441. return PTR_ERR(data);
  442. }
  443. switch (data[PKT_TYPE_OFFSET]) {
  444. case ECRYPTFS_MSG_RESPONSE:
  445. if (count < (MIN_MSG_PKT_SIZE
  446. + sizeof(struct ecryptfs_message))) {
  447. printk(KERN_WARNING "%s: Minimum acceptable packet "
  448. "size is [%zd], but amount of data written is "
  449. "only [%zd]. Discarding response packet.\n",
  450. __func__,
  451. (MIN_MSG_PKT_SIZE
  452. + sizeof(struct ecryptfs_message)), count);
  453. rc = -EINVAL;
  454. goto out_free;
  455. }
  456. memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
  457. seq = be32_to_cpu(counter_nbo);
  458. rc = ecryptfs_miscdev_response(
  459. &data[PKT_LEN_OFFSET + packet_size_length],
  460. packet_size, euid, current_user_ns(),
  461. task_pid(current), seq);
  462. if (rc) {
  463. printk(KERN_WARNING "%s: Failed to deliver miscdev "
  464. "response to requesting operation; rc = [%zd]\n",
  465. __func__, rc);
  466. goto out_free;
  467. }
  468. break;
  469. case ECRYPTFS_MSG_HELO:
  470. case ECRYPTFS_MSG_QUIT:
  471. break;
  472. default:
  473. ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
  474. "message of unrecognized type [%d]\n",
  475. data[0]);
  476. rc = -EINVAL;
  477. goto out_free;
  478. }
  479. rc = count;
  480. out_free:
  481. kfree(data);
  482. return rc;
  483. }
  484. static const struct file_operations ecryptfs_miscdev_fops = {
  485. .open = ecryptfs_miscdev_open,
  486. .poll = ecryptfs_miscdev_poll,
  487. .read = ecryptfs_miscdev_read,
  488. .write = ecryptfs_miscdev_write,
  489. .release = ecryptfs_miscdev_release,
  490. .llseek = noop_llseek,
  491. };
  492. static struct miscdevice ecryptfs_miscdev = {
  493. .minor = MISC_DYNAMIC_MINOR,
  494. .name = "ecryptfs",
  495. .fops = &ecryptfs_miscdev_fops
  496. };
  497. /**
  498. * ecryptfs_init_ecryptfs_miscdev
  499. *
  500. * Messages sent to the userspace daemon from the kernel are placed on
  501. * a queue associated with the daemon. The next read against the
  502. * miscdev handle by that daemon will return the oldest message placed
  503. * on the message queue for the daemon.
  504. *
  505. * Returns zero on success; non-zero otherwise
  506. */
  507. int __init ecryptfs_init_ecryptfs_miscdev(void)
  508. {
  509. int rc;
  510. atomic_set(&ecryptfs_num_miscdev_opens, 0);
  511. rc = misc_register(&ecryptfs_miscdev);
  512. if (rc)
  513. printk(KERN_ERR "%s: Failed to register miscellaneous device "
  514. "for communications with userspace daemons; rc = [%d]\n",
  515. __func__, rc);
  516. return rc;
  517. }
  518. /**
  519. * ecryptfs_destroy_ecryptfs_miscdev
  520. *
  521. * All of the daemons must be exorcised prior to calling this
  522. * function.
  523. */
  524. void ecryptfs_destroy_ecryptfs_miscdev(void)
  525. {
  526. BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
  527. misc_deregister(&ecryptfs_miscdev);
  528. }