proc_self.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "proc.h"
  10. #include "procfs.h"
  11. #include <rthw.h>
  12. #include <rtdbg.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <dfs_dentry.h>
  16. #if defined(RT_USING_SMART)
  17. #include <lwp.h>
  18. int proc_self_readlink(struct proc_dentry *dentry, char *buf, int len)
  19. {
  20. struct rt_lwp *lwp = RT_NULL;
  21. lwp = lwp_self();
  22. if (lwp)
  23. {
  24. rt_snprintf(buf, len, "%d", lwp_to_pid(lwp));
  25. buf[len - 1] = 0;
  26. return rt_strlen(buf);
  27. }
  28. else
  29. {
  30. rt_snprintf(buf, len, "null");
  31. buf[len - 1] = 0;
  32. return rt_strlen(buf);
  33. }
  34. return -1;
  35. }
  36. static const struct proc_ops proc_pid_fd_ops = {
  37. .readlink = proc_self_readlink,
  38. };
  39. int proc_self_init(void)
  40. {
  41. struct proc_dentry *ent;
  42. ent = proc_symlink("self", NULL, "NULL");
  43. if (ent)
  44. {
  45. ent->ops = &proc_pid_fd_ops;
  46. }
  47. proc_release(ent);
  48. return 0;
  49. }
  50. INIT_ENV_EXPORT(proc_self_init);
  51. #endif