procps/proc/namespace.c

112 lines
2.6 KiB
C
Raw Normal View History

/*
* 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] = (unsigned long)st.st_ino;
else
nsp->ns[i] = 0;
}
miscellaneous: silence a whole bunch of clang warnings [ but most definitely not all of them by a long shot ] Reference(s): proc/diskstat.c:186:17: warning: unused variable 'is_disk' [-Wunused-variable] int retval, is_disk; ^ proc/namespace.c:110:1: warning: control may reach end of non-void function [-Wreturn-type] } ^ proc/readproc.c:1131:50: warning: address of array 'ent->d_name' will always evaluate to 'true' [-Wpointer-bo if(unlikely(unlikely(!ent) || unlikely(!ent->d_name))) return 0; ~~~~~~^~~~~~ proc/readproc.c:1158:50: warning: address of array 'ent->d_name' will always evaluate to 'true' [-Wpointer-bo if(unlikely(unlikely(!ent) || unlikely(!ent->d_name))) return 0; ~~~~~~^~~~~~ proc/sysinfo.c:45:12: warning: unused variable 'stat_fd' [-Wunused-variable] static int stat_fd = -1; ^ proc/sysinfo.c:49:12: warning: unused variable 'meminfo_fd' [-Wunused-variable] static int meminfo_fd = -1; ^ proc/sysinfo.c:51:12: warning: unused variable 'vminfo_fd' [-Wunused-variable] static int vminfo_fd = -1; ^ proc/sysinfo.c:53:12: warning: unused variable 'vm_min_free_fd' [-Wunused-variable] static int vm_min_free_fd = -1; ^ proc/uptime.c:157:12: warning: unused variable 'realseconds' [-Wunused-variable] time_t realseconds; ^ proc/uptime.c:158:16: warning: unused variable 'realtime' [-Wunused-variable] struct tm *realtime; ^ vmstat.c:574:20: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] DSTAT(PROCPS_DISKSTAT_READ_TIME), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ vmstat.c:578:20: warning: format specifies type 'unsigned int' but the argument has type 'unsigned long' [-Wformat] DSTAT(PROCPS_DISKSTAT_WRITE_TIME), ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ watch.c:230:7: warning: variable 'endptr' is uninitialized when used here [-Wuninitialized] if (*endptr == '\0') set_ansi_attribute(0); /* [m treated as [0m */ ^~~~~~ Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-09-07 10:30:00 +05:30
return 0;
}