static void _g(void) { int inst; int i; indom = pmInDom_build(FORQA, 7); for (i = 0; i < 254; i++) { pmsprintf(nbuf, sizeof(nbuf), "hashing-instance-%03d", i); inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)(0xdeaf0000+i))); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); if (i % 2 == 0) { inst = pmdaCacheStore(indom, PMDA_CACHE_HIDE, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_HIDE failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); } if (i % 7 == 0) { pmsprintf(nbuf, sizeof(nbuf), "hashing-instance-%03d", i); inst = pmdaCacheStore(indom, PMDA_CACHE_CULL, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_CULL failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); } } __pmdaCacheDump(stderr, indom, 1); _a(0, 0, 0); __pmdaCacheDump(stderr, indom, 1); }
int __pmAttrStr_r(__pmAttrKey key, const char *data, char *string, size_t size) { char name[16]; /* must be sufficient to hold any key name (above) */ int sts; if ((sts = __pmAttrKeyStr_r(key, name, sizeof(name))) <= 0) return sts; switch (key) { case PCP_ATTR_PROTOCOL: case PCP_ATTR_USERNAME: case PCP_ATTR_PASSWORD: case PCP_ATTR_METHOD: case PCP_ATTR_REALM: case PCP_ATTR_SECURE: case PCP_ATTR_USERID: case PCP_ATTR_GROUPID: case PCP_ATTR_PROCESSID: case PCP_ATTR_CONTAINER: return pmsprintf(string, size, "%s=%s", name, data ? data : ""); case PCP_ATTR_UNIXSOCK: case PCP_ATTR_LOCAL: case PCP_ATTR_COMPRESS: case PCP_ATTR_USERAUTH: case PCP_ATTR_EXCLUSIVE: /* deprecated */ return pmsprintf(string, size, "%s", name); case PCP_ATTR_NONE: default: break; } return 0; }
char * __pmLabelIdentString(int ident, int type, char *buf, size_t buflen) { char *p, id[32]; switch (type) { case PM_LABEL_DOMAIN: pmsprintf(buf, buflen, "Domain %u", ident); break; case PM_LABEL_INDOM: case PM_LABEL_INSTANCES: pmsprintf(buf, buflen, "InDom %s", pmInDomStr_r(ident, id, sizeof(id))); break; case PM_LABEL_CLUSTER: pmIDStr_r(ident, id, sizeof(id)); p = rindex(id, '.'); *p = '\0'; pmsprintf(buf, buflen, "Cluster %s", id); break; case PM_LABEL_ITEM: pmsprintf(buf, buflen, "PMID %s", pmIDStr_r(ident, id, sizeof(id))); break; case PM_LABEL_CONTEXT: pmsprintf(buf, buflen, "Context"); break; default: buf[0] = '\0'; break; } return buf; }
static int __pmSetUserGroupAttributes(const char *username, __pmHashCtl *attrs) { char name[32]; char *namep; uid_t uid; gid_t gid; if (__pmGetUserIdentity(username, &uid, &gid, PM_RECOV_ERR) == 0) { pmsprintf(name, sizeof(name), "%u", uid); name[sizeof(name)-1] = '\0'; if ((namep = strdup(name)) != NULL) __pmHashAdd(PCP_ATTR_USERID, namep, attrs); else return -ENOMEM; pmsprintf(name, sizeof(name), "%u", gid); name[sizeof(name)-1] = '\0'; if ((namep = strdup(name)) != NULL) __pmHashAdd(PCP_ATTR_GROUPID, namep, attrs); else return -ENOMEM; return 0; } pmNotifyErr(LOG_ERR, "Authenticated user %s not found\n", username); return -ESRCH; }
static int unparseHostSpec(pmHostSpec *hostp, int count, char *string, size_t size, int prefix) { int off = 0, len = size; /* offset in string and space remaining */ int i, j, sts; for (i = 0; i < count; i++) { if (i > 0) { if ((sts = pmsprintf(string + off, len, "@")) >= size) { off = -E2BIG; goto done; } len -= sts; off += sts; } if (prefix && hostp[i].nports == PM_HOST_SPEC_NPORTS_LOCAL) { if ((sts = pmsprintf(string + off, len, "local:/%s", hostp[i].name + 1)) >= size) { off = -E2BIG; goto done; } } else if (prefix && hostp[i].nports == PM_HOST_SPEC_NPORTS_UNIX) { if ((sts = pmsprintf(string + off, len, "unix:/%s", hostp[i].name + 1)) >= size) { off = -E2BIG; goto done; } } else { if ((sts = pmsprintf(string + off, len, "%s", hostp[i].name)) >= size) { off = -E2BIG; goto done; } } len -= sts; off += sts; for (j = 0; j < hostp[i].nports; j++) { if ((sts = pmsprintf(string + off, len, "%c%u", (j == 0) ? ':' : ',', hostp[i].ports[j])) >= size) { off = -E2BIG; goto done; } len -= sts; off += sts; } } done: if (pmDebugOptions.context) { fprintf(stderr, "__pmUnparseHostSpec([name=%s ports=%p nport=%d], count=%d, ...) -> ", hostp->name, hostp->ports, hostp->nports, count); if (off < 0) { char errmsg[PM_MAXERRMSGLEN]; pmErrStr_r(off, errmsg, sizeof(errmsg)); fprintf(stderr, "%s\n", errmsg); } else fprintf(stderr, "%d \"%s\"\n", off, string); } return off; }
// return a string containing hour and milliseconds char *QedApp::timeHiResString(double time) { static char s[16]; char m[8]; time_t secs = (time_t)time; struct tm t; pmsprintf(m, sizeof(m), "%.3f", time - floor(time)); pmLocaltime(&secs, &t); pmsprintf(s, sizeof(s), "%02d:%02d:%02d.%s", t.tm_hour, t.tm_min, t.tm_sec, m+2); s[strlen(s)-1] = '\0'; return s; }
int __pmUnparseHostAttrsSpec( pmHostSpec *hosts, int count, __pmHashCtl *attrs, char *string, size_t size) { __pmHashNode *node; int off = 0, len = size; /* offset in string and space remaining */ int sts, first; if ((node = __pmHashSearch(PCP_ATTR_PROTOCOL, attrs)) != NULL) { if ((sts = pmsprintf(string, len, "%s://", (char *)node->data)) >= len) return -E2BIG; len -= sts; off += sts; } else if (__pmHashSearch(PCP_ATTR_UNIXSOCK, attrs) != NULL) { if ((sts = pmsprintf(string, len, "unix:/")) >= len) return -E2BIG; len -= sts; off += sts; } else if (__pmHashSearch(PCP_ATTR_LOCAL, attrs) != NULL) { if ((sts = pmsprintf(string, len, "local:/")) >= len) return -E2BIG; len -= sts; off += sts; } if ((sts = unparseHostSpec(hosts, count, string + off, len, 0)) >= len) return sts; len -= sts; off += sts; first = 1; for (node = __pmHashWalk(attrs, PM_HASH_WALK_START); node != NULL; node = __pmHashWalk(attrs, PM_HASH_WALK_NEXT)) { if (node->key == PCP_ATTR_PROTOCOL || node->key == PCP_ATTR_UNIXSOCK || node->key == PCP_ATTR_LOCAL) continue; if ((sts = pmsprintf(string + off, len, "%c", first ? '?' : '&')) >= len) return -E2BIG; len -= sts; off += sts; first = 0; if ((sts = unparseAttribute(node, string + off, len)) >= len) return -E2BIG; len -= sts; off += sts; } return off; }
void watch(char *fname) { char cmd[MYCMDSZ]; pmsprintf(cmd, MYCMDSZ, "xterm -hold -title \"dbpmda watch %s\" -geom 80x16 -bg dodgerblue4 -e tail -f %s &", fname, fname); if (system(cmd) != 0) fprintf(stderr, "watch cmd: %s failed: %s\n", cmd, pmErrStr(-oserror())); }
static char * pdutypestr(int type) { if (type == TRACE_PDU_ACK) return "ACK"; else if (type == TRACE_PDU_DATA) return "DATA"; else { static char buf[20]; pmsprintf(buf, sizeof(buf), "TYPE-%d?", type); return buf; } }
char * strnum(int n) { char *buf; #define MYBUFSZ 13 buf = (char *)malloc(MYBUFSZ); if (buf == NULL) { fprintf(stderr, "strnum: malloc failed: %s\n", osstrerror()); exit(1); } pmsprintf(buf, MYBUFSZ, "%d", n); return buf; }
static void _e(int since) { int i; int j; int sts; int inst; indom = pmInDom_build(FORQA, 11); sts = pmdaCacheOp(indom, PMDA_CACHE_LOAD); if (sts < 0) { fprintf(stderr, "PMDA_CACHE_LOAD failed: %s\n", pmErrStr(sts)); return; } j = 1; for (i = 0; i < 10; i++) { pmsprintf(nbuf, sizeof(nbuf), "boring-instance-%03d", i); inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)(0xcaffe000+i))); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); if (i == j) { j <<= 1; inst = pmdaCacheStore(indom, PMDA_CACHE_HIDE, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_HIDE failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); } } sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); fprintf(stderr, "Before purge ...\n"); __pmdaCacheDump(stderr, indom, 0); sleep(1); sts = pmdaCachePurge(indom, since); if (sts < 0) { fprintf(stderr, "pmdaCachePurge failed: %s\n", pmErrStr(sts)); return; } fprintf(stderr, "Purged %d entries\nAfter purge ...\n", sts); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); __pmdaCacheDump(stderr, indom, 0); }
static char * serverdb(char *path, size_t size, char *db_method) { int sep = pmPathSeparator(); char *nss_method = getenv("PCP_SECURE_DB_METHOD"); if (nss_method == NULL) nss_method = db_method; /* * Fill in a buffer with the server NSS database specification. * Return a pointer to the filesystem path component - without * the <method>:-prefix - for other routines to work with. */ pmsprintf(path, size, "%s" "%c" "etc" "%c" "pki" "%c" "nssdb", nss_method, sep, sep, sep); return path + strlen(nss_method); }
static void parseError(const char *func, const char *spec, const char *point, char *msg, char **rslt) { int need; const char *p; char *q; if (rslt == NULL) return; need = 2 * (int)strlen(spec) + 1 + 6 + (int)strlen(msg) + 2; *rslt = q = (char *)parseAlloc(func, need); for (p = spec; *p != '\0'; p++) *q++ = *p; *q++ = '\n'; for (p = spec; p != point; p++) *q++ = isgraph((int)*p) ? ' ' : *p; pmsprintf(q, need - (q - *rslt), "^ -- %s\n", msg); }
static void _j(void) { int inst; int sts; int i; indom = pmInDom_build(FORQA, 10); fprintf(stderr, "\nPopulate the instance domain ...\n"); for (i = 0; i < 20; i++) { strncpy(nbuf, xxx, ncount+3); pmsprintf(nbuf, sizeof(nbuf), "%03d", ncount); ncount++; inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)(0xbeef0000+ncount))); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); } sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); __pmdaCacheDump(stderr, indom, 0); /* We've now got a cache with 20 items. Try to resize the cache. */ sts = pmdaCacheResize(indom, 1234); fprintf(stderr, "Resize (good) -> %d", sts); if (sts < 0) fprintf(stderr, ": %s", pmErrStr(sts)); fputc('\n', stderr); /* This should fail, since we've got more than 10 items in this cache. */ sts = pmdaCacheResize(indom, 10); fprintf(stderr, "Resize (bad) -> %d", sts); if (sts < 0) fprintf(stderr, ": %s", pmErrStr(sts)); fputc('\n', stderr); /* Since we've changed the max, we'll need to save again. PMDA_CACHE_SAVE won't resave the cache with just a dirty cache header, but PMDA_CACHE_SYNC will. */ sts = pmdaCacheOp(indom, PMDA_CACHE_SYNC); fprintf(stderr, "Sync -> %d\n", sts); }
char * pmErrStr_r(int code, char *buf, int buflen) { int i; #ifndef IS_MINGW static int first = 1; static char *unknown = NULL; #else static char unknown[] = "Unknown error"; #endif if (code == 0) { strncpy(buf, "No error", buflen); buf[buflen-1] = '\0'; return buf; } /* * Is the code from a library wrapped by libpcp? (e.g. NSS/SSL/SASL) * By good fortune, these libraries are using error codes that do not * overlap - by design for NSS/SSL/NSPR, and by sheer luck with SASL. */ if (code < PM_ERR_NYI) { #ifdef HAVE_SECURE_SOCKETS #define DECODE_SECURE_SOCKETS_ERROR(c) ((c) - PM_ERR_NYI) /* negative */ #define DECODE_SASL_SPECIFIC_ERROR(c) ((c) < -1000 ? 0 : (c)) int error = DECODE_SECURE_SOCKETS_ERROR(code); if (DECODE_SASL_SPECIFIC_ERROR(error)) { PM_LOCK(__pmLock_extcall); pmsprintf(buf, buflen, "Authentication - %s", sasl_errstring(error, NULL, NULL)); /* THREADSAFE */ PM_UNLOCK(__pmLock_extcall); } else strncpy(buf, PR_ErrorToString(error, PR_LANGUAGE_EN), buflen); buf[buflen-1] = '\0'; return buf; #endif } #ifndef IS_MINGW PM_LOCK(err_lock); if (first) { /* * reference message for an unrecognized error code. * For IRIX, strerror() returns NULL in this case. */ strerror_x(-1, buf, buflen); if (buf[0] != '\0') { /* * For Linux et al, strip the last word, expected to be the * error number as in ... * Unknown error -1 * or * Unknown error 4294967295 */ char *sp = strrchr(buf, ' '); char *p; if (sp != NULL) { sp++; if (*sp == '-') sp++; for (p = sp; *p != '\0'; p++) { if (!isdigit((int)*p)) break; } if (*p == '\0') { PM_FAULT_POINT("libpcp/" __FILE__ ":1", PM_FAULT_ALLOC); *sp = '\0'; if ((unknown = strdup(buf)) != NULL) unknown[sp - buf] = '\0'; } } } first = 0; } PM_UNLOCK(err_lock); if (code < 0 && code > -PM_ERR_BASE) { /* intro(2) / errno(3) errors, maybe */ strerror_x(-code, buf, buflen); if (unknown == NULL) { if (buf[0] != '\0') return buf; } else { /* The intention here is to catch variants of "Unknown * error XXX" - in this case we're going to fail the * stncmp() below, fall through and return a pcp error * message, otherwise return the system error message */ if (strncmp(buf, unknown, strlen(unknown)) != 0) return buf; } } #else /* WIN32 */ if (code > -PM_ERR_BASE || code < -PM_ERR_NYI) { const char *bp; if ((bp = wsastrerror(-code)) != NULL) { strncpy(buf, bp, buflen); buf[buflen-1] = '\0'; } else { /* No strerror_r in MinGW, so need to lock */ char *tbp; PM_LOCK(__pmLock_extcall); tbp = strerror(-code); /* THREADSAFE */ strncpy(buf, tbp, buflen); buf[buflen-1] = '\0'; PM_UNLOCK(__pmLock_extcall); } if (strncmp(buf, unknown, strlen(unknown)) != 0) return buf; } #endif for (i = 0; errtab[i].err; i++) { if (errtab[i].err == code) { strncpy(buf, errtab[i].errmess, buflen); buf[buflen-1] = '\0'; return buf; } } /* failure */ pmsprintf(buf, buflen, BADCODE, code); return buf; }
int __pmAttrKeyStr_r(__pmAttrKey key, char *string, size_t size) { switch (key) { case PCP_ATTR_PROTOCOL: return pmsprintf(string, size, "protocol"); case PCP_ATTR_COMPRESS: return pmsprintf(string, size, "compress"); case PCP_ATTR_USERAUTH: return pmsprintf(string, size, "userauth"); case PCP_ATTR_USERNAME: return pmsprintf(string, size, "username"); case PCP_ATTR_AUTHNAME: return pmsprintf(string, size, "authname"); case PCP_ATTR_PASSWORD: return pmsprintf(string, size, "password"); case PCP_ATTR_METHOD: return pmsprintf(string, size, "method"); case PCP_ATTR_REALM: return pmsprintf(string, size, "realm"); case PCP_ATTR_SECURE: return pmsprintf(string, size, "secure"); case PCP_ATTR_UNIXSOCK: return pmsprintf(string, size, "unixsock"); case PCP_ATTR_LOCAL: return pmsprintf(string, size, "local"); case PCP_ATTR_USERID: return pmsprintf(string, size, "userid"); case PCP_ATTR_GROUPID: return pmsprintf(string, size, "groupid"); case PCP_ATTR_PROCESSID: return pmsprintf(string, size, "processid"); case PCP_ATTR_CONTAINER: return pmsprintf(string, size, "container"); case PCP_ATTR_EXCLUSIVE: return pmsprintf(string, size, "exclusive"); /* deprecated */ case PCP_ATTR_NONE: default: break; } return 0; }
static void _b(void) { int i; int j; int inst; int sts; char cmd[2*MAXPATHLEN+30]; indom = pmInDom_build(FORQA, 8); pmsprintf(cmd, sizeof(cmd), "rm -f %s/config/pmda/%s", pmGetConfig("PCP_VAR_DIR"), pmInDomStr(indom)); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: %s: exit status %d\n", cmd, sts); pmsprintf(cmd, sizeof(cmd), "[ -f %s/config/pmda/%s ] || exit 0; cat %s/config/pmda/%s", pmGetConfig("PCP_VAR_DIR"), pmInDomStr(indom), pmGetConfig("PCP_VAR_DIR"), pmInDomStr(indom)); fprintf(stderr, "\nPopulate the instance domain ...\n"); j = 1; for (i = 0; i < 20; i++) { strncpy(nbuf, xxx, ncount+3); pmsprintf(nbuf, sizeof(nbuf), "%03d", ncount); ncount++; inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)(0xbeef0000+ncount))); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); else if (i > 14) { sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); } if (i == j) { j <<= 1; inst = pmdaCacheStore(indom, PMDA_CACHE_HIDE, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_HIDE failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); } if (i == 6 || i == 13) { fprintf(stderr, "Save ...\n"); fprintf(stderr, "Before:\n"); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:1: %s: exit status %d\n", cmd, sts); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "return -> %d", sts); if (sts < 0) fprintf(stderr, ": %s", pmErrStr(sts)); fputc('\n', stderr); fprintf(stderr, "After:\n"); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:2: %s: exit status %d\n", cmd, sts); } if (i == 14) { fprintf(stderr, "Start save after changes ...\n"); } if (i > 14) { sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:3: %s: exit status %d\n", cmd, sts); } } __pmdaCacheDump(stderr, indom, 0); strncpy(nbuf, xxx, 11+3); pmsprintf(nbuf, sizeof(nbuf), "%03d", 11); fprintf(stderr, "\nHide %s ...\n", nbuf); inst = pmdaCacheStore(indom, PMDA_CACHE_HIDE, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_HIDE failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:4: %s: exit status %d\n", cmd, sts); fprintf(stderr, "Add %s ...\n", nbuf); inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)0xdeadbeef)); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:5: %s: exit status %d\n", cmd, sts); fprintf(stderr, "Cull %s ...\n", nbuf); inst = pmdaCacheStore(indom, PMDA_CACHE_CULL, nbuf, NULL); if (inst < 0) fprintf(stderr, "PMDA_CACHE_CULL failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:6: %s: exit status %d\n", cmd, sts); fprintf(stderr, "Add %s ...\n", nbuf); inst = pmdaCacheStore(indom, PMDA_CACHE_ADD, nbuf, (void *)((__psint_t)0xdeadbeef)); if (inst < 0) fprintf(stderr, "PMDA_CACHE_ADD failed for \"%s\": %s\n", nbuf, pmErrStr(inst)); sts = pmdaCacheOp(indom, PMDA_CACHE_SAVE); fprintf(stderr, "Save -> %d\n", sts); sts = system(cmd); if (sts != 0) fprintf(stderr, "Warning: _b:7: %s: exit status %d\n", cmd, sts); }