library: rework namespace calls

Functions related to namespaces were half-in half-out of the
procps library and didn't fit the standard naming scheme.

While struct { long ns[x]} is a bit clunky, its the only way
to "lock in" x. The alternative is to use ns_* variables.

This work was needed before pgrep could be converted.
This commit is contained in:
Craig Small
2015-09-03 22:32:19 +10:00
parent d9caf0980e
commit a61f78d6e0
16 changed files with 297 additions and 134 deletions

1
proc/.gitignore vendored
View File

@@ -1 +1,2 @@
test_sysinfo
test_namespace

View File

@@ -7,8 +7,6 @@ global:
escape_strlist;
escaped_copy;
freeproc;
get_ns_id;
get_ns_name;
get_pid_digits;
look_up_our_self;
lookup_wchan;
@@ -41,6 +39,9 @@ global:
procps_meminfo_getstack;
procps_meminfo_stack_fill;
procps_meminfo_stack_alloc;
procps_ns_get_name;
procps_ns_get_id;
procps_ns_read_pid;
procps_pids_new;
procps_pids_read_next;
procps_pids_read_open;

110
proc/namespace.c Normal file
View File

@@ -0,0 +1,110 @@
/*
* libprocps - Library to read proc filesystem
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <proc/namespace.h>
#include "proc/procps-private.h"
#define NSPATHLEN 64
static const char *ns_names[] = {
[PROCPS_NS_IPC] = "ipc",
[PROCPS_NS_MNT] = "mnt",
[PROCPS_NS_NET] = "net",
[PROCPS_NS_PID] = "pid",
[PROCPS_NS_USER] = "user",
[PROCPS_NS_UTS] = "uts",
};
/*
* procps_ns_get_name:
*
* Find the name of the namespace with the given ID
*
* @id: The ID of the required namespace, see
* namespace_type
*
* Returns: static string of the namespace
*/
PROCPS_EXPORT const char *procps_ns_get_name(const int id)
{
if (id >= PROCPS_NS_COUNT || id < 0)
return NULL;
return ns_names[id];
}
/*
* procps_ns_get_id:
*
* Find the namespace ID that matches the given
* name.
*
* @name: the name of the required namespace
*
* Returns: ID of found name
* < 0 means error
*/
PROCPS_EXPORT int procps_ns_get_id(const char *name)
{
int i;
if (name == NULL)
return -EINVAL;
for (i=0; i < PROCPS_NS_COUNT; i++)
if (!strcmp(ns_names[i], name))
return i;
return -EINVAL;
}
/*
* procs_ns_read_pid:
*
* Find all namespaces for the given process.
* @pid: Process ID for required process
* @nsp: Pointer to the struct procps_namespaces
*
* Returns:
* 0 on success
* < 0 on error
*/
PROCPS_EXPORT int procps_ns_read_pid(
const int pid,
struct procps_namespaces *nsp)
{
char path[NSPATHLEN+1];
struct stat st;
int i;
if (nsp == NULL)
return -EINVAL;
if (pid < 1)
return -EINVAL;
for (i=0; i < PROCPS_NS_COUNT; i++) {
snprintf(path, NSPATHLEN, "/proc/%d/ns/%s", pid, ns_names[i]);
if (0 == stat(path, &st))
nsp->ns[i] = (long)st.st_ino;
else
nsp->ns[i] = 0;
}
}

51
proc/namespace.h Normal file
View File

