1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Interface for root device discovery via sysfs with optional
6  * bells and whistles.
7  */
8 #ifndef ROOTDEV_ROOTDEV_H_
9 #define ROOTDEV_ROOTDEV_H_
10 
11 #include <stdbool.h>
12 #include <sys/types.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * rootdev: returns the path to the root device in @path
20  * @path: pre-allocated char array the result will be written to
21  * @size: size of @path
22  * @full: whether to try to do full resolution. E.g., device-mapper
23  * @strip: whether to remove the partition # or not.
24  *
25  * Returns 0 on success, non-zero on error.
26  */
27 int rootdev(char *path, size_t size, bool full, bool strip);
28 
29 /* All interface below this point will most definitely be C specific. If
30  * we rewrite this as a C++ class, only the above generic interface should
31  * still be provided.
32  */
33 
34 /**
35  * rootdev_wrapper: rootdev equivalent with paths can be substituted.
36  */
37 int rootdev_wrapper(char *path, size_t size,
38                     bool full, bool strip,
39                     dev_t *dev,
40                     const char *search, const char *dev_path);
41 /**
42  * rootdev_get_device: finds the /dev path for @dev
43  * @dst: destination char array
44  * @size: size of @dst
45  * @dev: dev_t specifying the known root device
46  * @search: path to search under. NULL for default.
47  *
48  * Returns 0 on success, non-zero on error.
49  *
50  * The name of the devices is placed in @dst. It will not
51  * be qualified with /dev/ by default.
52  */
53 int rootdev_get_device(char *dst, size_t size, dev_t dev,
54                        const char *search);
55 
56 /**
57  * rootdev_get_device_slave: returns the first device under @device/slaves
58  * @slave: destination char array for storing the result
59  * @size: size of @slave
60  * @dev: pointer to a dev_t to populate
61  * @device: name of the device to probe, like "sdb"
62  * @search: path to search under. NULL for default.
63  *
64  * It is safe for @device == @slave.
65  */
66 void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
67                               const char *device, const char *search);
68 
69 /**
70  * rootdev_get_path: converts a device name to a path in the device tree
71  * @path: char array to store the path
72  * @size: size of @devpath
73  * @device: name of the device
74  * @dev_path: path to dev tree. NULL for default (/dev)
75  *
76  * A @dev of 0 is ignored.
77  *
78  * @path is populated for all return codes.
79  * Returns 0 on success and non-zero on error:
80  * -1 on unexpected errors (@path may be invalid)
81  *
82  * Nb, this function does NOT search /dev for a match.  It performs a normal
83  *     string concatenation.
84  *     We can't check if the device actually exists as vendors may create an
85  *     SELinux context we don't know about for it (in which case, this function
86  *     would always fail).
87  */
88 int rootdev_get_path(char *path, size_t size, const char *device,
89                      const char *dev_path);
90 
91 const char *rootdev_get_partition(const char *dst, size_t len);
92 void rootdev_strip_partition(char *dst, size_t len);
93 int rootdev_symlink_active(const char *path);
94 int rootdev_create_devices(const char *name, dev_t dev, bool symlink);
95 
96 #ifdef __cplusplus
97 }  /* extern "C" */
98 #endif
99 
100 #endif  /* ROOTDEV_ROOTDEV_H_ */
101