signalfd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. * 2023-08-29 zmq810150896 first version
  9. * 2024-04-08 TroyMitchell Add all function comments
  10. */
  11. #include <rtthread.h>
  12. #include <sys/signalfd.h>
  13. #include <dfs_file.h>
  14. #include <signal.h>
  15. #include <rthw.h>
  16. #include <sys/time.h>
  17. #include <lwp_signal.h>
  18. #include <lwp.h>
  19. #include <poll.h>
  20. #define SIGNALFD_MUTEX_NAME "signalfd"
  21. #define SIGINFO_MAX 10
  22. #define SIGNALFD_SHART_MAX RT_SIGNALFD_MAX_NUM
  23. static int is_head_init = 0;
  24. struct rt_signalfd_ctx
  25. {
  26. sigset_t sigmask;
  27. struct rt_mutex lock;
  28. siginfo_t info[SIGINFO_MAX];
  29. int sig_num;
  30. rt_wqueue_t signalfd_queue;
  31. struct rt_lwp *lwp[SIGNALFD_SHART_MAX];
  32. };
  33. static int signalfd_close(struct dfs_file *file);
  34. static int signalfd_poll(struct dfs_file *file, struct rt_pollreq *req);
  35. #ifndef RT_USING_DFS_V2
  36. static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count);
  37. #else
  38. static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
  39. #endif
  40. static int signalfd_add_notify(struct rt_signalfd_ctx *sfd);
  41. static const struct dfs_file_ops signalfd_fops =
  42. {
  43. .close = signalfd_close,
  44. .poll = signalfd_poll,
  45. .read = signalfd_read,
  46. };
  47. /**
  48. * @brief Closes the file descriptor associated with a signalfd file.
  49. * @param file Pointer to the file structure.
  50. * @return Upon successful completion, returns 0; otherwise, returns an error code.
  51. */
  52. static int signalfd_close(struct dfs_file *file)
  53. {
  54. struct rt_signalfd_ctx *sfd;
  55. if (file->vnode->ref_count != 1)
  56. return 0;
  57. sfd = file->vnode->data;
  58. if (sfd)
  59. {
  60. rt_mutex_detach(&sfd->lock);
  61. rt_free(sfd);
  62. }
  63. return 0;
  64. }
  65. /**
  66. * @brief Adds a signalfd file descriptor to the poll queue and checks for pending events.
  67. * @param file Pointer to the file structure.
  68. * @param req Pointer to the poll request structure.
  69. * @return The events that are ready on the file descriptor.
  70. */
  71. static int signalfd_poll(struct dfs_file *file, struct rt_pollreq *req)
  72. {
  73. struct rt_signalfd_ctx *sfd;
  74. int events = 0;
  75. if (file->vnode)
  76. {
  77. sfd = file->vnode->data;
  78. rt_poll_add(&sfd->signalfd_queue, req);
  79. signalfd_add_notify(sfd);
  80. rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
  81. if (sfd->sig_num)
  82. events |= POLLIN;
  83. rt_mutex_release(&sfd->lock);
  84. }
  85. return events;
  86. }
  87. #ifndef RT_USING_DFS_V2
  88. /**
  89. * @brief Reads signals from a signalfd file descriptor.
  90. * @param file Pointer to the file structure.
  91. * @param buf Pointer to the buffer to store the signals.
  92. * @param count Maximum number of bytes to read.
  93. * @return Upon successful completion, returns the number of bytes read; otherwise, returns an error code.
  94. */
  95. static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count)
  96. #else
  97. /**
  98. * @brief Reads signals from a signalfd file descriptor with file offset.
  99. * @param file Pointer to the file structure.
  100. * @param buf Pointer to the buffer to store the signals.
  101. * @param count Maximum number of bytes to read.
  102. * @param pos Pointer to the file offset.
  103. * @return Upon successful completion, returns the number of bytes read; otherwise, returns an negative error code.
  104. */
  105. static ssize_t signalfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  106. #endif
  107. {
  108. struct rt_signalfd_ctx *sfd = RT_NULL;
  109. struct signalfd_siginfo *buffer = RT_NULL;
  110. int user_buf_num = 0;
  111. int sig_num = 0;
  112. int i = 0;
  113. rt_err_t ret = -1;
  114. if (sizeof(struct signalfd_siginfo) > count)
  115. return -1;
  116. if (buf == RT_NULL)
  117. return -1;
  118. buffer = (struct signalfd_siginfo *)buf;
  119. user_buf_num = count / sizeof(struct signalfd_siginfo);
  120. if (file->vnode)
  121. {
  122. sfd = file->vnode->data;
  123. signalfd_add_notify(sfd);
  124. if ((sfd->sig_num == 0) && (file->flags & O_NONBLOCK))
  125. {
  126. ret = -EAGAIN;
  127. }
  128. else
  129. {
  130. if (sfd->sig_num == 0)
  131. {
  132. rt_wqueue_wait(&sfd->signalfd_queue, 0, RT_WAITING_FOREVER);
  133. }
  134. rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
  135. for (i = 0; i < sfd->sig_num; i++)
  136. {
  137. if (i < user_buf_num)
  138. {
  139. memcpy(&buffer[i], &sfd->info[i], sizeof(struct signalfd_siginfo));
  140. sfd->sig_num -= 1;
  141. sig_num += 1;
  142. }
  143. else
  144. {
  145. break;
  146. }
  147. }
  148. for (int j = 0; j < sfd->sig_num; j ++)
  149. {
  150. memcpy(&sfd->info[j], &sfd->info[i ++], sizeof(struct signalfd_siginfo));
  151. }
  152. rt_mutex_release(&sfd->lock);
  153. ret = sizeof(struct signalfd_siginfo) * sig_num;
  154. }
  155. }
  156. return ret;
  157. }
  158. /**
  159. * @brief Callback function for signalfd file descriptor notifications.
  160. * @param signalfd_queue Pointer to the signalfd queue.
  161. * @param signum Signal number.
  162. */
  163. static void signalfd_callback(rt_wqueue_t *signalfd_queue, int signum)
  164. {
  165. struct rt_signalfd_ctx *sfd;
  166. sfd = rt_container_of(signalfd_queue, struct rt_signalfd_ctx, signalfd_queue);
  167. if (sfd)
  168. {
  169. if (sigismember(&sfd->sigmask, signum))
  170. {
  171. rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
  172. if (sfd->sig_num < SIGINFO_MAX)
  173. {
  174. sfd->info[sfd->sig_num].si_signo = signum;
  175. sfd->sig_num += 1;
  176. }
  177. rt_mutex_release(&sfd->lock);
  178. rt_wqueue_wakeup(signalfd_queue, (void*)POLLIN);
  179. }
  180. }
  181. }
  182. /**
  183. * @brief Adds a signal file descriptor notification.
  184. * @param sfd Pointer to the signalfd context.
  185. * @return Upon successful completion, returns 0; otherwise, returns an error code.
  186. */
  187. static int signalfd_add_notify(struct rt_signalfd_ctx *sfd)
  188. {
  189. struct rt_lwp_notify *lwp_notify;
  190. rt_err_t ret = -1;
  191. rt_slist_t *node;
  192. int is_lwp = 0;
  193. rt_mutex_take(&sfd->lock, RT_WAITING_FOREVER);
  194. for (int i = 0; i < is_head_init; i++)
  195. {
  196. if (sfd->lwp[i])
  197. {
  198. if (sfd->lwp[i] == lwp_self())
  199. {
  200. is_lwp = 1;
  201. }
  202. }
  203. }
  204. if (is_lwp == 0)
  205. {
  206. sfd->lwp[is_head_init] = lwp_self();
  207. if (is_head_init == 0)
  208. {
  209. rt_slist_init(&sfd->lwp[is_head_init]->signalfd_notify_head);
  210. }
  211. lwp_notify = (struct rt_lwp_notify *)rt_malloc(sizeof(struct rt_lwp_notify));
  212. if (lwp_notify)
  213. {
  214. lwp_notify->notify = signalfd_callback;
  215. lwp_notify->signalfd_queue = &sfd->signalfd_queue;
  216. rt_slist_append(&sfd->lwp[is_head_init]->signalfd_notify_head, &(lwp_notify->list_node));
  217. is_head_init ++;
  218. ret = 0;
  219. }
  220. else
  221. {
  222. rt_slist_for_each(node, &sfd->lwp[is_head_init]->signalfd_notify_head)
  223. {
  224. struct rt_lwp_notify *n = rt_slist_entry(node, struct rt_lwp_notify, list_node);
  225. rt_slist_remove(&sfd->lwp[is_head_init]->signalfd_notify_head, &n->list_node);
  226. rt_free(n);
  227. }
  228. rt_set_errno(ENOMEM);
  229. }
  230. }
  231. rt_mutex_release(&sfd->lock);
  232. return ret;
  233. }
  234. /**
  235. * @brief Creates a new signalfd file descriptor or modifies an existing one.
  236. * @param fd File descriptor to modify (-1 to create a new one).
  237. * @param mask Signal mask.
  238. * @param flags File descriptor flags.
  239. * @return Upon successful completion, returns the file descriptor number; otherwise, returns an error code.
  240. */
  241. static int signalfd_do(int fd, const sigset_t *mask, int flags)
  242. {
  243. struct dfs_file *df;
  244. struct rt_signalfd_ctx *sfd;
  245. rt_err_t ret = 0;
  246. if (fd == -1)
  247. {
  248. fd = fd_new();
  249. if (fd < 0)
  250. return -1;
  251. ret = fd;
  252. df = fd_get(fd);
  253. if (df)
  254. {
  255. sfd = (struct rt_signalfd_ctx *)rt_malloc(sizeof(struct rt_signalfd_ctx));
  256. if (sfd)
  257. {
  258. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  259. if (df->vnode)
  260. {
  261. dfs_vnode_init(df->vnode, FT_REGULAR, &signalfd_fops);
  262. df->vnode->data = sfd;
  263. for (int i = 0; i < is_head_init; i++)
  264. {
  265. sfd->lwp[i] = RT_NULL;
  266. }
  267. sigemptyset(&sfd->sigmask);
  268. memcpy(&sfd->sigmask, mask, sizeof(sigset_t));
  269. rt_mutex_init(&sfd->lock, SIGNALFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  270. rt_wqueue_init(&sfd->signalfd_queue);
  271. if (signalfd_add_notify(sfd) < 0)
  272. {
  273. is_head_init = 0;
  274. fd_release(fd);
  275. rt_free(sfd);
  276. ret = -1;
  277. }
  278. sfd->sig_num = 0;
  279. df->flags |= flags;
  280. #ifdef RT_USING_DFS_V2
  281. df->fops = &signalfd_fops;
  282. #endif
  283. }
  284. else
  285. {
  286. fd_release(fd);
  287. rt_free(sfd);
  288. ret = -1;
  289. }
  290. }
  291. else
  292. {
  293. fd_release(fd);
  294. ret = -1;
  295. }
  296. }
  297. else
  298. {
  299. fd_release(fd);
  300. }
  301. }
  302. else
  303. {
  304. df = fd_get(fd);
  305. if (df)
  306. {
  307. sfd = df->vnode->data;
  308. df->flags = flags;
  309. sigemptyset(&sfd->sigmask);
  310. memcpy(&sfd->sigmask, mask, sizeof(sigset_t));
  311. ret = fd;
  312. }
  313. else
  314. {
  315. rt_set_errno(EBADF);
  316. ret = -1;
  317. }
  318. }
  319. return ret;
  320. }
  321. /**
  322. * @brief Creates a new signalfd file descriptor or modifies an existing one.
  323. * @param fd File descriptor to modify (-1 to create a new one).
  324. * @param mask Signal mask.
  325. * @param flags File descriptor flags.
  326. * @return Upon successful completion, returns the file descriptor number; otherwise, returns an error code.
  327. */
  328. int signalfd(int fd, const sigset_t *mask, int flags)
  329. {
  330. return signalfd_do(fd, mask, flags);
  331. }