@@ -0,0 +1,51 @@
/*
* libprocps - Library to read proc filesystem
*
* Copyright (C) 1996 Charles Blake <cblake@bbn.com>
* Copyright (C) 1998 Michael K. Johnson
* Copyright (C) 1998-2003 Albert Cahalan
* Copyright (C) 2015 Craig Small <csmall@enc.com.au>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PROC_NAMESPACE_H
#define PROC_NAMESPACE_H
#include <proc/procps.h>
__BEGIN_DECLS
enum namespace_type {
PROCPS_NS_IPC,
PROCPS_NS_MNT,
PROCPS_NS_NET,
PROCPS_NS_PID,
PROCPS_NS_USER,
PROCPS_NS_UTS,
PROCPS_NS_COUNT // total namespaces (fencepost)
};
struct procps_namespaces {
long ns[PROCPS_NS_COUNT];
};
const char *procps_ns_get_name(const int id);
int procps_ns_get_id(const char *name);
int procps_ns_read_pid(const int pid, struct procps_namespaces *nsp);
__END_DECLS
#endif

View File

@@ -172,12 +172,12 @@ REG_set(MEM_VIRT, sl_int, size)
CVT_set(MEM_VIRT_KIB, ul_int, size)
REG_set(NICE, sl_int, nice)
REG_set(NLWP, s_int, nlwp)
REG_set(NS_IPC, ul_int, ns[0])
REG_set(NS_MNT, ul_int, ns[1])
REG_set(NS_NET, ul_int, ns[2])
REG_set(NS_PID, ul_int, ns[3])
REG_set(NS_USER, ul_int, ns[4])
REG_set(NS_UTS, ul_int, ns[5])
REG_set(NS_IPC, ul_int, ns.ns[0])
REG_set(NS_MNT, ul_int, ns.ns[1])
REG_set(NS_NET, ul_int, ns.ns[2])
REG_set(NS_PID, ul_int, ns.ns[3])
REG_set(NS_USER, ul_int, ns.ns[4])
REG_set(NS_UTS, ul_int, ns.ns[5])
REG_set(OOM_ADJ, s_int, oom_adj)
REG_set(OOM_SCORE, s_int, oom_score)
REG_set(PRIORITY, s_int, priority)

View File

@@ -40,6 +40,7 @@
#ifdef WITH_SYSTEMD
#include <systemd/sd-login.h>
#endif
#include <proc/namespace.h>
// sometimes it's easier to do this manually, w/o gcc helping
#ifdef PROF
@@ -469,46 +470,6 @@ static void oomadj2proc(const char* S, proc_t *restrict P)
}
///////////////////////////////////////////////////////////////////////
static const char *ns_names[] = {
[IPCNS] = "ipc",
[MNTNS] = "mnt",
[NETNS] = "net",
[PIDNS] = "pid",
[USERNS] = "user",
[UTSNS] = "uts",
};
const char *get_ns_name(int id) {
if (id >= NUM_NS)
return NULL;
return ns_names[id];
}
int get_ns_id(const char *name) {
int i;
for (i = 0; i < NUM_NS; i++)
if (!strcmp(ns_names[i], name))
return i;
return -1;
}
static void ns2proc(const char *directory, proc_t *restrict p) {
char path[PROCPATHLEN];
struct stat sb;
int i;
for (i = 0; i < NUM_NS; i++) {
snprintf(path, sizeof(path), "%s/ns/%s", directory, ns_names[i]);
if (0 == stat(path, &sb))
p->ns[i] = (long)sb.st_ino;
#if 0
else // this allows a caller to distinguish
p->ns[i] = -errno; // between the ENOENT or EACCES errors
#endif
}
}
#ifdef WITH_SYSTEMD
static void sd2proc(proc_t *restrict p) {
char buf[64];
@@ -991,7 +952,8 @@ static proc_t* simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
}
if (unlikely(flags & PROC_FILLNS)) // read /proc/#/ns/*
ns2proc(path, p);
procps_ns_read_pid(p->tid, &(p->ns));
#ifdef WITH_SYSTEMD
if (unlikely(flags & PROC_FILLSYSTEMD)) // get sd-login.h stuff
@@ -1148,7 +1110,7 @@ static proc_t* simple_readtask(PROCTAB *restrict const PT, const proc_t *restric
}
if (unlikely(flags & PROC_FILLNS)) // read /proc/#/task/#/ns/*
ns2proc(path, t);
procps_ns_read_pid(t->tid, &(t->ns));
return t;
next_task:

View File

@@ -12,6 +12,7 @@
#include <proc/procps.h>
#include <proc/pwcache.h>
#include <proc/namespace.h>
#define SIGNAL_STRING
//#define QUICK_THREADS /* copy (vs. read) some thread info from parent proc_t */
@@ -163,8 +164,7 @@ typedef struct proc_t {
int
oom_score, // oom_score (badness for OOM killer)
oom_adj; // oom_adj (adjustment to OOM score)
long
ns[NUM_NS]; // (ns subdir) inode number of namespaces
struct procps_namespaces ns; // (ns subdir) inode number of namespaces
char
*sd_mach, // n/a systemd vm/container name
*sd_ouid, // n/a systemd session owner uid

87
proc/test_namespace.c Normal file
View File

@@ -0,0 +1,87 @@
/*
* libprocps - Library to read proc filesystem
* Tests for namespace library calls
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <proc/namespace.h>
struct test_func {
int (*func)(void *data);
char *name;
};
int check_name_minus(void *data)
{
return (procps_ns_get_name(-1) == NULL);
}
int check_name_over(void *data)
{
return (procps_ns_get_name(999) == NULL);
}
int check_name_ipc(void *data)
{
return (strcmp(procps_ns_get_name(PROCPS_NS_IPC),"ipc")==0);
}
int check_id_null(void *data)
{
return (procps_ns_get_id(NULL) < 0);
}
int check_id_unfound(void *data)
{
return (procps_ns_get_id("foobar") < 0);
}
int check_id_mnt(void *data)
{
return (procps_ns_get_id("mnt") == PROCPS_NS_MNT);
}
struct test_func tests[] = {
{ check_name_minus, "procps_ns_get_name() negative id"},
{ check_name_over, "procps_ns_get_name() id over limit"},
{ check_name_ipc, "procps_ns_get_name() ipc"},
{ check_id_null, "procps_ns_get_id(NULL)"},
{ check_id_unfound, "procps_ns_get_id(unknown)"},
{ check_id_mnt, "procps_ns_get_id(mnt)"},
{ NULL, NULL}
};
int main(int argc, char *argv[])
{
int i;
struct test_func *current;
for(i=0; tests[i].func != NULL; i++) {
current = &tests[i];
if (!current->func(NULL)) {
fprintf(stderr, "FAIL: %s\n", current->name);
return EXIT_FAILURE;
} else {
fprintf(stderr, "PASS: %s\n", current->name);
}
}
return EXIT_SUCCESS;
}