| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
|
| 3 |
#include "get_pid_from_inode.h" |
| 4 |
|
| 5 |
#include <net-snmp/library/system.h> /* strlcpy() */ |
| 6 |
#include <net-snmp/output_api.h> |
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdio.h> |
| 10 |
#ifdef HAVE_STDLIB_H |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_STRING_H |
| 14 |
#include <string.h> |
| 15 |
#else |
| 16 |
#include <strings.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_UNISTD_H |
| 19 |
#include <unistd.h> |
| 20 |
#endif |
| 21 |
#ifdef HAVE_LIMITS_H |
| 22 |
#include <limits.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
# define PROC_PATH "/proc" |
| 26 |
# define SOCKET_TYPE_1 "socket:[" |
| 27 |
# define SOCKET_TYPE_2 "[0000]:" |
| 28 |
|
| 29 |
/* Definition of a simple open addressing hash table.*/ |
| 30 |
/* When inode == 0 then the entry is empty.*/ |
| 31 |
typedef struct { |
| 32 |
ino64_t inode; |
| 33 |
pid_t pid; |
| 34 |
} inode_pid_ent_t; |
| 35 |
|
| 36 |
#define INODE_PID_TABLE_MAX_COLLISIONS 1000 |
| 37 |
#define INODE_PID_TABLE_LENGTH 20000 |
| 38 |
#define INODE_PID_TABLE_SIZE (INODE_PID_TABLE_LENGTH * sizeof (inode_pid_ent_t)) |
| 39 |
static inode_pid_ent_t inode_pid_table[INODE_PID_TABLE_LENGTH]; |
| 40 |
|
| 41 |
static uint32_t |
| 42 |
_hash(uint64_t key) |
| 43 |
{ |
| 44 |
key = (~key) + (key << 18); |
| 45 |
key = key ^ (key >> 31); |
| 46 |
key = key * 21; |
| 47 |
key = key ^ (key >> 11); |
| 48 |
key = key + (key << 6); |
| 49 |
key = key ^ (key >> 22); |
| 50 |
return key; |
| 51 |
} |
| 52 |
|
| 53 |
static void |
| 54 |
_clear(void) |
| 55 |
{ |
| 56 |
/* Clear the inode/pid hash table.*/ |
| 57 |
memset(inode_pid_table, 0, INODE_PID_TABLE_SIZE); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
_set(ino64_t inode, pid_t pid) |
| 62 |
{ |
| 63 |
uint32_t hash = _hash(inode); |
| 64 |
uint32_t i; |
| 65 |
inode_pid_ent_t *entry; |
| 66 |
|
| 67 |
/* We will try for a maximum number of collisions.*/ |
| 68 |
for (i = 0; i < INODE_PID_TABLE_MAX_COLLISIONS; i++) { |
| 69 |
entry = &inode_pid_table[(hash + i) % INODE_PID_TABLE_LENGTH]; |
| 70 |
|
| 71 |
/* Check if this entry is empty, or the actual inode we were looking for.*/ |
| 72 |
/* The second part should never happen, but it is here for completeness.*/ |
| 73 |
if (entry->inode == 0 || entry->inode == inode) { |
| 74 |
entry->inode = inode; |
| 75 |
entry->pid = pid; |
| 76 |
return; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/* We will silently fail to insert the inode if we get too many collisions.*/ |
| 81 |
/* the _get function will return a zero pid.*/ |
| 82 |
} |
| 83 |
|
| 84 |
static pid_t _get(ino64_t inode) |
| 85 |
{ |
| 86 |
uint32_t hash = _hash(inode); |
| 87 |
uint32_t i; |
| 88 |
inode_pid_ent_t *entry; |
| 89 |
|
| 90 |
/* We will try for a maximum number of collisions.*/ |
| 91 |
for (i = 0; i < INODE_PID_TABLE_MAX_COLLISIONS; i++) { |
| 92 |
entry = &inode_pid_table[(hash + i) % INODE_PID_TABLE_LENGTH]; |
| 93 |
|
| 94 |
/* Check if this entry is empty, or the actual inode we were looking for.*/ |
| 95 |
/* If the entry is empty it means the inode is not in the table and we*/ |
| 96 |
/* should return 0, the entry will also have a zero pid.*/ |
| 97 |
if (entry->inode == 0 || entry->inode == inode) { |
| 98 |
return entry->pid; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/* We could not find the pid.*/ |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
void |
| 107 |
netsnmp_get_pid_from_inode_init(void) |
| 108 |
{ |
| 109 |
DIR *procdirs = NULL, *piddirs = NULL; |
| 110 |
char path_name[PATH_MAX + 1]; |
| 111 |
char socket_lnk[NAME_MAX + 1]; |
| 112 |
int filelen = 0, readlen = 0; |
| 113 |
struct dirent *procinfo, *pidinfo; |
| 114 |
pid_t pid = 0; |
| 115 |
ino64_t temp_inode; |
| 116 |
|
| 117 |
_clear(); |
| 118 |
|
| 119 |
/* walk over all directories in /proc*/ |
| 120 |
if (!(procdirs = opendir(PROC_PATH))) { |
| 121 |
NETSNMP_LOGONCE((LOG_ERR, "snmpd: cannot open /proc\n")); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
while ((procinfo = readdir(procdirs)) != NULL) { |
| 126 |
const char* name = procinfo->d_name; |
| 127 |
|
| 128 |
/* A pid directory only contains digits, check for those.*/ |
| 129 |
for (; *name; name++) { |
| 130 |
if (!isdigit(*name)) |
| 131 |
break; |
| 132 |
} |
| 133 |
if(*name) |
| 134 |
continue; |
| 135 |
|
| 136 |
/* Create the /proc/<pid>/fd/ path name.*/ |
| 137 |
memset(path_name, '\0', PATH_MAX + 1); |
| 138 |
filelen = snprintf(path_name, PATH_MAX, |
| 139 |
PROC_PATH "/%s/fd/", procinfo->d_name); |
| 140 |
if (filelen <= 0 || PATH_MAX < filelen) |
| 141 |
continue; |
| 142 |
|
| 143 |
/* walk over all the files in /proc/<pid>/fd/*/ |
| 144 |
if (!(piddirs = opendir(path_name))) |
| 145 |
continue; |
| 146 |
|
| 147 |
while ((pidinfo = readdir(piddirs)) != NULL) { |
| 148 |
if (filelen + strlen(pidinfo->d_name) > PATH_MAX) |
| 149 |
continue; |
| 150 |
|
| 151 |
strlcpy(path_name + filelen, pidinfo->d_name, |
| 152 |
sizeof(path_name) - filelen); |
| 153 |
|
| 154 |
/* The file discriptor is a symbolic link to a socket or a file.*/ |
| 155 |
/* Thus read the symbolic link.*/ |
| 156 |
memset(socket_lnk, '\0', NAME_MAX + 1); |
| 157 |
readlen = readlink(path_name, socket_lnk, NAME_MAX); |
| 158 |
if (readlen < 0) |
| 159 |
continue; |
| 160 |
|
| 161 |
socket_lnk[readlen] = '\0'; |
| 162 |
|
| 163 |
/* Check if to see if the file descriptor is a socket by comparing*/ |
| 164 |
/* the start to a string. Also extract the inode number from this*/ |
| 165 |
/* symbolic link.*/ |
| 166 |
if (!strncmp(socket_lnk, SOCKET_TYPE_1, 8)) { |
| 167 |
temp_inode = strtoull(socket_lnk + 8, NULL, 0); |
| 168 |
} else if (!strncmp(socket_lnk, SOCKET_TYPE_2, 7)) { |
| 169 |
temp_inode = strtoull(socket_lnk + 7, NULL, 0); |
| 170 |
} else { |
| 171 |
temp_inode = 0; |
| 172 |
} |
| 173 |
|
| 174 |
/* Add the inode/pid combination to our hash table.*/ |
| 175 |
if (temp_inode != 0) { |
| 176 |
pid = strtoul(procinfo->d_name, NULL, 0); |
| 177 |
_set(temp_inode, pid); |
| 178 |
} |
| 179 |
} |
| 180 |
closedir(piddirs); |
| 181 |
} |
| 182 |
if (procdirs) |
| 183 |
closedir(procdirs); |
| 184 |
} |
| 185 |
|
| 186 |
pid_t |
| 187 |
netsnmp_get_pid_from_inode(ino64_t inode) |
| 188 |
{ |
| 189 |
return _get(inode); |
| 190 |
} |
| 191 |
|