| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * wrapper/random.c |
| 4 | * |
| 5 | * wrapper around bootid read. Read the boot id through the /proc filesystem. |
| 6 | * |
| 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/errno.h> |
| 11 | |
| 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> |
| 19 | #include <wrapper/random.h> |
| 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 | } |
| 48 | |
| 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 | } |
| 56 | EXPORT_SYMBOL_GPL(wrapper_get_bootid); |
| 57 | |
| 58 | #else |
| 59 | |
| 60 | int wrapper_get_bootid(char *bootid) |
| 61 | { |
| 62 | return -ENOSYS; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(wrapper_get_bootid); |
| 65 | |
| 66 | #endif |