This file is indexed.

/var/lib/pcp/testsuite/src/multictx.c is in pcp-testsuite 4.0.1-1.

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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * Copyright (c) 1995-2001 Silicon Graphics, Inc.  All Rights Reserved.
 * Copyright (c) 2016 Ken McDonell.  All Rights Reserved.
 *
 * Exercise multiple contexts ... aim to reproduce
 * http://oss.sgi.com/bugzilla/show_bug.cgi?id=1158
 *
 * One metric name per argument.
 *
 * PMAPI loop repeated for each metric.
 * - pmLookupName(all names)
 * - pmLookupDesc for one metric
 * - pmNameID for one metric
 * - pmGetInDom for one metric
 * - pmNameInDom for one metric
 * - pmLookupInDom for one metric
 * - pmLookupText for one metric
 * - pmTraversePMNS for parent of one metric
 * - pmGetChildrenStatus for parent of one metric
 * - pmFetch for one metric
 *
 * Each context starts with a different metric, and at a different point
 * in the loop.
 */

#include <pcp/pmapi.h>
#include "libpcp.h"
#include <string.h>

typedef struct {
    char	*name;
    pmID	pmid;
    pmDesc	desc;
    int		ctx;
    int		ctx_bad;	/* after IPC or TIMEOUT & before Reconnect */
    int		op;		/* next operation */
    int		midx;		/* next metric */
    char	*parent;	/* parent name in PMNS */
    int		inst;		/* middle instance, assumes indom is not dynamic */
    char	*instname;	/* middle instance name */
} ctl_t;

#define OP_LOOKUPNAME	0
#define OP_GETDESC	1
#define OP_NAMEID	2
#define OP_GETINDOM	3
#define OP_NAMEINDOM	4
#define OP_LOOKUPINDOM	5
#define OP_LOOKUPTEXT	6
#define OP_TRAVERSE	7
#define OP_GETCHILDREN	8
#define OP_FETCH	9
#define NUMOP		10

int	nummetrics;

static void
dometric(const char *name)
{
    nummetrics++;
}

