int __pmSetProgname(const char *program) { int sts1, sts2; char *p, *suffix = NULL; WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; /* Trim command name of leading directory components and ".exe" suffix */ if (program) pmProgname = (char *)program; for (p = pmProgname; pmProgname && *p; p++) { if (*p == '\\' || *p == '/') pmProgname = p + 1; if (*p == '.') suffix = p; } if (suffix && strcmp(suffix, ".exe") == 0) *suffix = '\0'; /* Deal with all files in binary mode - no EOL futzing */ _fmode = O_BINARY; /* * If Windows networking is not setup, all networking calls fail; * this even includes gethostname(2), if you can believe that. :[ */ sts1 = WSAStartup(wVersionRequested, &wsaData); /* * Here we are emulating POSIX signals using Event objects. * For all processes we want a SIGTERM handler, which allows * us an opportunity to cleanly shutdown: atexit(1) handlers * get a look-in, IOW. Other signals (HUP/USR1) are handled * in a similar way, but only by processes that need them. */ sts2 = __pmSetSignalHandler(SIGTERM, sigterm_callback); return sts1 | sts2; }
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); }
int main(int argc, char **argv) { int sep = __pmPathSeparator(); int sts; int c; char *p; char pmnsfile[MAXPATHLEN]; char outfname[MAXPATHLEN]; struct stat sbuf; if ((p = getenv("PMNS_DEFAULT")) != NULL) { strncpy(pmnsfile, p, MAXPATHLEN); pmnsfile[MAXPATHLEN-1]= '\0'; } else { snprintf(pmnsfile, sizeof(pmnsfile), "%s%c" "pmns" "%c" "root", pmGetConfig("PCP_VAR_DIR"), sep, sep); } while ((c = pmgetopt_r(argc, argv, &opts)) != EOF) { switch (c) { case 'd': /* duplicate PMIDs are OK */ fprintf(stderr, "%s: Warning: -d deprecated, duplicate PMNS names allowed by default\n", pmProgname); break; case 'D': /* debug flag */ if ((sts = __pmParseDebug(opts.optarg)) < 0) { fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n", pmProgname, opts.optarg); opts.errors++; } else { pmDebug |= sts; } break; case 'n': /* alternative name space file */ strncpy(pmnsfile, opts.optarg, MAXPATHLEN); pmnsfile[MAXPATHLEN-1]= '\0'; break; case '?': default: opts.errors++; break; } } if (opts.errors || opts.optind > argc - 1) { pmUsageMessage(&opts); exit(1); } if ((sts = pmLoadASCIINameSpace(pmnsfile, 1)) < 0) { fprintf(stderr, "%s: Error: pmLoadASCIINameSpace(%s, 1): %s\n", pmProgname, pmnsfile, pmErrStr(sts)); exit(1); } { __pmnsTree *t; t = __pmExportPMNS(); if (t == NULL) { /* sanity check - shouldn't ever happen */ fprintf(stderr, "Exported PMNS is NULL !"); exit(1); } root = t->root; } while (opts.optind < argc) { delpmns(root, fullname = argv[opts.optind]); opts.optind++; } /* * from here on, ignore SIGHUP, SIGINT and SIGTERM to protect * the integrity of the new ouput file */ __pmSetSignalHandler(SIGHUP, SIG_IGN); __pmSetSignalHandler(SIGINT, SIG_IGN); __pmSetSignalHandler(SIGTERM, SIG_IGN); snprintf(outfname, sizeof(outfname), "%s.new", pmnsfile); if ((outf = fopen(outfname, "w")) == NULL) { fprintf(stderr, "%s: Error: cannot open PMNS file \"%s\" for writing: %s\n", pmProgname, outfname, osstrerror()); exit(1); } if (stat(pmnsfile, &sbuf) == 0) { /* * preserve the mode and ownership of any existing PMNS file */ chmod(outfname, sbuf.st_mode & ~S_IFMT); #if defined(HAVE_CHOWN) if (chown(outfname, sbuf.st_uid, sbuf.st_gid) < 0) fprintf(stderr, "%s: chown(%s, ...) failed: %s\n", pmProgname, outfname, osstrerror()); #endif } pmns_output(root, outf); fclose(outf); /* rename the PMNS */ if (rename2(outfname, pmnsfile) == -1) { fprintf(stderr, "%s: cannot rename \"%s\" to \"%s\": %s\n", pmProgname, outfname, pmnsfile, osstrerror()); /* remove the new PMNS */ unlink(outfname); exit(1); } exit(0); }
int main(int argc, char **argv) { int sts; int first = 1; int c; int j; int force = 0; int asis = 0; int dupok = 0; __pmnsNode *tmp; umask((mode_t)022); /* anything else is pretty silly */ while ((c = pmgetopt_r(argc, argv, &opts)) != EOF) { switch (c) { case 'a': asis = 1; break; case 'd': /* duplicate PMIDs are OK */ dupok = 1; break; case 'D': /* debug flag */ if ((sts = __pmParseDebug(opts.optarg)) < 0) { pmprintf("%s: unrecognized debug flag specification (%s)\n", pmProgname, opts.optarg); opts.errors++; } else { pmDebug |= sts; } break; case 'f': /* force ... unlink file first */ force = 1; break; case 'v': verbose++; break; case '?': default: opts.errors++; break; } } if (opts.errors || opts.optind > argc - 2) { pmUsageMessage(&opts); exit(1); } if (force) unlink(argv[argc-1]); if (access(argv[argc-1], F_OK) == 0) { fprintf(stderr, "%s: Error: output PMNS file \"%s\" already exists!\nYou must either remove it first, or use -f\n", pmProgname, argv[argc-1]); exit(1); } /* * from here on, ignore SIGHUP, SIGINT and SIGTERM to protect * the integrity of the new ouput file */ __pmSetSignalHandler(SIGHUP, SIG_IGN); __pmSetSignalHandler(SIGINT, SIG_IGN); __pmSetSignalHandler(SIGTERM, SIG_IGN); if ((outf = fopen(argv[argc-1], "w+")) == NULL) { fprintf(stderr, "%s: Error: cannot create output PMNS file \"%s\": %s\n", pmProgname, argv[argc-1], osstrerror()); exit(1); } if (!asis) sortargs(&argv[opts.optind], argc - opts.optind - 1); j = opts.optind; while (j < argc-1) { if (verbose) printf("%s:\n", argv[j]); if ((sts = pmLoadASCIINameSpace(argv[j], dupok)) < 0) { fprintf(stderr, "%s: Error: pmLoadNameSpace(%s): %s\n", pmProgname, argv[j], pmErrStr(sts)); exit(1); } { __pmnsTree *t; t = __pmExportPMNS(); if (t == NULL) { /* sanity check - shouldn't ever happen */ fprintf(stderr, "Exported PMNS is NULL !"); exit(1); } tmp = t->root; } if (first) { root = tmp; first = 0; } else { pmns_traverse(tmp, 0, "", merge); } j++; } pmns_output(root, outf); fclose(outf); /* * now load the merged PMNS to check for errors ... */ if ((sts = pmLoadASCIINameSpace(argv[argc-1], dupok)) < 0) { fprintf(stderr, "%s: Error: pmLoadNameSpace(%s): %s\n", pmProgname, argv[argc-1], pmErrStr(sts)); exit(1); } exit(0); }