1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-11-17 xqyjlj the first version
* 2023-11-29 Shell Add direct reference of sess for group
*/
#include "lwp.h"
#include "lwp_internal.h"
#include "lwp_syscall.h"
#include "terminal/terminal.h"
#define DBG_TAG "lwp.session"
#define DBG_LVL DBG_WARNING
#include <rtdbg.h>
rt_session_t lwp_session_find(pid_t sid)
{
rt_base_t level;
rt_session_t session = RT_NULL;
rt_list_t *node = RT_NULL;
struct rt_object_information *information = RT_NULL;
information = rt_object_get_information(RT_Object_Class_Session);
/* parameter check */
if ((sid < 0) || (information == RT_NULL))
{
return RT_NULL;
}
if (sid == 0)
{
sid = lwp_getpid();
}
/* enter critical */
level = rt_spin_lock_irqsave(&(information->spinlock));
/* try to find session */
rt_list_for_each(node, &(information->object_list))
{
session = (rt_session_t)rt_list_entry(node, struct rt_object, list);
if (session->sid == sid)
{
rt_spin_unlock_irqrestore(&(information->spinlock), level);
return session;
}
}
rt_spin_unlock_irqrestore(&(information->spinlock), level);
return RT_NULL;
}
rt_session_t lwp_session_create(rt_lwp_t leader)
{
rt_session_t session = RT_NULL;
/* parameter check */
if (leader == RT_NULL)
{
return RT_NULL;
}
session = rt_malloc(sizeof(struct rt_session));
if (session != RT_NULL)
{
rt_object_init(&(session->object), RT_Object_Class_Session, "session");
rt_list_init(&(session->processgroup));
rt_mutex_init(&(session->mutex), "session", RT_IPC_FLAG_PRIO);
session->leader = leader;
session->sid = leader->pid;
lwp_pgrp_update_children_info(leader->pgrp, session->sid, leader->pgid);
session->foreground_pgid = session->sid;
session->ctty = RT_NULL;
}
return session;
}
int lwp_session_delete(rt_session_t session)
{
int retry = 1;
lwp_tty_t ctty;
/* parameter check */
if (session == RT_NULL)
{
return -EINVAL;
}
/* clear children sid */
lwp_session_update_children_info(session, 0);
while (retry)
{
retry = 0;
ctty = session->ctty;
SESS_LOCK_NESTED(session);
if (session->ctty == ctty)
{
if (ctty)
{
SESS_UNLOCK(session);
/**
* Note: it's safe to release the session lock now. Even if someone
* race to acquire the tty, it's safe under protection of tty_lock()
* and the check inside
*/
tty_lock(ctty);
tty_rel_sess(ctty, session);
session->ctty = RT_NULL;
}
else
{
SESS_UNLOCK(session);
}
}
else
{
SESS_UNLOCK(session);
retry = 1;
}
}
rt_object_detach(&(session->object));
rt_mutex_detach(&(session->mutex));
rt_free(session);
return 0;
}
int lwp_session_insert(rt_session_t session, rt_processgroup_t group)
{
/* parameter check */
if (session == RT_NULL || group == RT_NULL)
{
return -EINVAL;
}
SESS_LOCK_NESTED(session);
PGRP_LOCK_NESTED(group);
group->sid = session->sid;
group->session = session;
lwp_pgrp_update_children_info(group, session->sid, group->pgid);
rt_list_insert_after(&(session->processgroup), &(group->pgrp_list_node));
PGRP_UNLOCK(group);
SESS_UNLOCK(session);
return 0;
}
int lwp_session_remove(rt_session_t session, rt_processgroup_t group)
{
rt_bool_t is_empty = RT_FALSE;
/* parameter check */
if (session == RT_NULL || group == RT_NULL)
{
return -EINVAL;
}
SESS_LOCK_NESTED(session);
PGRP_LOCK_NESTED(group);
rt_list_remove(&(group->pgrp_list_node));
/* clear children sid */
lwp_pgrp_update_children_info(group, 0, group->pgid);
group->sid = 0;
group->session = RT_NULL;
PGRP_UNLOCK(group);
is_empty = rt_list_isempty(&(session->processgroup));
SESS_UNLOCK(session);
if (is_empty)
{
lwp_session_delete(session);
return 1;
}
return 0;
}
int lwp_session_move(rt_session_t session, rt_processgroup_t group)
{
rt_session_t prev_session;
/* parameter check */
if (session == RT_NULL || group == RT_NULL)
{
return -EINVAL;
}
if (lwp_sid_get_bysession(session) == lwp_sid_get_bypgrp(group))
{
return 0;
}
SESS_LOCK(session);
prev_session = group->session;
if (prev_session)
{
SESS_LOCK(prev_session);
lwp_session_remove(prev_session, group);
SESS_UNLOCK(prev_session);
}
lwp_session_insert(session, group);
SESS_UNLOCK(session);
return 0;
}
int lwp_session_update_children_info(rt_session_t session, pid_t sid)
{
rt_list_t *node = RT_NULL;
rt_processgroup_t group = RT_NULL;
if (session == RT_NULL)
{
return -EINVAL;
}
SESS_LOCK_NESTED(session);
rt_list_for_each(node, &(session->processgroup))
{
group = (rt_processgroup_t)rt_list_entry(node, struct rt_processgroup, pgrp_list_node);
PGRP_LOCK_NESTED(group);
if (sid != -1)
{
group->sid = sid;
group->session = session;
lwp_pgrp_update_children_info(group, sid, group->pgid);
}
PGRP_UNLOCK(group);
}
SESS_UNLOCK(session);
return 0;
}
int lwp_session_set_foreground(rt_session_t session, pid_t pgid)
{
rt_processgroup_t group = RT_NULL;
rt_list_t *node = RT_NULL;
rt_bool_t is_contains = RT_FALSE;
/* parameter check */
if (session == RT_NULL || pgid <= 0)
{
return -EINVAL;
}
SESS_LOCK(session);
rt_list_for_each(node, &(session->processgroup))
{
group = (rt_processgroup_t)rt_list_entry(node, struct rt_processgroup, pgrp_list_node);
PGRP_LOCK(group);
if (group->pgid == pgid)
{
is_contains = RT_TRUE;
}
PGRP_UNLOCK(group);
}
if (is_contains)
{
session->foreground_pgid = pgid;
// TODO: maybe notify tty
}
SESS_UNLOCK(session);
return is_contains ? 0 : -EINVAL;
}
/**
* setsid() creates a new session if the calling process is not a process group leader.
* The calling process is the leader of the new session (i.e., its session ID is made the same as its process ID).
* The calling process also becomes the process group leader of a new process group in the session
* (i.e., its process group ID is made the same as its process ID).
*/
sysret_t sys_setsid(void)
{
rt_lwp_t process;
pid_t pid;
rt_processgroup_t group;
rt_session_t session;
sysret_t err = 0;
process = lwp_self();
pid = lwp_to_pid(process);
/**
* if the calling process is already a process group leader.
*/
if (lwp_pgrp_find(pid))
{
err = -EPERM;
goto exit;
}
group = lwp_pgrp_create(process);
if (group)
{
lwp_pgrp_move(group, process);
session = lwp_session_create(process);
if (session)
{
lwp_session_move(session, group);
}
else
{
lwp_pgrp_delete(group);
}
err = lwp_sid_get_bysession(session);
}
else
{
err = -ENOMEM;
}
exit:
return err;
}
/**
* getsid() returns the session ID of the process with process ID pid.
* If pid is 0, getsid() returns the session ID of the calling process.
*/
sysret_t sys_getsid(pid_t pid)
{
rt_lwp_t process, self_process;
pid_t sid;
lwp_pid_lock_take();
process = lwp_from_pid_locked(pid);
lwp_pid_lock_release();
if (process == RT_NULL)
{
return -ESRCH;
}
self_process = lwp_self();
sid = lwp_sid_get_byprocess(process);
if (sid != lwp_sid_get_byprocess(self_process))
{
/**
* A process with process ID pid exists, but it is not in the same session as the calling process,
* and the implementation considers this an error.
*
* Note: Linux does not return EPERM.
*/
return -EPERM;
}
return sid;
}
#ifdef RT_USING_FINSH
#include "finsh.h"
long list_session(void)
{
int count = 0, index;
rt_session_t *sessions;
rt_session_t session;
rt_thread_t thread;
char name[RT_NAME_MAX];
rt_kprintf("SID leader process\n");
rt_kprintf("---- ----------------\n");
count = rt_object_get_length(RT_Object_Class_Session);
if (count > 0)
{
/* get pointers */
sessions = (rt_session_t *)rt_calloc(count, sizeof(rt_session_t));
if (sessions)
{
index = rt_object_get_pointers(RT_Object_Class_Session, (rt_object_t *)sessions, count);
if (index > 0)
{
for (index = 0; index < count; index++)
{
struct rt_session se;
session = sessions[index];
SESS_LOCK(session);
rt_memcpy(&se, session, sizeof(struct rt_session));
SESS_UNLOCK(session);
if (se.leader && se.leader)
{
thread = rt_list_entry(se.leader->t_grp.prev, struct rt_thread, sibling);
rt_strncpy(name, thread->parent.name, RT_NAME_MAX);
}
else
{
rt_strncpy(name, "nil", RT_NAME_MAX);
}
rt_kprintf("%4d %-*.*s\n", se.sid, RT_NAME_MAX, RT_NAME_MAX, name);
}
}
rt_free(sessions);
}
}
return 0;
}
MSH_CMD_EXPORT(list_session, list session);
#endif
|