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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2009 Pino Toscano <pino@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <hurd.h>
#include <dirent.h>
#include <ps.h>
#include <ttyent.h>
#ifdef HAVE_PATHS_H
#include <paths.h>
#endif /* HAVE_PATHS_H */
#include "ck-sysdeps.h"
struct _CkProcessStat
{
struct proc_stat *ps; /* the statistics of a process */
};
static struct ps_context *pc = NULL;
static gboolean
get_proc_stat_from_pid (pid_t pid,
ps_flags_t flags,
struct proc_stat **res_ps)
{
error_t err;
struct proc_stat *ps;
g_assert (pid >= 0);
g_assert (res_ps != NULL);
if (pc == NULL) {
err = ps_context_create (getproc (), &pc);
if (err) {
return FALSE;
}
}
err = _proc_stat_create (pid, pc, &ps);
if (err) {
return FALSE;
}
err = proc_stat_set_flags (ps, PSTAT_PID | flags);
if (err) {
return FALSE;
}
*res_ps = ps;
return TRUE;
}
pid_t
ck_process_stat_get_ppid (CkProcessStat *stat)
{
g_return_val_if_fail (stat != NULL, -1);
return proc_stat_pid (stat->ps);
}
char *
ck_process_stat_get_cmd (CkProcessStat *stat)
{
g_return_val_if_fail (stat != NULL, NULL);
return g_strdup (proc_stat_args (stat->ps));
}
char *
ck_process_stat_get_tty (CkProcessStat *stat)
{
struct ps_tty *tty;
g_return_val_if_fail (stat != NULL, NULL);
tty = proc_stat_tty (stat->ps);
return tty ? g_strdup (ps_tty_name (tty)) : NULL;
}
gboolean
ck_process_stat_new_for_unix_pid (pid_t pid,
CkProcessStat **stat,
GError **error)
{
gboolean res;
struct proc_stat *ps;
CkProcessStat *proc;
g_return_val_if_fail (pid > 1, FALSE);
if (stat == NULL) {
return FALSE;
}
*stat = NULL;
res = get_proc_stat_from_pid (pid, PSTAT_ARGS | PSTAT_TTY, &ps);
if (!res) {
return FALSE;
}
proc = g_new0 (CkProcessStat, 1);
proc->ps = ps;
*stat = proc;
return TRUE;
}
void
ck_process_stat_free (CkProcessStat *stat)
{
_proc_stat_free (stat->ps);
g_free (stat);
}
GHashTable *
ck_unix_pid_get_env_hash (pid_t pid)
{
struct proc_stat *ps;
char *env_p;
size_t env_index;
size_t env_l;
gboolean res;
GHashTable *hash;
g_return_val_if_fail (pid > 1, NULL);
res = get_proc_stat_from_pid (pid, PSTAT_ENV, &ps);
if (!res) {
return NULL;
}
hash = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_free);
env_index = 0;
env_l = 0;
env_p = proc_stat_env (ps);
while (env_index < proc_stat_env_len (ps)) {
env_l = strlen (env_p);
env_index += env_l + 1;
if (env_l) {
char **vals;
vals = g_strsplit (env_p, "=", 2);
if (vals != NULL) {
g_hash_table_insert (hash,
g_strdup (vals[0]),
g_strdup (vals[1]));
g_strfreev (vals);
}
}
env_p = env_p + env_l + 1;
}
_proc_stat_free (ps);
return hash;
}
char *
ck_unix_pid_get_env (pid_t pid,
const char *var)
{
struct proc_stat *ps;
char *env_p;
size_t env_index;
size_t env_l;
char *prefix;
int prefix_len;
char *val;
gboolean res;
g_return_val_if_fail (pid > 1, NULL);
val = NULL;
res = get_proc_stat_from_pid (pid, PSTAT_ENV, &ps);
if (!res) {
return NULL;
}
prefix = g_strdup_printf ("%s=", var);
prefix_len = strlen (prefix);
env_index = 0;
env_l = 0;
env_p = proc_stat_env (ps);
while (env_index < proc_stat_env_len (ps)) {
env_l = strlen (env_p);
env_index += env_l + 1;
if (env_l && g_str_has_prefix (env_p, prefix)) {
val = g_strdup (env_p + prefix_len);
break;
}
env_p = env_p + env_l + 1;
}
g_free (prefix);
_proc_stat_free (ps);
return val;
}
uid_t
ck_unix_pid_get_uid (pid_t pid)
{
struct proc_stat *ps;
gboolean res;
uid_t uid;
g_return_val_if_fail (pid > 1, 0);
res = get_proc_stat_from_pid (pid, PSTAT_OWNER_UID, &ps);
if (!res) {
return 0;
}
uid = proc_stat_owner_uid (ps);
_proc_stat_free (ps);
return uid;
}
pid_t
ck_unix_pid_get_ppid (pid_t pid)
{
struct proc_stat *ps;
gboolean res;
pid_t ppid;
g_return_val_if_fail (pid > 1, 0);
res = get_proc_stat_from_pid (pid, PSTAT_PROC_INFO, &ps);
if (!res) {
return 0;
}
ppid = proc_stat_proc_info (ps)->ppid;
_proc_stat_free (ps);
return ppid;
}
gboolean
ck_unix_pid_get_login_session_id (pid_t pid,
char **idp)
{
g_return_val_if_fail (pid > 1, FALSE);
return FALSE;
}
gboolean
ck_get_max_num_consoles (guint *num)
{
int max_consoles;
int res;
gboolean ret;
struct ttyent *t;
ret = FALSE;
max_consoles = 0;
res = setttyent ();
if (res == 0) {
goto done;
}
while ((t = getttyent ()) != NULL) {
if (t->ty_status & TTY_ON && strncmp (t->ty_name, "tty", 3) == 0)
max_consoles++;
}
/* Increment one more so that all consoles are properly counted
* this is arguable a bug in vt_add_watches().
*/
max_consoles++;
ret = TRUE;
endttyent ();
done:
if (num != NULL) {
*num = max_consoles;
}
return ret;
}
gboolean
ck_supports_activatable_consoles (void)
{
return TRUE;
}
char *
ck_get_console_device_for_num (guint num)
{
char *device;
device = g_strdup_printf (_PATH_TTY "%u", num);
return device;
}
gboolean
ck_get_console_num_from_device (const char *device,
guint *num)
{
guint n;
gboolean ret;
n = 0;
ret = FALSE;
if (device == NULL) {
return FALSE;
}
if (sscanf (device, _PATH_TTY "%u", &n) == 1) {
ret = TRUE;
}
if (num != NULL) {
*num = n;
}
return ret;
}
gboolean
ck_get_active_console_num (int console_fd,
guint *num)
{
gboolean ret;
int res;
long cur_active;
char buf[30];
guint active;
g_assert (console_fd != -1);
active = 0;
ret = FALSE;
res = readlink ("/dev/cons/vcs", buf, sizeof (buf));
if (res > 0) {
/* the resolved path is like "/dev/vcs/$number", so skip
the non-number part at the start */
const char *p = buf;
while ((*p) && ((*p < '0') || (*p > '9'))) {
++p;
}
if (*p) {
cur_active = strtol (p, NULL, 10);
g_debug ("Current VT: tty%ld", cur_active);
active = cur_active;
ret = TRUE;
}
}
if (num != NULL) {
*num = active;
}
return ret;
}
|