1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2023 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _PROC_FS_H 33 #define _PROC_FS_H 34 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 38 #include "los_config.h" 39 40 #ifdef LOSCFG_FS_PROC 41 #include "linux/spinlock.h" 42 #include "asm/atomic.h" 43 #include "vnode.h" 44 #include "fs/file.h" 45 #include "los_seq_buf.h" 46 47 #ifdef __cplusplus 48 #if __cplusplus 49 extern "C" { 50 #endif /* __cplusplus */ 51 #endif /* __cplusplus */ 52 53 typedef unsigned short fmode_t; 54 #define PROC_ERROR (-1) 55 56 /* Default file mode for procfs */ 57 #define PROCFS_DEFAULT_MODE 0550 58 59 /* 64bit hashes as llseek() offset (for directories) */ 60 #define FMODE_64BITHASH ((fmode_t)0x400) 61 /* 32bit hashes as llseek() offset (for directories) */ 62 #define FMODE_32BITHASH ((fmode_t)0x200) 63 /* File is opened using open(.., 3, ..) and is writable only for ioctls 64 * (specialy hack for floppy.c) 65 */ 66 #define FMODE_WRITE_IOCTL ((fmode_t)0x100) 67 /* File is opened with O_EXCL (only set for block devices) */ 68 #define FMODE_EXCL ((fmode_t)0x80) 69 /* File is opened with O_NDELAY (only set for block devices) */ 70 #define FMODE_NDELAY ((fmode_t)0x40) 71 /* File is opened for execution with sys_execve / sys_uselib */ 72 #define FMODE_EXEC ((fmode_t)0x20) 73 /* file can be accessed using pwrite */ 74 #define FMODE_PWRITE ((fmode_t)0x10) 75 /* file can be accessed using pread */ 76 #define FMODE_PREAD ((fmode_t)0x8) 77 /* file is seekable */ 78 #define FMODE_LSEEK ((fmode_t)0x4) 79 /* file is open for writing */ 80 #define FMODE_WRITE ((fmode_t)0x2) 81 /* file is open for reading */ 82 #define FMODE_READ ((fmode_t)0x1) 83 84 struct ProcFile; 85 struct ProcDirEntry; 86 87 struct ProcFileOperations { 88 char *name; 89 ssize_t (*write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos); 90 int (*open)(struct Vnode *vnode, struct ProcFile *pf); 91 int (*release)(struct Vnode *vnode, struct ProcFile *pf); 92 int (*read)(struct SeqBuf *m, void *v); 93 ssize_t (*readLink)(struct ProcDirEntry *pde, char *buf, size_t bufLen); 94 }; 95 96 #ifdef LOSCFG_KERNEL_PLIMITS 97 struct ProcDirOperations { 98 int (*rmdir)(struct ProcDirEntry *parent, struct ProcDirEntry *pde, const char *name); 99 int (*mkdir)(struct ProcDirEntry *parent, const char *dirName, mode_t mode, struct ProcDirEntry **pde); 100 }; 101 #endif 102 103 #define PROC_DATA_STATIC 0 104 #define PROC_DATA_FREE 1 105 struct ProcDirEntry { 106 uint uid; 107 uint gid; 108 mode_t mode; 109 int flags; 110 const struct ProcFileOperations *procFileOps; 111 struct ProcFile *pf; 112 struct ProcDirEntry *next, *parent, *subdir; 113 #ifdef LOSCFG_KERNEL_PLIMITS 114 const struct ProcDirOperations *procDirOps; 115 #endif 116 int dataType; 117 void *data; 118 atomic_t count; /* open file count */ 119 spinlock_t pdeUnloadLock; 120 121 int nameLen; 122 struct ProcDirEntry *pdirCurrent; 123 char name[NAME_MAX]; 124 enum VnodeType type; 125 }; 126 127 struct ProcDataParm { 128 void *data; 129 int dataType; 130 }; 131 132 struct ProcFile { 133 fmode_t fMode; 134 spinlock_t fLock; 135 atomic_t fCount; 136 struct SeqBuf *sbuf; 137 struct ProcDirEntry *pPDE; 138 unsigned long long fVersion; 139 loff_t fPos; 140 char name[NAME_MAX]; 141 }; 142 143 struct ProcStat { 144 mode_t stMode; 145 struct ProcDirEntry *pPDE; 146 char name[NAME_MAX]; 147 }; 148 149 struct ProcData { 150 ssize_t size; 151 loff_t fPos; 152 char buf[1]; 153 }; 154 155 #define PROCDATA(n) (sizeof(struct ProcData) + (n)) 156 157 #define S_IALLUGO (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) 158 159 /** 160 * Interface for modules using proc below internal proc module; 161 */ 162 /** 163 * @ingroup procfs 164 * @brief create a proc node 165 * 166 * @par Description: 167 * This API is used to create the node by 'name' and parent vnode 168 * 169 * @attention 170 * <ul> 171 * <li>This interface should be called after system initialization.</li> 172 * <li>The parameter name should be a valid string.</li> 173 * </ul> 174 * 175 * @param name [IN] Type #const char * The name of the node to be created. 176 * @param mode [IN] Type #mode_t the mode of create's node. 177 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be created, 178 * if pass NULL, default parent node is "/proc". 179 * 180 * @retval #NULL Create failed. 181 * @retval #ProcDirEntry* Create successfully. 182 * @par Dependency: 183 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 184 * @see 185 * 186 */ 187 extern struct ProcDirEntry *CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent); 188 189 /** 190 * @ingroup procfs 191 * @brief remove a proc node 192 * 193 * @par Description: 194 * This API is used to remove the node by 'name' and parent vnode 195 * 196 * @attention 197 * <ul> 198 * <li>This interface should be called after system initialization.</li> 199 * <li>The parameter name should be a valid string.</li> 200 * </ul> 201 * 202 * @param name [IN] Type #const char * The name of the node to be removed. 203 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be remove. 204 * 205 * @par Dependency: 206 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 207 * @see 208 * 209 */ 210 extern void RemoveProcEntry(const char *name, struct ProcDirEntry *parent); 211 212 /** 213 * @ingroup procfs 214 * @brief create a proc directory node 215 * 216 * @par Description: 217 * This API is used to create the directory node by 'name' and parent vnode 218 * 219 * @attention 220 * <ul> 221 * <li>This interface should be called after system initialization.</li> 222 * <li>The parameter name should be a valid string.</li> 223 * </ul> 224 * 225 * @param name [IN] Type #const char * The name of the node directory to be created. 226 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the directory node to be created, 227 * if pass NULL, default parent node is "/proc". 228 * 229 * @retval #NULL Create failed. 230 * @retval #ProcDirEntry* Create successfully. 231 * @par Dependency: 232 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 233 * @see 234 * 235 */ 236 extern struct ProcDirEntry *ProcMkdir(const char *name, struct ProcDirEntry *parent); 237 238 /** 239 * @ingroup procfs 240 * @brief create a proc node 241 * 242 * @par Description: 243 * This API is used to create the node by 'name' and parent vnode, 244 * And assignment operation function 245 * 246 * @attention 247 * <ul> 248 * <li>This interface should be called after system initialization.</li> 249 * <li>The parameter name should be a valid string.</li> 250 * </ul> 251 * 252 * @param name [IN] Type #const char * The name of the node to be created. 253 * @param mode [IN] Type #mode_t the mode of create's node. 254 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be created. 255 * @param procFops [IN] Type #const struct ProcFileOperations * operation function of the node. 256 * 257 * @retval #NULL Create failed. 258 * @retval #ProcDirEntry* Create successfully. 259 * @par Dependency: 260 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 261 * @see 262 * 263 */ 264 extern struct ProcDirEntry *ProcCreate(const char *name, mode_t mode, 265 struct ProcDirEntry *parent, const struct ProcFileOperations *procFops); 266 267 /** 268 * @ingroup procfs 269 * @brief create a proc node 270 * 271 * @par Description: 272 * This API is used to create the node by 'name' and parent vnode, 273 * And assignment operation function 274 * 275 * @attention 276 * <ul> 277 * <li>This interface should be called after system initialization.</li> 278 * <li>The parameter name should be a valid string.</li> 279 * </ul> 280 * 281 * @param name [IN] Type #const char * The name of the node to be created. 282 * @param mode [IN] Type #mode_t the mode of create's node. 283 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be created. 284 * @param procFops [IN] Type #const struct ProcFileOperations * operation function of the node. 285 * @param data [IN] Type #void * data of the node. 286 * 287 * @retval #NULL Create failed. 288 * @retval #ProcDirEntry* Create successfully. 289 * @par Dependency: 290 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 291 * @see 292 * 293 */ 294 extern struct ProcDirEntry *ProcCreateData(const char *name, mode_t mode, struct ProcDirEntry *parent, 295 const struct ProcFileOperations *procFileOps, struct ProcDataParm *param); 296 /** 297 * @ingroup procfs 298 * @brief init proc fs 299 * 300 * @par Description: 301 * This API is used to init proc fs. 302 * 303 * @attention 304 * <ul> 305 * <li>None.</li> 306 * </ul> 307 * 308 * @param NONE 309 * 310 * @retval NONE 311 * @par Dependency: 312 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul> 313 * @see ProcFsInit 314 * 315 */ 316 extern void ProcFsInit(void); 317 318 extern struct ProcDirEntry *VnodeToEntry(struct Vnode *node); 319 #ifdef __cplusplus 320 #if __cplusplus 321 } 322 #endif /* __cplusplus */ 323 #endif /* __cplusplus */ 324 325 #endif /* LOSCFG_FS_PROC */ 326 #endif /* _PROC_FS_H */ 327