/var/lib/pcp/testsuite/src/ipc.c is in pcp-testsuite 3.8.12ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | /*
* Copyright (c) 1995-2002 Silicon Graphics, Inc. All Rights Reserved.
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>
#define SEMAPHORES /* comment this to NOT test semaphores */
#define IPC_N 4
#define NSEMS 8
int shm_list[IPC_N], shm_n=0, shmindom;
int sem_list[IPC_N * NSEMS], sem_n=0, semindom;
int semset_list[IPC_N], semset_n = 0, semsetindom;
static char *metrics[] = {
"ipc.shm.nattch",
"ipc.shm.segsz",
#ifdef SEMAPHORES
"ipc.sem.nsems",
"ipc.sem.ncnt",
"ipc.sem.zcnt",
#endif
NULL };
static int npmids;
static pmID pmids[sizeof(metrics) / sizeof(metrics[0])];
static void
_force_err_noprofile(pmInDom indom, pmID pmid)
{
pmResult *result;
int sts;
pmAddProfile(indom, 0, NULL);
sts = pmFetch(1, &pmid, &result);
fprintf(stderr, "\n\ndeliberate error check (no explicit profile) : %s\n", pmErrStr(sts));
__pmDumpResult(stderr, result);
pmFreeResult(result);
}
static void
_force_err_unknown_inst(pmInDom indom, pmID pmid)
{
pmResult *result;
int sts;
int i;
pmDelProfile(indom, 0, NULL);
i = -3;
pmAddProfile(indom, 1, &i);
sts = pmFetch(1, &pmid, &result);
fprintf(stderr, "\n\ndeliberate error check (1 unknown instance) : %s\n", pmErrStr(sts));
__pmDumpResult(stderr, result);
pmFreeResult(result);
}
static void
_force_err_unknown_and_known_inst(pmInDom indom, int inst, pmID pmid)
{
pmResult *result;
int sts;
int i;
pmDelProfile(indom, 0, NULL);
i = -3; pmAddProfile(indom, 1, &i);
pmAddProfile(indom, 1, &inst);
sts = pmFetch(1, &pmid, &result);
fprintf(stderr, "\n\ndeliberate error check (1 unknown instance + 1 known) : %s\n", pmErrStr(sts));
__pmDumpResult(stderr, result);
pmFreeResult(result);
}
static void
_force_err(pmInDom indom, int inst, pmID pmid)
{
_force_err_noprofile(indom, pmid);
_force_err_unknown_inst(indom, pmid);
_force_err_unknown_and_known_inst(indom, inst, pmid);
}
int
main(int argc, char **argv)
{
int c;
int i;
int j;
key_t key;
int sts;
char *p;
int errflag = 0;
char *host = "localhost";
char *namespace = PM_NS_DEFAULT;
int iterations = 1;
int iter;
pmResult *result;
pmDesc desc;
int id;
int e;
#ifdef PCP_DEBUG
static char *debug = "[-D N] ";
#else
static char *debug = "";
#endif
static char *usage = "[-h hostname] [-n namespace] [-i iterations]";
extern char *optarg;
extern int optind;
extern int pmDebug;
/* trim command name of leading directory components */
pmProgname = argv[0];
for (p = pmProgname; *p; p++) {
if (*p == '/')
pmProgname = p+1;
}
while ((c = getopt(argc, argv, "D:h:l:n:i:")) != EOF) {
switch (c) {
#ifdef PCP_DEBUG
case 'D': /* debug flag */
sts = __pmParseDebug(optarg);
if (sts < 0) {
fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n",
pmProgname, optarg);
errflag++;
}
else
pmDebug |= sts;
break;
#endif
case 'h': /* hostname for PMCD to contact */
host = optarg;
break;
case 'i': /* iterations */
iterations = atoi(optarg);
break;
case 'n': /* alternative name space file */
namespace = optarg;
break;
case '?':
default:
errflag++;
break;
}
}
if (errflag) {
USAGE:
fprintf(stderr, "Usage: %s %s%s\n", pmProgname, debug, usage);
exit(1);
}
if ((sts = pmLoadNameSpace(namespace)) < 0) {
printf("%s: Cannot load namespace from \"%s\": %s\n", pmProgname, namespace, pmErrStr(sts));
exit(1);
}
sts = pmNewContext(PM_CONTEXT_HOST, host);
if (sts < 0) {
printf("%s: Cannot connect to PMCD on host \"%s\": %s\n", pmProgname, host, pmErrStr(sts));
exit(1);
}
/* non-flag args are argv[optind] ... argv[argc-1] */
if (optind > argc)
goto USAGE;
memset(pmids, 0, sizeof(pmids));
for (npmids=0; metrics[npmids]; npmids++) {
if ((sts = pmLookupName(1, &metrics[npmids], &pmids[npmids])) < 0) {
fprintf(stderr, "%s: metric ``%s'' : %s\n", pmProgname, metrics[npmids], pmErrStr(sts));
exit(1);
}
fprintf(stderr, "pmid=%s <%s>\n", pmIDStr(pmids[npmids]), metrics[npmids]);
}
if ((e = pmLookupDesc(pmids[0], &desc)) < 0) {
printf("pmLookupDesc: %s\n", pmErrStr(e));
exit(1);
}
shmindom = desc.indom;
#ifdef SEMAPHORES
if ((e = pmLookupDesc(pmids[2], &desc)) < 0) {
printf("pmLookupDesc: %s\n", pmErrStr(e));
exit(1);
}
semsetindom = desc.indom;
if ((e = pmLookupDesc(pmids[3], &desc)) < 0) {
printf("pmLookupDesc: %s\n", pmErrStr(e));
exit(1);
}
semindom = desc.indom;
#endif
fprintf(stderr, "shmindom=%s", pmInDomStr(shmindom));
#ifdef SEMAPHORES
fprintf(stderr, " semsetindom=%s", pmInDomStr(semsetindom));
fprintf(stderr, " semindom=%s", pmInDomStr(semindom));
#endif
fputc('\n', stderr);
key = (key_t)0xabcd0000;
for (i=0; i < IPC_N; i++) {
if ((id = shmget(key++, 4096, IPC_CREAT|IPC_EXCL|0777)) >= 0) {
shm_list[shm_n++] = id;
fprintf(stderr, "SHMID_%d\n", id);
}
else {
perror("shmget");
goto CLEANUP;
}
#ifdef SEMAPHORES
if ((id = semget(key++, NSEMS, IPC_CREAT|IPC_EXCL|0777)) >= 0) {
semset_list[semset_n++] = id;
fprintf(stderr, "SEMSET_%d\n", id);
for (j=0; j < NSEMS; j++) {
sem_list[sem_n++] = (id << 16) | j;
fprintf(stderr, "SEMID_%d.%d ", id, j);
}
fputc('\n', stderr);
}
else {
perror("semget");
goto CLEANUP;
}
#endif
}
pmDelProfile(shmindom, 0, NULL);
pmAddProfile(shmindom, shm_n, shm_list);
#ifdef SEMAPHORES
pmDelProfile(semsetindom, 0, NULL);
pmAddProfile(semsetindom, semset_n, semset_list);
pmDelProfile(semindom, 0, NULL);
pmAddProfile(semindom, sem_n, sem_list);
#endif
fprintf(stderr, "Single Metrics ...\n");
for (j = 0; j < npmids; j++) {
sts = pmFetch(1, &pmids[j], &result);
if (sts < 0) {
fprintf(stderr, "%s: metric %s : %s\n", pmProgname, metrics[j], pmErrStr(sts));
exit(1);
}
__pmDumpResult(stderr, result);
pmFreeResult(result);
}
fprintf(stderr, "Cascading Sets of Metrics ...\n");
for (iter=0; iter < iterations; iter++) {
fprintf(stderr, "Iteration: %d\n", iter);
for (j = 0; j < npmids; j++) {
sts = pmFetch(j+1, pmids, &result);
if (sts < 0) {
fprintf(stderr, "%s: iteration %d cascade %d : %s\n",
pmProgname, iter, j, pmErrStr(sts));
exit(1);
}
__pmDumpResult(stderr, result);
pmFreeResult(result);
}
}
/* now test the err conditions */
_force_err(shmindom, shm_list[0], pmids[0]);
#ifdef SEMAPHORES
_force_err(semsetindom, semset_list[0], pmids[2]);
_force_err(semindom, sem_list[0], pmids[3]);
#endif
CLEANUP:
for (i=0; i < shm_n; i++) {
if (shm_list[i] >= 0)
if (shmctl(shm_list[i], IPC_RMID, 0) < 0)
perror("shmctl(IPC_RMID)");
}
#ifdef SEMAPHORES
for (i=0; i < sem_n; i += NSEMS) {
if (sem_list[i] >= 0)
if (semctl(sem_list[i] >> 16, 0, IPC_RMID) < 0)
perror("semctl(IPC_RMID)");
}
#endif
exit(0);
}
|