82876e75152b9d8c32240f6180043c89dbdaf287
[lttng-modules.git] / wrapper / random.c
1 /*
2 * wrapper/random.c
3 *
4 * wrapper around bootid read. Using KALLSYMS to get its address when
5 * available, else we need to have a kernel that exports this function to GPL
6 * modules.
7 *
8 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; only
13 * version 2.1 of the License.
14 *
15 * This library 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 GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <linux/errno.h>
26
27 /* boot_id depends on sysctl */
28 #if defined(CONFIG_SYSCTL)
29
30 #include <linux/fs.h>
31 #include <linux/file.h>
32 #include <linux/sched.h>
33 #include <linux/uaccess.h>
34 #include <wrapper/random.h>
35
36 /*
37 * Returns string boot id.
38 */
39 int wrapper_get_bootid(char *bootid)
40 {
41 struct file *file;
42 int ret;
43 ssize_t len;
44 mm_segment_t old_fs;
45
46 file = filp_open("/proc/sys/kernel/random/boot_id", O_RDONLY, 0);
47 if (IS_ERR(file))
48 return PTR_ERR(file);
49
50 old_fs = get_fs();
51 set_fs(KERNEL_DS);
52
53 if (!file->f_op || !file->f_op->read) {
54 ret = -EINVAL;
55 goto end;
56 }
57
58 len = file->f_op->read(file, bootid, BOOT_ID_LEN - 1, &file->f_pos);
59 if (len != BOOT_ID_LEN - 1) {
60 ret = -EINVAL;
61 goto end;
62 }
63
64 bootid[BOOT_ID_LEN - 1] = '\0';
65 ret = 0;
66 end:
67 set_fs(old_fs);
68 filp_close(file, current->files);
69 return ret;
70 }
71
72 #else
73
74 int wrapper_get_bootid(char *bootid)
75 {
76 return -ENOSYS;
77 }
78
79 #endif
This page took 0.029551 seconds and 3 git commands to generate.