int
main(int argc, char **argv)
{
    int		c;
    int		i;
    int		sts;
    int		errflag = 0;
    int		type = 0;
    char	*host = NULL;			/* pander to gcc */
    char	local[MAXHOSTNAMELEN];
    int		samples = 10;
    char	*endnum;
    ctl_t	*ctl;
    int		numctl;
    pmID	pmid;
    char	*name;
    pmDesc	desc;
    pmResult	*rp;
    int		iter;
    char	**offspring;
    int		*status;
    char	*p;
    int		*instlist;
    char	**namelist;

    /* trim cmd name of leading directory components */
    pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "a:D:h:Ls:?")) != EOF) {
	switch (c) {

	case 'a':	/* archive name */
	    if (type != 0) {
#ifdef BUILD_STANDALONE
		fprintf(stderr, "%s: at most one of -a, -h and -L allowed\n", pmGetProgname());
#else
		fprintf(stderr, "%s: at most one of -a and -h allowed\n", pmGetProgname());
#endif
		errflag++;
	    }
	    type = PM_CONTEXT_ARCHIVE;
	    host = optarg;
	    break;

	case 'D':	/* debug options */
	    sts = pmSetDebug(optarg);
	    if (sts < 0) {
		fprintf(stderr, "%s: unrecognized debug options specification (%s)\n",
		    pmGetProgname(), optarg);
		errflag++;
	    }
	    break;

	case 'h':	/* contact PMCD on this hostname */
	    if (type != 0) {
#ifdef BUILD_STANDALONE
		fprintf(stderr, "%s: at most one of -a, -h and -L allowed\n", pmGetProgname());
#else
		fprintf(stderr, "%s: at most one of -a and -h allowed\n", pmGetProgname());
#endif
		errflag++;
	    }
	    host = optarg;
	    type = PM_CONTEXT_HOST;
	    break;

#ifdef BUILD_STANDALONE
	case 'L':	/* LOCAL, no PMCD */
	    if (type != 0) {
		fprintf(stderr, "%s: at most one of -a, -h, -L and -U allowed\n", pmGetProgname());
		errflag++;
	    }
	    host = NULL;
	    type = PM_CONTEXT_LOCAL;
	    putenv("PMDA_LOCAL_PROC=");		/* if proc PMDA needed */
	    putenv("PMDA_LOCAL_SAMPLE=");	/* if sampledso PMDA needed */
	    break;
#endif

	case 's':	/* sample count */
	    samples = (int)strtol(optarg, &endnum, 10);
	    if (*endnum != '\0' || samples < 0) {
		fprintf(stderr, "%s: -s requires numeric argument\n", pmGetProgname());
		errflag++;
	    }
	    break;

	case '?':
	default:
	    errflag++;
	    break;
	}
    }

    numctl = argc - optind;
    if (numctl < 1)
	errflag++;

    if (errflag) {
	fprintf(stderr, 
"Usage: %s [options] metric [...]\n\
\n\
Options:\n\
  -a archive     metrics source is a PCP log archive\n\
  -h host        metrics source is PMCD on host\n"
#ifdef BUILD_STANDALONE
"  -L             use local context instead of PMCD\n"
#endif
"  -s samples     terminate after this many samples [default 10]\n",
                pmGetProgname());
        exit(1);
    }

    if (type == 0) {
	type = PM_CONTEXT_HOST;
	(void)gethostname(local, MAXHOSTNAMELEN);
	local[MAXHOSTNAMELEN-1] = '\0';
	host = local;
    }

    ctl = (ctl_t *)malloc(numctl * sizeof(ctl[0]));
    if (ctl == NULL) {
	pmNoMem("ctl", numctl*sizeof(ctl[0]), PM_FATAL_ERR);
	/* NOTREACHED */
    }

    i = 0;
    /* non-flag args are argv[optind] ... argv[argc-1] */
    while (optind < argc) {

	if ((sts = pmNewContext(type, host)) < 0) {
	    if (type == PM_CONTEXT_HOST)
		fprintf(stderr, "%s: Cannot connect to PMCD on host \"%s\": %s\n",
		    pmGetProgname(), host, pmErrStr(sts));
	    else
		fprintf(stderr, "%s: Cannot open archive \"%s\": %s\n",
		    pmGetProgname(), host, pmErrStr(sts));
	    exit(1);
	}
	ctl[i].ctx = sts;
	ctl[i].ctx_bad = 0;
	ctl[i].name = argv[optind];
	sts = pmLookupName(1, &ctl[i].name, &ctl[i].pmid);
	if (sts < 0) {
	    fprintf(stderr, "pmLookupName(%s): %s\n", ctl[i].name, pmErrStr(sts));
	    exit(1);
	}
	sts = pmLookupDesc(ctl[i].pmid, &ctl[i].desc);
	if (sts < 0) {
	    fprintf(stderr, "pmLookupDesc(%s): %s\n", ctl[i].name, pmErrStr(sts));
	    exit(1);
	}
	p = strrchr(ctl[i].name, '.');
	if (p == NULL) {
	    ctl[i].parent = "";
	}
	else {
	    c = *p;
	    *p = '\0';
	    ctl[i].parent = strdup(ctl[i].name);
	    *p = c;
	}
	sts = pmGetInDom(ctl[i].desc.indom, &instlist, &namelist);
	if (sts <= 0) {
	    /* errors and zero sized instance domains are the same */
	    ctl[i].inst = PM_IN_NULL;
	    ctl[i].instname = "";
	}
	else {
	    int		j = sts / 2;
	    ctl[i].inst = instlist[j];
	    ctl[i].instname = strdup(namelist[j]);
	    free(instlist);
	    free(namelist);
	}
	ctl[i].op = i;
	ctl[i].midx = i;
	i++;
	optind++;
    }

    for (iter = 0; iter < samples; iter++) {
	/* work forwards through the control structures */
	i = (iter % numctl);

	if (ctl[i].ctx_bad) {
	    sts = pmReconnectContext(ctl[i].ctx);
	    if (sts >= 0) {
		fprintf(stderr, "iter %d: pmReconnectContext(%d) -> %d OK\n", iter, ctl[i].ctx, sts);
		ctl[i].ctx = sts;
		ctl[i].ctx_bad = 0;
	    }
	    else {
		fprintf(stderr, "iter %d: pmReconnectContext(%d) failed: %s\n", iter, ctl[i].ctx, pmErrStr(sts));
		continue;
	    }
	}

	sts = pmUseContext(ctl[i].ctx);
	if (sts < 0) {
	    fprintf(stderr, "iter %d: pmUseContext(%d): %s\n", iter, ctl[i].ctx, pmErrStr(sts));
	    exit(1);
	}

	switch (ctl[i].op) {

	case OP_LOOKUPNAME:
	    fprintf(stderr, "iter %d: ctx %d: pmLookupName metric: %s\n", iter, ctl[i].ctx, ctl[i].name);
	    sts = pmLookupName(1, &ctl[i].name, &pmid);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (pmid != ctl[i].pmid) {
		    fprintf(stderr, "Error: returned name %s", pmIDStr(pmid));
		    fprintf(stderr, " expecting %s\n", pmIDStr(ctl[i].pmid));
		}
	    }
	    break;

	case OP_GETDESC:
	    fprintf(stderr, "iter %d: ctx %d: pmLookupDesc metric: %s\n", iter, ctl[i].ctx, ctl[i].name);
	    sts = pmLookupDesc(ctl[i].pmid, &desc);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (desc.pmid != ctl[i].pmid) {
		    fprintf(stderr, "Error: returned pmid %s", pmIDStr(desc.pmid));
		    fprintf(stderr, " expecting %s\n", pmIDStr(ctl[i].pmid));
		}
		if (desc.pmid != ctl[i].desc.pmid) {
		    fprintf(stderr, "Error: returned desc.pmid %s", pmIDStr(desc.pmid));
		    fprintf(stderr, " expecting %s\n", pmIDStr(ctl[i].desc.pmid));
		}
		if (desc.type != ctl[i].desc.type) {
		    fprintf(stderr, "Error: returned desc.type %s", pmTypeStr(desc.type));
		    fprintf(stderr, " expecting %s\n", pmTypeStr(ctl[i].desc.type));
		}
		if (desc.indom != ctl[i].desc.indom) {
		    fprintf(stderr, "Error: returned desc.indom %s", pmInDomStr(desc.indom));
		    fprintf(stderr, " expecting %s\n", pmInDomStr(ctl[i].desc.indom));
		}
		if (desc.sem != ctl[i].desc.sem) {
		    fprintf(stderr, "Error: returned desc.sem %s", pmSemStr(desc.sem));
		    fprintf(stderr, " expecting %s\n", pmSemStr(ctl[i].desc.sem));
		}
		if (desc.units.scaleCount != ctl[i].desc.units.scaleCount ||
		    desc.units.scaleTime != ctl[i].desc.units.scaleTime ||
		    desc.units.scaleSpace != ctl[i].desc.units.scaleSpace ||
		    desc.units.dimCount != ctl[i].desc.units.dimCount ||
		    desc.units.dimTime != ctl[i].desc.units.dimTime ||
		    desc.units.dimSpace != ctl[i].desc.units.dimSpace) {
		    fprintf(stderr, "Error: returned desc.units %s", pmUnitsStr(&desc.units));
		    fprintf(stderr, " expecting %s\n", pmUnitsStr(&ctl[i].desc.units));
		}
	    }
	    break;

	case OP_NAMEID:
	    fprintf(stderr, "iter %d: ctx %d: pmNameID metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmNameID(ctl[i].pmid, &name);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (strcmp(ctl[i].name, name) != 0) {
		    fprintf(stderr, "Error: returned name %s", name);
		    fprintf(stderr, " expecting %s\n", ctl[i].name);
		}
		free(name);
	    }
	    break;

	case OP_GETINDOM:
	    fprintf(stderr, "iter %d: ctx %d: pmGetInDom metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmGetInDom(ctl[i].desc.indom, &instlist, &namelist);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (ctl[i].inst != PM_IN_NULL) {
		    int	j = sts / 2;
		    if (instlist[j] != ctl[i].inst) {
			fprintf(stderr, "Error: returned inst[%d] %d", j, instlist[j]);
			fprintf(stderr, " expecting %d\n", ctl[i].inst);
		    }
		    if (strcmp(namelist[j], ctl[i].instname) != 0) {
			fprintf(stderr, "Error: returned instname[%d] \"%s\"", j, namelist[j]);
			fprintf(stderr, " expecting \"%s\"\n", ctl[i].instname);
		    }
		}
		free(instlist);
		free(namelist);
	    }
	    break;

	case OP_NAMEINDOM:
	    fprintf(stderr, "iter %d: ctx %d: pmNameInDom metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmNameInDom(ctl[i].desc.indom, ctl[i].inst, &p);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (ctl[i].inst != PM_IN_NULL) {
		    if (strcmp(p, ctl[i].instname) != 0) {
			fprintf(stderr, "Error: returned instname \"%s\"", p);
			fprintf(stderr, " expecting \"%s\"\n", ctl[i].instname);
		    }
		}
		free(p);
	    }
	    break;

	case OP_LOOKUPINDOM:
	    fprintf(stderr, "iter %d: ctx %d: pmLookupInDom metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmLookupInDom(ctl[i].desc.indom, ctl[i].instname);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (ctl[i].inst != PM_IN_NULL) {
		    if (sts != ctl[i].inst) {
			fprintf(stderr, "Error: returned inst %d", sts);
			fprintf(stderr, " expecting %d\n", ctl[i].inst);
		    }
		}
	    }
	    break;

	case OP_LOOKUPTEXT:
	    fprintf(stderr, "iter %d: ctx %d: pmLookupText metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmLookupText(ctl[i].pmid, PM_TEXT_ONELINE, &p);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		free(p);
	    }
	    break;

	case OP_TRAVERSE:
	    fprintf(stderr, "iter %d: ctx %d: pmTraversePMNS metric: %s\n", iter, ctl[i].ctx, ctl[i].parent);
	    nummetrics = 0;
	    sts = pmTraversePMNS(ctl[i].parent, dometric);
	    if (sts <= 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (nummetrics == 0) {
		    fprintf(stderr, "Error: returned %d metrics", nummetrics);
		    fprintf(stderr, " expecting something > 0\n");
		}
	    }
	    break;

	case OP_GETCHILDREN:
	    fprintf(stderr, "iter %d: ctx %d: pmGetChildrenStatus metric: %s\n", iter, ctl[i].ctx, ctl[i].parent);
	    sts = pmGetChildrenStatus(ctl[i].parent, &offspring, &status);
	    if (sts <= 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		/* anything > 0 is OK */
		free(offspring);
		free(status);
	    }
	    break;

	case OP_FETCH:
	    fprintf(stderr, "iter %d: ctx %d: pmFetch metric: %s (aka %s)\n", iter, ctl[i].ctx, pmIDStr(ctl[i].pmid), ctl[i].name);
	    sts = pmFetch(1, &ctl[i].pmid, &rp);
	    if (sts < 0) {
		fprintf(stderr, "Error: %s\n", pmErrStr(sts));
		if (sts == PM_ERR_IPC || sts == PM_ERR_TIMEOUT)
		    ctl[i].ctx_bad = 1;
	    }
	    else {
		if (rp->numpmid != 1) {
		    fprintf(stderr, "Error: returned numpmid %d", rp->numpmid);
		    fprintf(stderr, " expecting %d\n", 1);
		}
		if (rp->vset[0]->pmid != ctl[i].pmid) {
		    fprintf(stderr, "Error: returned vset[0]->pmid %s", pmIDStr(rp->vset[0]->pmid));
		    fprintf(stderr, " expecting %s\n", pmIDStr(ctl[i].pmid));
		}
		pmFreeResult(rp);
	    }
	    break;

	}

	/*
	 * work forwards (even context) or backwards (odd context)
	 * through the PMAPI operations
	 */
	if (i % 2 == 0) {
	    ctl[i].op++;
	    if (ctl[i].op == NUMOP)
		ctl[i].op = 0;
	}
	else {
	    ctl[i].op--;
	    if (ctl[i].op < 0)
		ctl[i].op = NUMOP - 1;
	}
	/* work backwards through  the metrics */
	ctl[i].midx--;
	if (ctl[i].midx < 0)
	    ctl[i].midx = numctl - 1;
    }

    return 0;
}