Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
886d51a3 | 3 | * wrapper/random.c |
a82c63f1 | 4 | * |
5b4ea313 | 5 | * wrapper around bootid read. Read the boot id through the /proc filesystem. |
a82c63f1 | 6 | * |
886d51a3 | 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a82c63f1 MD |
8 | */ |
9 | ||
2fa61a31 MD |
10 | #include <linux/errno.h> |
11 | ||
a82c63f1 MD |
12 | /* boot_id depends on sysctl */ |
13 | #if defined(CONFIG_SYSCTL) | |
14 | ||
15 | #include <linux/fs.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/uaccess.h> | |
5a2f5e92 | 19 | #include <wrapper/random.h> |
a82c63f1 MD |
20 | |
21 | /* | |
22 | * Returns string boot id. | |
23 | */ | |
24 | int wrapper_get_bootid(char *bootid) | |
25 | { | |
26 | struct file *file; | |
27 | int ret; | |
28 | ssize_t len; | |
29 | mm_segment_t old_fs; | |
30 | ||
31 | file = filp_open("/proc/sys/kernel/random/boot_id", O_RDONLY, 0); | |
32 | if (IS_ERR(file)) | |
33 | return PTR_ERR(file); | |
34 | ||
35 | old_fs = get_fs(); | |
36 | set_fs(KERNEL_DS); | |
37 | ||
38 | if (!file->f_op || !file->f_op->read) { | |
39 | ret = -EINVAL; | |
40 | goto end; | |
41 | } | |
42 | ||
43 | len = file->f_op->read(file, bootid, BOOT_ID_LEN - 1, &file->f_pos); | |
44 | if (len != BOOT_ID_LEN - 1) { | |
45 | ret = -EINVAL; | |
46 | goto end; | |
47 | } | |
1e543e7c | 48 | |
a82c63f1 MD |
49 | bootid[BOOT_ID_LEN - 1] = '\0'; |
50 | ret = 0; | |
51 | end: | |
52 | set_fs(old_fs); | |
53 | filp_close(file, current->files); | |
54 | return ret; | |
55 | } | |
1c999280 | 56 | EXPORT_SYMBOL_GPL(wrapper_get_bootid); |
a82c63f1 MD |
57 | |
58 | #else | |
59 | ||
60 | int wrapper_get_bootid(char *bootid) | |
61 | { | |
62 | return -ENOSYS; | |
63 | } | |
1c999280 | 64 | EXPORT_SYMBOL_GPL(wrapper_get_bootid); |
a82c63f1 MD |
65 | |
66 | #endif |