From 291bffb3d400356084d88120c5ba05a1a5fe6b3f Mon Sep 17 00:00:00 2001 From: rim Date: Thu, 17 May 2018 01:22:13 +0300 Subject: [PATCH] BSD, FreeBSD: --- src/task-manager-bsd.c | 6 ++++-- src/task-manager-freebsd.c | 65 ++++++++++++++++++++++++++++++++++++++++++++--------------------- src/task-manager-linux.c | 18 ++++++++++-------- src/task-manager-skel.c | 9 +++++---- src/task-manager-solaris.c | 15 ++++++++------- 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/src/task-manager-bsd.c b/src/task-manager-bsd.c index 79d1b35..67fabbd 100644 --- a/src/task-manager-bsd.c +++ b/src/task-manager-bsd.c @@ -102,6 +102,7 @@ gboolean get_task_list (GArray *task_list) #else struct kinfo_proc2 p = kp[i]; #endif + memset(&t, 0x00, sizeof(t)); t.pid = p.p_pid; t.ppid = p.p_ppid; t.uid = p.p_uid; @@ -113,9 +114,10 @@ gboolean get_task_list (GArray *task_list) g_strlcpy(t.name, p.p_comm, strlen(p.p_comm) + 1); /* shamelessly stolen from top/machine.c */ if (!P_ZOMBIE(&p)) { - size = 128; + size = 1024; if ((args = malloc(size)) == NULL) errx(1,"failed to allocate memory for argv structures at %zu", size); + memset(args, 0x00, size); for (;; size *= 2) { if ((args = realloc(args, size)) == NULL) errx(1,"failed to allocate memory (size=%zu) for argv structures of pid %d", size, t.pid); @@ -140,7 +142,7 @@ gboolean get_task_list (GArray *task_list) } t.cpu_user = (100.0 * ((double) p.p_pctcpu / FSCALE)); - t.cpu_system = 0; /* TODO ? */ + t.cpu_system = 0.0f; /* TODO ? */ /* get username from uid */ passwdp = getpwuid(t.uid); if(passwdp != NULL && passwdp->pw_name != NULL) diff --git a/src/task-manager-freebsd.c b/src/task-manager-freebsd.c index 24c0122..84b8e5f 100644 --- a/src/task-manager-freebsd.c +++ b/src/task-manager-freebsd.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2010 Mike Massonnet * Copyright (c) 2006 Oliver Lehmann + * Copyright (c) 2018 Rozhuk Ivan * * 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 @@ -18,6 +19,7 @@ #include #include #include +#include #if defined(__FreeBSD_version) && __FreeBSD_version >= 900044 #include #endif @@ -45,7 +47,7 @@ get_mem_by_pages (const gchar *name) res = get_mem_by_bytes (name); if (res > 0) - res = res * getpagesize (); + res *= getpagesize (); return res; } @@ -70,8 +72,8 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c return FALSE; kvm_getswapinfo (kd, &kswap, 1, 0); - *swap_total = kswap.ksw_total * getpagesize (); - *swap_free = (kswap.ksw_total - kswap.ksw_used) * getpagesize (); + *swap_total = ((guint64)kswap.ksw_total) * getpagesize (); + *swap_free = ((guint64)(kswap.ksw_total - kswap.ksw_used)) * getpagesize (); kvm_close (kd); } @@ -104,11 +106,11 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system) cp_system = cpu_state[CP_SYS] + cpu_state[CP_INTR]; cp_total = cpu_state[CP_IDLE] + cp_user + cp_system; - *cpu_user = *cpu_system = 0.0; + *cpu_user = *cpu_system = 0.0f; if (cp_total > cp_total_old) { - *cpu_user = (cp_user - cp_user_old) * 100 / (gdouble)(cp_total - cp_total_old); - *cpu_system = (cp_system - cp_system_old) * 100 / (gdouble)(cp_total - cp_total_old); + *cpu_user = (cp_user - cp_user_old) * 100.0f / (float)(cp_total - cp_total_old); + *cpu_system = (cp_system - cp_system_old) * 100.0f / (float)(cp_total - cp_total_old); } } @@ -119,33 +121,54 @@ static gboolean get_task_details (kvm_t *kd, struct kinfo_proc *kp, Task *task) { struct passwd *pw; - char **argv; - int i; + char buf[1024], *p; + size_t bufsz; + int i, oid[4]; + memset(task, 0x00, sizeof(Task)); task->pid = kp->ki_pid; task->ppid = kp->ki_ppid; - task->cpu_user = 100 * ((double)kp->ki_pctcpu / FSCALE); - task->cpu_system = 0; + task->cpu_user = 100.0f * ((float)kp->ki_pctcpu / FSCALE); + task->cpu_system = 0.0f; task->vsz = kp->ki_size; task->rss = kp->ki_rssize * getpagesize (); pw = getpwuid (kp->ki_uid); task->uid = kp->ki_uid; g_strlcpy (task->uid_name, (pw != NULL) ? pw->pw_name : "nobody", sizeof (task->uid_name)); task->prio = (gushort)kp->ki_nice; - g_strlcpy (task->name, kp->ki_comm, 256); - - task->cmdline[0] = '\0'; - if ((argv = kvm_getargv (kd, kp, 1024)) != NULL) - { - for (i = 0; argv[i] != NULL; i++) - { - g_strlcat (task->cmdline, argv[i], 1024); - g_strlcat (task->cmdline, " ", 1024); + g_strlcpy (task->name, kp->ki_comm, sizeof(task->name)); + + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = kp->ki_pid; + bufsz = sizeof(buf); + memset(buf, 0x00, sizeof(buf)); + if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) { + /* + * If the supplied buf is too short to hold the requested + * value the sysctl returns with ENOMEM. The buf is filled + * with the truncated value and the returned bufsz is equal + * to the requested len. + */ + if (errno != ENOMEM || bufsz != sizeof(buf)) { + bufsz = 0; + } else { + buf[(bufsz - 1)] = 0; } } + + if (0 != bufsz) { + p = buf; + do { + g_strlcat (task->cmdline, p, sizeof(task->cmdline)); + g_strlcat (task->cmdline, " ", sizeof(task->cmdline)); + p += (strlen(p) + 1); + } while (p < buf + bufsz); + } else { - g_strlcpy (task->cmdline, kp->ki_comm, 1024); + g_strlcpy (task->cmdline, kp->ki_comm, sizeof(task->cmdline)); } i = 0; @@ -213,7 +236,7 @@ get_task_list (GArray *task_list) { kvm_t *kd; struct kinfo_proc *kp; - int cnt, i; + int cnt = 0, i; Task task; if ((kd = kvm_openfiles (_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL)) == NULL) diff --git a/src/task-manager-linux.c b/src/task-manager-linux.c index 1262d08..2e26a70 100644 --- a/src/task-manager-linux.c +++ b/src/task-manager-linux.c @@ -40,7 +40,7 @@ get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_c *swap_total = 0; *swap_free = 0; - while (found < 6 && fgets (buffer, 1024, file) != NULL) + while (found < 6 && fgets (buffer, sizeof(buffer), file) != NULL) { found += sscanf (buffer, "MemTotal:\t%llu kB", (unsigned long long*)memory_total); found += sscanf (buffer, "MemFree:\t%llu kB", (unsigned long long*)memory_free); @@ -71,14 +71,14 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system) static gulong jiffies_user_old = 0, jiffies_system_old = 0, jiffies_total_old = 0; gulong user = 0, user_nice = 0, system = 0, idle = 0; - if ((file = fopen (filename, "r")) == NULL || fgets (buffer, 1024, file) == NULL) + if ((file = fopen (filename, "r")) == NULL || fgets (buffer, sizeof(buffer), file) == NULL) return FALSE; sscanf (buffer, "cpu\t%lu %lu %lu %lu", &user, &user_nice, &system, &idle); if (_cpu_count == 0) { - while (fgets (buffer, 1024, file) != NULL) + while (fgets (buffer, sizeof(buffer), file) != NULL) { if (buffer[0] != 'c' && buffer[1] != 'p' && buffer[2] != 'u') break; @@ -130,7 +130,7 @@ get_task_cmdline (Task *task) gint i; gint c; - snprintf (filename, 96, "/proc/%i/cmdline", task->pid); + snprintf (filename, sizeof(filename), "/proc/%i/cmdline", task->pid); if ((file = fopen (filename, "r")) == NULL) return FALSE; @@ -195,10 +195,12 @@ get_task_details (guint pid, Task *task) gchar buffer[1024]; snprintf (filename, sizeof(filename), "/proc/%d/stat", pid); - if ((file = fopen (filename, "r")) == NULL || fgets (buffer, 1024, file) == NULL) + if ((file = fopen (filename, "r")) == NULL || fgets (buffer, sizeof(buffer), file) == NULL) return FALSE; fclose (file); + memset(task, 0x00, sizeof(Task)); + /* Scanning the short process name is unreliable with scanf when it contains * spaces, retrieve it manually and fill the buffer */ { @@ -313,7 +315,7 @@ get_task_list (GArray *task_list) GDir *dir; const gchar *name; guint pid; - Task task = { 0 }; + Task task; if ((dir = g_dir_open ("/proc", 0, NULL)) == NULL) return FALSE; @@ -342,11 +344,11 @@ pid_is_sleeping (guint pid) gchar buffer[1024]; gchar state[2]; - snprintf (filename, 96, "/proc/%i/status", pid); + snprintf (filename, sizeof(filename), "/proc/%i/status", pid); if ((file = fopen (filename, "r")) == NULL) return FALSE; - while (fgets (buffer, 1024, file) != NULL) + while (fgets (buffer, sizeof(buffer), file) != NULL) { if (sscanf (buffer, "State:\t%1s", state) > 0) break; diff --git a/src/task-manager-skel.c b/src/task-manager-skel.c index d9d1ad5..5673523 100644 --- a/src/task-manager-skel.c +++ b/src/task-manager-skel.c @@ -50,18 +50,19 @@ get_cpu_usage (gushort *cpu_count, gfloat *cpu_user, gfloat *cpu_system) static gboolean get_task_details (guint pid, Task *task) { - g_snprintf (task->name, 256, "foo"); - g_snprintf (task->cmdline, 1024, "foo -bar"); - g_snprintf (task->uid_name, 256, "baz"); + memset(task, 0x00, sizeof(Task)); + g_snprintf (task->name, sizeof(task->name), "foo"); + g_snprintf (task->cmdline, sizeof(task->cmdline), "foo -bar"); + g_snprintf (task->uid_name, sizeof(task->uid_name), "baz"); return TRUE; } gboolean get_task_list (GArray *task_list) { guint pid = 0; - Task task = { 0 }; + Task task; //while (/* read all PIDs */) { diff --git a/src/task-manager-solaris.c b/src/task-manager-solaris.c index f2d54af..0fe53e3 100644 --- a/src/task-manager-solaris.c +++ b/src/task-manager-solaris.c @@ -176,21 +176,22 @@ get_task_details (guint pid, Task *task) struct passwd *pw; psinfo_t process; - snprintf (filename, 96, "/proc/%d/psinfo", pid); + snprintf (filename, sizeof(filename), "/proc/%d/psinfo", pid); if ((file = fopen (filename, "r")) == NULL) return FALSE; if (fread (&process, sizeof (psinfo_t), 1, file) != 1) { fclose (file); return FALSE; } + memset(task, 0x00, sizeof(Task)); task->pid = (guint)process.pr_pid; task->ppid = (guint)process.pr_ppid; - g_strlcpy (task->name, process.pr_fname, 256); - snprintf (task->cmdline, 1024, "%s", process.pr_psargs); - snprintf (task->state, 16, "%c", process.pr_lwp.pr_sname); + g_strlcpy (task->name, process.pr_fname, sizeof(task->name)); + snprintf (task->cmdline, sizeof(task->cmdline), "%s", process.pr_psargs); + snprintf (task->state, sizeof(task->state), "%c", process.pr_lwp.pr_sname); task->vsz = (guint64)process.pr_size * 1024; task->rss = (guint64)process.pr_rssize * 1024; task->prio = (gushort)process.pr_lwp.pr_pri; @@ -210,7 +211,7 @@ get_task_list (GArray *task_list) GDir *dir; const gchar *name; guint pid; - Task task = { 0 }; + Task task; if ((dir = g_dir_open ("/proc", 0, NULL)) == NULL) return FALSE; @@ -237,17 +238,17 @@ pid_is_sleeping (guint pid) gchar state[2]; psinfo_t process; - snprintf (filename, 96, "/proc/%d/psinfo", pid); + snprintf (filename, sizeof(filename), "/proc/%d/psinfo", pid); if ((file = fopen (filename, "r")) == NULL) return FALSE; if (fread (&process, sizeof (psinfo_t), 1, file) != 1) { fclose (file); return FALSE; } - snprintf (state, 2, "%c", process.pr_lwp.pr_sname); + snprintf (state, sizeof(state), "%c", process.pr_lwp.pr_sname); fclose (file); return (state[0] == 'T') ? TRUE : FALSE; -- libgit2 0.27.0