1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  */
9 
10 #include "proc.h"
11 #include "procfs.h"
12 
13 #include <rthw.h>
14 #include <rtdbg.h>
15 
16 #include <fcntl.h>
17 #include <errno.h>
18 
19 #include <dfs_dentry.h>
20 
21 #if defined(RT_USING_SMART)
22 
23 #include <lwp.h>
24 
25 
proc_self_readlink(struct proc_dentry * dentry,char * buf,int len)26 int proc_self_readlink(struct proc_dentry *dentry, char *buf, int len)
27 {
28     struct rt_lwp *lwp  = RT_NULL;
29 
30     lwp = lwp_self();
31     if (lwp)
32     {
33         rt_snprintf(buf, len, "%d", lwp_to_pid(lwp));
34         buf[len - 1] = 0;
35         return rt_strlen(buf);
36     }
37     else
38     {
39         rt_snprintf(buf, len, "null");
40         buf[len - 1] = 0;
41         return rt_strlen(buf);
42     }
43 
44     return -1;
45 }
46 
47 static const struct proc_ops proc_pid_fd_ops = {
48     .readlink = proc_self_readlink,
49 };
50 
proc_self_init(void)51 int proc_self_init(void)
52 {
53     struct proc_dentry *ent;
54 
55     ent = proc_symlink("self", NULL, "NULL");
56     if (ent)
57     {
58         ent->ops = &proc_pid_fd_ops;
59     }
60     proc_release(ent);
61 
62     return 0;
63 }
64 INIT_ENV_EXPORT(proc_self_init);
65 
66 #endif
67