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
|
/*
* Copyright (c) 2013-2014 Red Hat.
* Copyright (c) 1998 Silicon Graphics, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <limits.h>
#include "pmapi.h"
#include "impl.h"
/* possible exit states */
#define EXIT_STS_SUCCESS 0
#define EXIT_STS_USAGE 1
#define EXIT_STS_TIMEOUT 2
#define EXIT_STS_UNIXERR 3
#define EXIT_STS_PCPERR 4
static char *hostname;
static long delta = 60;
static int verbose;
static pmLongOptions longopts[] = {
PMAPI_OPTIONS_HEADER("General options"),
PMOPT_DEBUG,
{ "host", 1, 'h', "HOST", "wait for PMCD on host" },
{ "interval", 1, 't', "TIME", "maximum interval to wait for PMCD [default 60 seconds]" },
{ "verbose", 0, 'v', 0, "turn on output messages" },
PMAPI_OPTIONS_END
};
static pmOptions opts = {
.short_options = "D:h:t:v?",
.long_options = longopts,
};
void
PrintTimeout(void)
{
if (verbose) {
fprintf(stderr, "%s: Failed to connect to PMCD on host \"%s\""
" in %ld seconds\n",
pmProgname, hostname, delta);
}
}
int
main(int argc, char **argv)
{
int c;
int sts;
char env[256];
long delta_count;
while ((c = pmGetOptions(argc, argv, &opts)) != EOF) {
switch (c) {
case 'v':
verbose = 1;
break;
}
}
if (opts.optind < argc) {
pmprintf("%s: Too many arguments\n", pmProgname);
opts.errors++;
}
if (opts.interval.tv_sec != 0) {
delta = opts.interval.tv_sec;
if (delta <= 0) {
pmprintf("%s: -t argument must be at least 1 second\n",
pmProgname);
opts.errors++;
}
}
if (opts.errors) {
pmUsageMessage(&opts);
exit(EXIT_STS_USAGE);
}
if (opts.nhosts == 0)
hostname = "local:";
else
hostname = opts.hosts[0];
sts = sprintf(env, "PMCD_CONNECT_TIMEOUT=%ld", delta);
if (sts < 0) {
if (verbose) {
fprintf(stderr, "%s: Failed to create env string: %s\n",
pmProgname, osstrerror());
}
exit(EXIT_STS_UNIXERR);
}
sts = putenv(env);
if (sts != 0) {
if (verbose) {
fprintf(stderr, "%s: Failed to set PMCD_CONNECT_TIMEOUT: %s\n",
pmProgname, osstrerror());
}
exit(EXIT_STS_UNIXERR);
}
delta_count = delta;
for(;;) {
sts = pmNewContext(PM_CONTEXT_HOST, hostname);
if (sts >= 0) {
(void)pmDestroyContext(sts);
exit(EXIT_STS_SUCCESS);
}
if (sts == -ECONNREFUSED || sts == -ENOENT || sts == PM_ERR_IPC) {
static const struct timeval onesec = { 1, 0 };
delta_count--;
if (delta_count < 0) {
PrintTimeout();
exit(EXIT_STS_TIMEOUT);
}
__pmtimevalSleep(onesec);
}
else if (sts == PM_ERR_TIMEOUT) {
PrintTimeout();
exit(EXIT_STS_TIMEOUT);
}
else {
if (verbose) {
fprintf(stderr, "%s: Cannot connect to PMCD on host \"%s\": %s\n",
pmProgname, hostname, pmErrStr(sts));
}
if (sts > PM_ERR_BASE)
exit(EXIT_STS_UNIXERR);
else
exit(EXIT_STS_PCPERR);
}
}
}
|