* @brief Convert process name to process ID * * @param[in] name Process name to look up (without path) * * @return pid_t Process ID if found, or 0 if not found * * @note The function only returns PIDs for processes whose main thread * is not in CLOSED state. */
| 1104 | * is not in CLOSED state. |
| 1105 | */ |
| 1106 | pid_t lwp_name2pid(const char *name) |
| 1107 | { |
| 1108 | int idx; |
| 1109 | pid_t pid = 0; |
| 1110 | rt_thread_t main_thread; |
| 1111 | char* process_name = RT_NULL; |
| 1112 | rt_sched_lock_level_t slvl; |
| 1113 | |
| 1114 | lwp_pid_lock_take(); |
| 1115 | for (idx = 0; idx < RT_LWP_MAX_NR; idx++) |
| 1116 | { |
| 1117 | /* 0 is reserved */ |
| 1118 | struct rt_lwp *lwp = (struct rt_lwp *)lwp_pid_ary[idx].data; |
| 1119 | |
| 1120 | if (lwp) |
| 1121 | { |
| 1122 | process_name = strrchr(lwp->exe_file, '/'); |
| 1123 | process_name = process_name? process_name + 1: lwp->cmd; |
| 1124 | if (!rt_strncmp(name, process_name, RT_NAME_MAX)) |
| 1125 | { |
| 1126 | main_thread = rt_list_entry(lwp->t_grp.prev, struct rt_thread, sibling); |
| 1127 | rt_sched_lock(&slvl); |
| 1128 | if (!(rt_sched_thread_get_stat(main_thread) == RT_THREAD_CLOSE)) |
| 1129 | { |
| 1130 | pid = lwp->pid; |
| 1131 | } |
| 1132 | rt_sched_unlock(slvl); |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | lwp_pid_lock_release(); |
| 1137 | return pid; |
| 1138 | } |
| 1139 | |
| 1140 | /** |
| 1141 | * @brief Get the process ID of the current lightweight process (LWP) |
no test coverage detected