1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2009 Pino Toscano <pino@kde.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36
#endif /* HAVE_PATHS_H */
38
#include "ck-sysdeps.h"
42
struct proc_stat *ps; /* the statistics of a process */
45
static struct ps_context *pc = NULL;
48
get_proc_stat_from_pid (pid_t pid,
50
struct proc_stat **res_ps)
56
g_assert (res_ps != NULL);
59
err = ps_context_create (getproc (), &pc);
65
err = _proc_stat_create (pid, pc, &ps);
70
err = proc_stat_set_flags (ps, PSTAT_PID | flags);
81
ck_process_stat_get_ppid (CkProcessStat *stat)
83
g_return_val_if_fail (stat != NULL, -1);
85
return proc_stat_pid (stat->ps);
89
ck_process_stat_get_cmd (CkProcessStat *stat)
91
g_return_val_if_fail (stat != NULL, NULL);
93
return g_strdup (proc_stat_args (stat->ps));
97
ck_process_stat_get_tty (CkProcessStat *stat)
101
g_return_val_if_fail (stat != NULL, NULL);
103
tty = proc_stat_tty (stat->ps);
105
return tty ? g_strdup (ps_tty_name (tty)) : NULL;
109
ck_process_stat_new_for_unix_pid (pid_t pid,
110
CkProcessStat **stat,
114
struct proc_stat *ps;
117
g_return_val_if_fail (pid > 1, FALSE);
125
res = get_proc_stat_from_pid (pid, PSTAT_ARGS | PSTAT_TTY, &ps);
130
proc = g_new0 (CkProcessStat, 1);
138
ck_process_stat_free (CkProcessStat *stat)
140
_proc_stat_free (stat->ps);
146
ck_unix_pid_get_env_hash (pid_t pid)
148
struct proc_stat *ps;
155
g_return_val_if_fail (pid > 1, NULL);
157
res = get_proc_stat_from_pid (pid, PSTAT_ENV, &ps);
162
hash = g_hash_table_new_full (g_str_hash,
169
env_p = proc_stat_env (ps);
170
while (env_index < proc_stat_env_len (ps)) {
171
env_l = strlen (env_p);
172
env_index += env_l + 1;
175
vals = g_strsplit (env_p, "=", 2);
177
g_hash_table_insert (hash,
183
env_p = env_p + env_l + 1;
186
_proc_stat_free (ps);
192
ck_unix_pid_get_env (pid_t pid,
195
struct proc_stat *ps;
204
g_return_val_if_fail (pid > 1, NULL);
208
res = get_proc_stat_from_pid (pid, PSTAT_ENV, &ps);
213
prefix = g_strdup_printf ("%s=", var);
214
prefix_len = strlen (prefix);
218
env_p = proc_stat_env (ps);
219
while (env_index < proc_stat_env_len (ps)) {
220
env_l = strlen (env_p);
221
env_index += env_l + 1;
222
if (env_l && g_str_has_prefix (env_p, prefix)) {
223
val = g_strdup (env_p + prefix_len);
226
env_p = env_p + env_l + 1;
231
_proc_stat_free (ps);
237
ck_unix_pid_get_uid (pid_t pid)
239
struct proc_stat *ps;
243
g_return_val_if_fail (pid > 1, 0);
245
res = get_proc_stat_from_pid (pid, PSTAT_OWNER_UID, &ps);
250
uid = proc_stat_owner_uid (ps);
252
_proc_stat_free (ps);
258
ck_unix_pid_get_ppid (pid_t pid)
260
struct proc_stat *ps;
264
g_return_val_if_fail (pid > 1, 0);
266
res = get_proc_stat_from_pid (pid, PSTAT_PROC_INFO, &ps);
271
ppid = proc_stat_proc_info (ps)->ppid;
273
_proc_stat_free (ps);
279
ck_unix_pid_get_login_session_id (pid_t pid,
282
g_return_val_if_fail (pid > 1, FALSE);
288
ck_get_max_num_consoles (guint *num)
303
while ((t = getttyent ()) != NULL) {
304
if (t->ty_status & TTY_ON && strncmp (t->ty_name, "tty", 3) == 0)
308
/* Increment one more so that all consoles are properly counted
309
* this is arguable a bug in vt_add_watches().
326
ck_supports_activatable_consoles (void)
332
ck_get_console_device_for_num (guint num)
336
device = g_strdup_printf (_PATH_TTY "%u", num);
342
ck_get_console_num_from_device (const char *device,
351
if (device == NULL) {
355
if (sscanf (device, _PATH_TTY "%u", &n) == 1) {
367
ck_get_active_console_num (int console_fd,
376
g_assert (console_fd != -1);
381
res = readlink ("/dev/cons/vcs", buf, sizeof (buf));
383
/* the resolved path is like "/dev/vcs/$number", so skip
384
the non-number part at the start */
386
while ((*p) && ((*p < '0') || (*p > '9'))) {
390
cur_active = strtol (p, NULL, 10);
391
g_debug ("Current VT: tty%ld", cur_active);