int main(int argc, char **argv) { int c; int sts; char *endnum; iflag = isatty(0); while ((c = pmgetopt_r(argc, argv, &opts)) != EOF) { switch (c) { case 'D': /* debug flag */ sts = __pmParseDebug(opts.optarg); if (sts < 0) { fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n", pmProgname, opts.optarg); opts.errors++; } else pmDebug |= sts; break; case 'e': /* echo input */ eflag++; break; case 'f': /* skip .dbpmdarc processing */ fflag++; break; case 'i': /* be interactive */ iflag = 1; break; case 'n': /* alternative name space file */ pmnsfile = opts.optarg; break; case 'q': sts = (int)strtol(opts.optarg, &endnum, 10); if (*endnum != '\0' || sts <= 0.0) { pmprintf("%s: -q requires a positive numeric argument\n", pmProgname); opts.errors++; } else { _creds_timeout = sts; } break; case 'U': /* run under alternate user account */ __pmSetProcessIdentity(opts.optarg); break; default: case '?': opts.errors++; break; } } if ((c = argc - opts.optind) > 0) { if (c > 1) opts.errors++; else { /* pid was specified */ if (primary) { pmprintf("%s: you may not specify both -P and a pid\n", pmProgname); opts.errors++; } else { pid = (int)strtol(argv[opts.optind], &endnum, 10); if (*endnum != '\0') { pmprintf("%s: pid must be a numeric process id\n", pmProgname); opts.errors++; } } } } if (opts.errors) { pmUsageMessage(&opts); exit(1); } if (pmnsfile == PM_NS_DEFAULT) { if ((sts = pmLoadNameSpace(pmnsfile)) < 0) { fprintf(stderr, "%s: Cannot load default namespace: %s\n", pmProgname, pmErrStr(sts)); exit(1); } } else { if ((sts = pmLoadASCIINameSpace(pmnsfile, 1)) < 0) { fprintf(stderr, "%s: Cannot load namespace from \"%s\": %s\n", pmProgname, pmnsfile, pmErrStr(sts)); exit(1); } } /* initialize the "fake context" ... */ setup_context(); setlinebuf(stdout); setlinebuf(stderr); #ifdef HAVE_ATEXIT atexit(cleanup); #endif for ( ; ; ) { initmetriclist(); yyparse(); if (yywrap()) { if (iflag) putchar('\n'); break; } __pmSetInternalState(PM_STATE_PMCS); switch (stmt_type) { case OPEN: profile_changed = 1; break; case CLOSE: switch (connmode) { case CONN_DSO: closedso(); break; case CONN_DAEMON: closepmda(); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } connmode = NO_CONN; break; case DESC: switch (connmode) { case CONN_DSO: dodso(PDU_DESC_REQ); break; case CONN_DAEMON: dopmda(PDU_DESC_REQ); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case FETCH: switch (connmode) { case CONN_DSO: dodso(PDU_FETCH); break; case CONN_DAEMON: dopmda(PDU_FETCH); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case INSTANCE: switch (connmode) { case CONN_DSO: dodso(PDU_INSTANCE_REQ); break; case CONN_DAEMON: dopmda(PDU_INSTANCE_REQ); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case STORE: switch (connmode) { case CONN_DSO: dodso(PDU_RESULT); break; case CONN_DAEMON: dopmda(PDU_RESULT); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case HELP: dohelp(param.number, param.pmid); break; case WATCH: break; case DBG: pmDebug = param.number; break; case QUIT: goto done; case STATUS: dostatus(); break; case INFO: switch (connmode) { case CONN_DSO: dodso(PDU_TEXT_REQ); break; case CONN_DAEMON: dopmda(PDU_TEXT_REQ); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case NAMESPACE: if (cmd_namespace != NULL) free(cmd_namespace); cmd_namespace = strdup(param.name); if (cmd_namespace == NULL) { fprintf(stderr, "%s: No memory for new namespace\n", pmProgname); exit(1); } pmUnloadNameSpace(); strcpy(cmd_namespace, param.name); if ((sts = pmLoadASCIINameSpace(cmd_namespace, 1)) < 0) { fprintf(stderr, "%s: Cannot load namespace from \"%s\": %s\n", pmProgname, cmd_namespace, pmErrStr(sts)); pmUnloadNameSpace(); if (pmnsfile == PM_NS_DEFAULT) { fprintf(stderr, "%s: Reload default namespace\n", pmProgname); if ((sts = pmLoadNameSpace(pmnsfile)) < 0) { fprintf(stderr, "%s: Cannot load default namespace: %s\n", pmProgname, pmErrStr(sts)); exit(1); } } else { fprintf(stderr, "%s: Reload namespace from \"%s\"\n", pmProgname, pmnsfile); if ((sts = pmLoadASCIINameSpace(pmnsfile, 1)) < 0) { fprintf(stderr, "%s: Cannot load namespace from \"%s\"" ": %s\n", pmProgname, pmnsfile, pmErrStr(sts)); exit(1); } } } break; case EOL: break; case PMNS_NAME: switch (connmode) { case CONN_DSO: dodso(PDU_PMNS_IDS); break; case CONN_DAEMON: dopmda(PDU_PMNS_IDS); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case PMNS_PMID: switch (connmode) { case CONN_DSO: dodso(PDU_PMNS_NAMES); break; case CONN_DAEMON: dopmda(PDU_PMNS_NAMES); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case PMNS_CHILDREN: switch (connmode) { case CONN_DSO: dodso(PDU_PMNS_CHILD); break; case CONN_DAEMON: dopmda(PDU_PMNS_CHILD); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case PMNS_TRAVERSE: switch (connmode) { case CONN_DSO: dodso(PDU_PMNS_TRAVERSE); break; case CONN_DAEMON: dopmda(PDU_PMNS_TRAVERSE); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; case ATTR: switch (connmode) { case CONN_DSO: dodso(PDU_AUTH); break; case CONN_DAEMON: dopmda(PDU_AUTH); break; case NO_CONN: yywarn("No PMDA currently opened"); break; } break; default: printf("Unexpected result (%d) from parser?\n", stmt_type); break; } __pmSetInternalState(PM_STATE_APPL); } done: cleanup(); exit(0); }
int main(int argc, char *argv[]) { int sts; int nport = 0; char *envstr; umask(022); __pmGetUsername(&username); __pmSetInternalState(PM_STATE_PMCS); __pmServerSetFeature(PM_SERVER_FEATURE_DISCOVERY); if ((envstr = getenv("PMPROXY_PORT")) != NULL) nport = __pmServerAddPorts(envstr); ParseOptions(argc, argv, &nport); if (nport == 0) __pmServerAddPorts(TO_STRING(PROXY_PORT)); GetProxyHostname(); __pmServerSetServiceSpec(PM_SERVER_PROXY_SPEC); if (run_daemon) { fflush(stderr); StartDaemon(argc, argv); __pmServerCreatePIDFile(PM_SERVER_PROXY_SPEC, 0); } __pmSetSignalHandler(SIGHUP, SIG_IGN); __pmSetSignalHandler(SIGINT, SigIntProc); __pmSetSignalHandler(SIGTERM, SigIntProc); __pmSetSignalHandler(SIGBUS, SigBad); __pmSetSignalHandler(SIGSEGV, SigBad); /* Open request ports for client connections */ if ((sts = __pmServerOpenRequestPorts(&sockFds, MAXPENDING)) < 0) DontStart(); maxReqPortFd = maxSockFd = sts; __pmOpenLog(pmProgname, logfile, stderr, &sts); /* close old stdout, and force stdout into same stream as stderr */ fflush(stdout); close(fileno(stdout)); if (dup(fileno(stderr)) == -1) { fprintf(stderr, "Warning: dup() failed: %s\n", pmErrStr(-oserror())); } fprintf(stderr, "pmproxy: PID = %" FMT_PID, getpid()); fprintf(stderr, ", PDU version = %u\n", PDU_VERSION); __pmServerDumpRequestPorts(stderr); fflush(stderr); /* lose root privileges if we have them */ __pmSetProcessIdentity(username); if (__pmSecureServerSetup(certdb, dbpassfile) < 0) DontStart(); /* all the work is done here */ ClientLoop(); Shutdown(); exit(0); }
int main(int argc, char *argv[]) { int sts; int nport = 0; char *envstr; #ifdef HAVE_SA_SIGINFO static struct sigaction act; #endif umask(022); __pmProcessDataSize(NULL); __pmGetUsername(&username); __pmSetInternalState(PM_STATE_PMCS); __pmServerSetFeature(PM_SERVER_FEATURE_DISCOVERY); __pmServerSetFeature(PM_SERVER_FEATURE_CONTAINERS); if ((envstr = getenv("PMCD_PORT")) != NULL) nport = __pmServerAddPorts(envstr); ParseOptions(argc, argv, &nport); if (nport == 0) __pmServerAddPorts(TO_STRING(SERVER_PORT)); /* Set the local socket path. A message will be generated into the log * if this fails, but it is not fatal, since other connection options * may exist. */ __pmServerSetLocalSocket(sockpath); /* Set the service spec. This will cause our service to be advertised on * the network if that is supported. */ __pmServerSetServiceSpec(PM_SERVER_SERVICE_SPEC); if (run_daemon) { fflush(stderr); StartDaemon(argc, argv); } #ifdef HAVE_SA_SIGINFO act.sa_sigaction = SigIntProc; act.sa_flags = SA_SIGINFO; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); #else __pmSetSignalHandler(SIGINT, SigIntProc); __pmSetSignalHandler(SIGTERM, SigIntProc); #endif __pmSetSignalHandler(SIGHUP, SigHupProc); __pmSetSignalHandler(SIGBUS, SigBad); __pmSetSignalHandler(SIGSEGV, SigBad); if ((sts = __pmServerOpenRequestPorts(&clientFds, MAXPENDING)) < 0) DontStart(); maxReqPortFd = maxClientFd = sts; __pmOpenLog(pmProgname, logfile, stderr, &sts); /* close old stdout, and force stdout into same stream as stderr */ fflush(stdout); close(fileno(stdout)); sts = dup(fileno(stderr)); /* if this fails beware of the sky falling in */ assert(sts >= 0); sts = pmLoadASCIINameSpace(pmnsfile, dupok); if (sts < 0) { fprintf(stderr, "Error: pmLoadASCIINameSpace(%s, %d): %s\n", (pmnsfile == PM_NS_DEFAULT) ? "DEFAULT" : pmnsfile, dupok, pmErrStr(sts)); DontStart(); } if (ParseInitAgents(configFileName) < 0) { /* error already reported in ParseInitAgents() */ DontStart(); } if (nAgents <= 0) { fprintf(stderr, "Error: No PMDAs found in the configuration file \"%s\"\n", configFileName); DontStart(); } if (run_daemon) { if (__pmServerCreatePIDFile(PM_SERVER_SERVICE_SPEC, PM_FATAL_ERR) < 0) DontStart(); if (__pmSetProcessIdentity(username) < 0) DontStart(); } if (__pmSecureServerSetup(certdb, dbpassfile) < 0) DontStart(); PrintAgentInfo(stderr); __pmAccDumpLists(stderr); fprintf(stderr, "\npmcd: PID = %" FMT_PID, getpid()); fprintf(stderr, ", PDU version = %u\n", PDU_VERSION); __pmServerDumpRequestPorts(stderr); fflush(stderr); /* all the work is done here */ ClientLoop(); Shutdown(); exit(0); }