1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2015-2019. All rights reserved. 3 * Description: LiteOS PROC FS Headfile 4 * Author: Huawei LiteOS Team 5 * Create: 2013-01-01 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without specific prior written 15 * permission. 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * --------------------------------------------------------------------------- */ 28 29 #ifndef _PROC_FS_H 30 #define _PROC_FS_H 31 #include <sys/types.h> 32 33 #ifdef LOSCFG_FS_PROC 34 #include "linux/spinlock.h" 35 #include "asm/atomic.h" 36 #include "fs/fs.h" 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif /* __cplusplus */ 41 42 typedef unsigned short fmode_t; 43 #define MAX_NAMELEN 32 44 45 struct proc_file; 46 struct proc_dir_entry; 47 struct proc_file_operations; 48 49 typedef int (*read_proc_t)(struct proc_file *pf, void *buf); 50 typedef int (*write_proc_t)(struct proc_file *pf, const char *buf, size_t count, loff_t *ppos); 51 52 struct proc_file_operations { 53 char *name; 54 55 /* make a proc file */ 56 loff_t (*llseek)(struct proc_file *pf, loff_t offset, int whence); 57 int (*open)(struct inode *inode, struct proc_file *pf); 58 int (*release)(struct inode *inode, struct proc_file *pf); 59 ssize_t (*read)(struct proc_file *pf, char *buf, size_t count, loff_t *ppos); 60 ssize_t (*write)(struct proc_file *pf, const char *buf, size_t count, loff_t *ppos); 61 }; 62 63 struct proc_dir_entry { 64 mode_t mode; 65 int flags; 66 const struct proc_file_operations *proc_fops; 67 struct proc_file *pf; 68 struct proc_dir_entry *next, *parent, *subdir; 69 void *data; 70 read_proc_t read_proc; 71 write_proc_t write_proc; 72 atomic_t count; /* open file count */ 73 spinlock_t pde_unload_lock; 74 75 int namelen; 76 struct proc_dir_entry *pdir_current; 77 char name[MAX_NAMELEN]; 78 }; 79 80 struct proc_file { 81 fmode_t f_mode; 82 spinlock_t f_lock; 83 atomic_t f_count; 84 struct proc_dir_entry *pPDE; 85 void *private_data; 86 unsigned long long f_version; 87 loff_t f_pos; 88 char name[MAX_NAMELEN]; 89 }; 90 91 /** 92 * Interface for modules using proc below internal proc moudule; 93 */ 94 /** 95 * @ingroup procfs 96 * @brief create a proc node 97 * 98 * @par Description: 99 * This API is used to create the node by 'name' and parent inode 100 * 101 * @attention 102 * <ul> 103 * <li>This interface should be called after system initialization.</li> 104 * <li>The parameter name should be a valid string.</li> 105 * </ul> 106 * 107 * @param name [IN] Type #const char * The name of the node to be created. 108 * @param mode [IN] Type #mode_t the mode of create's node. 109 * @param parent [IN] Type #struct proc_dir_entry * the parent node of the node to be created, 110 * if pass NULL, default parent node is "/proc". 111 * 112 * @retval #NULL Create failed. 113 * @retval #proc_dir_entry* Create successfully. 114 * @par Dependency: 115 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 116 * @see 117 * 118 * @since Huawei LiteOS V100R001C00 119 */ 120 extern struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, 121 struct proc_dir_entry *parent); 122 123 /** 124 * @ingroup procfs 125 * @brief remove a proc node 126 * 127 * @par Description: 128 * This API is used to remove the node by 'name' and parent inode 129 * 130 * @attention 131 * <ul> 132 * <li>This interface should be called after system initialization.</li> 133 * <li>The parameter name should be a valid string.</li> 134 * </ul> 135 * 136 * @param name [IN] Type #const char * The name of the node to be removed. 137 * @param parent [IN] Type #struct proc_dir_entry * the parent node of the node to be remove. 138 * 139 * @par Dependency: 140 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 141 * @see 142 * 143 * @since Huawei LiteOS V100R001C00 144 */ 145 extern void remove_proc_entry(const char *name, struct proc_dir_entry *parent); 146 147 /** 148 * @ingroup procfs 149 * @brief create a proc directory node 150 * 151 * @par Description: 152 * This API is used to create the directory node by 'name' and parent inode 153 * 154 * @attention 155 * <ul> 156 * <li>This interface should be called after system initialization.</li> 157 * <li>The parameter name should be a valid string.</li> 158 * </ul> 159 * 160 * @param name [IN] Type #const char * The name of the node directory to be created. 161 * @param parent [IN] Type #struct proc_dir_entry * the parent node of the directory node to be created, 162 * if pass NULL, default parent node is "/proc". 163 * 164 * @retval #NULL Create failed. 165 * @retval #proc_dir_entry* Create successfully. 166 * @par Dependency: 167 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 168 * @see 169 * 170 * @since Huawei LiteOS V100R001C00 171 */ 172 extern struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent); 173 174 /** 175 * @ingroup procfs 176 * @brief create a proc node 177 * 178 * @par Description: 179 * This API is used to create the node by 'name' and parent inode, 180 * And assignment operation function 181 * 182 * @attention 183 * <ul> 184 * <li>This interface should be called after system initialization.</li> 185 * <li>The parameter name should be a valid string.</li> 186 * </ul> 187 * 188 * @param name [IN] Type #const char * The name of the node to be created. 189 * @param mode [IN] Type #mode_t the mode of create's node. 190 * @param parent [IN] Type #struct proc_dir_entry * the parent node of the node to be created. 191 * @param proc_fops [IN] Type #const struct proc_file_operations * operation function of the node. 192 * 193 * @retval #NULL Create failed. 194 * @retval #proc_dir_entry* Create successfully. 195 * @par Dependency: 196 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 197 * @see 198 * 199 * @since Huawei LiteOS V100R001C00 200 */ 201 extern struct proc_dir_entry *proc_create(const char *name, mode_t mode, 202 struct proc_dir_entry *parent, const struct proc_file_operations *proc_fops); 203 204 /** 205 * @ingroup procfs 206 * @brief init proc fs 207 * 208 * @par Description: 209 * This API is used to init proc fs. 210 * 211 * @attention 212 * <ul> 213 * <li>None.</li> 214 * </ul> 215 * 216 * @param NONE 217 * 218 * @retval NONE 219 * @par Dependency: 220 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 221 * @see proc_fs_init 222 * 223 * @since Huawei LiteOS V100R001C00 224 */ 225 extern void proc_fs_init(void); 226 227 #ifdef __cplusplus 228 } 229 #endif /* __cplusplus */ 230 231 #endif /* LOSCFG_FS_PROC */ 232 #endif /* _PROC_FS_H */ 233