Skip to content

Commit

Permalink
Merge pull request #146 from chu11/curl_init
Browse files Browse the repository at this point in the history
redfishpower: send http request after cmd active
  • Loading branch information
mergify[bot] authored Feb 23, 2024
2 parents 226d705 + 485e80e commit 3d3abe8
Showing 1 changed file with 62 additions and 52 deletions.
114 changes: 62 additions & 52 deletions src/redfishpower/redfishpower.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,59 @@ static size_t output_cb(void *contents, size_t size, size_t nmemb, void *userp)
return realsize;
}

/* called before putting powermsg on activecmds list */
static void powermsg_init_curl(struct powermsg *pm)
{
CURLMcode mc;

if (test_mode)
return;

if ((pm->eh = curl_easy_init()) == NULL)
err_exit(false, "curl_easy_init failed");

Curl_easy_setopt((pm->eh, CURLOPT_TIMEOUT, MESSAGE_TIMEOUT));
Curl_easy_setopt((pm->eh, CURLOPT_FAILONERROR, 1));

/* for time being */
Curl_easy_setopt((pm->eh, CURLOPT_SSL_VERIFYPEER, 0L));
Curl_easy_setopt((pm->eh, CURLOPT_SSL_VERIFYHOST, 0L));

if (verbose)
Curl_easy_setopt((pm->eh, CURLOPT_VERBOSE, 1L));

if (header) {
if (!header_list) {
if (!(header_list = curl_slist_append(header_list, header)))
err_exit(false, "curl_slist_append");
}
Curl_easy_setopt((pm->eh, CURLOPT_HTTPHEADER, header_list));
}

if (userpwd) {
Curl_easy_setopt((pm->eh, CURLOPT_USERPWD, userpwd));
Curl_easy_setopt((pm->eh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC));
}

Curl_easy_setopt((pm->eh, CURLOPT_WRITEFUNCTION, output_cb));
Curl_easy_setopt((pm->eh, CURLOPT_WRITEDATA, (void *)pm));

Curl_easy_setopt((pm->eh, CURLOPT_PRIVATE, pm));

if ((mc = curl_multi_add_handle(pm->mh, pm->eh)) != CURLM_OK)
err_exit(false, "curl_multi_add_handle: %s", curl_multi_strerror(mc));

Curl_easy_setopt((pm->eh, CURLOPT_URL, pm->url));

if (pm->postdata) {
Curl_easy_setopt((pm->eh, CURLOPT_POST, 1));
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDS, pm->postdata));
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDSIZE, strlen(pm->postdata)));
}
else
Curl_easy_setopt((pm->eh, CURLOPT_HTTPGET, 1));
}

static struct powermsg *powermsg_create(CURLM *mh,
const char *hostname,
const char *cmd,
Expand All @@ -185,7 +238,6 @@ static struct powermsg *powermsg_create(CURLM *mh,
struct powermsg *pm = calloc(1, sizeof(*pm));
struct timeval now;
struct timeval waitdelay = { 0 };
CURLMcode mc;

if (!pm)
err_exit(true, "calloc");
Expand All @@ -202,44 +254,6 @@ static struct powermsg *powermsg_create(CURLM *mh,
if (postdata)
pm->postdata = xstrdup(postdata);

if (!test_mode) {
if ((pm->eh = curl_easy_init()) == NULL)
err_exit(false, "curl_easy_init failed");

Curl_easy_setopt((pm->eh, CURLOPT_TIMEOUT, MESSAGE_TIMEOUT));
Curl_easy_setopt((pm->eh, CURLOPT_FAILONERROR, 1));

/* for time being */
Curl_easy_setopt((pm->eh, CURLOPT_SSL_VERIFYPEER, 0L));
Curl_easy_setopt((pm->eh, CURLOPT_SSL_VERIFYHOST, 0L));

if (verbose)
Curl_easy_setopt((pm->eh, CURLOPT_VERBOSE, 1L));

if (header) {
if (!header_list) {
if (!(header_list = curl_slist_append(header_list, header)))
err_exit(false, "curl_slist_append");
}
Curl_easy_setopt((pm->eh, CURLOPT_HTTPHEADER, header_list));
}

if (userpwd) {
Curl_easy_setopt((pm->eh, CURLOPT_USERPWD, userpwd));
Curl_easy_setopt((pm->eh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC));
}

Curl_easy_setopt((pm->eh, CURLOPT_WRITEFUNCTION, output_cb));
Curl_easy_setopt((pm->eh, CURLOPT_WRITEDATA, (void *)pm));

Curl_easy_setopt((pm->eh, CURLOPT_PRIVATE, pm));

if ((mc = curl_multi_add_handle(pm->mh, pm->eh)) != CURLM_OK)
err_exit(false, "curl_multi_add_handle: %s", curl_multi_strerror(mc));

Curl_easy_setopt((pm->eh, CURLOPT_URL, pm->url));
}

if (start) {
pm->start.tv_sec = start->tv_sec;
pm->start.tv_usec = start->tv_usec;
Expand Down Expand Up @@ -271,7 +285,7 @@ static void powermsg_destroy(struct powermsg *pm)
if (pm->postdata)
xfree(pm->postdata);
free(pm->output);
if (!test_mode) {
if (!test_mode && pm->eh) {
CURLMcode mc;
Curl_easy_setopt((pm->eh, CURLOPT_URL, ""));
if ((mc = curl_multi_remove_handle(pm->mh, pm->eh)) != CURLM_OK)
Expand Down Expand Up @@ -324,8 +338,6 @@ static struct powermsg *stat_cmd_host(CURLM * mh, char *hostname)
NULL,
0,
STATE_SEND_POWERCMD);
if (!test_mode)
Curl_easy_setopt((pm->eh, CURLOPT_HTTPGET, 1));
return pm;
}

Expand Down Expand Up @@ -354,6 +366,7 @@ static void stat_cmd(zlistx_t *activecmds, CURLM *mh, char **av)

while ((hostname = hostlist_next(itr))) {
struct powermsg *pm = stat_cmd_host(mh, hostname);
powermsg_init_curl(pm);
if (!(pm->handle = zlistx_add_end(activecmds, pm)))
err_exit(true, "zlistx_add_end");
free(hostname);
Expand Down Expand Up @@ -438,12 +451,6 @@ struct powermsg *power_cmd_host(CURLM * mh,
NULL,
0,
STATE_SEND_POWERCMD);

if (!test_mode) {
Curl_easy_setopt((pm->eh, CURLOPT_POST, 1));
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDS, pm->postdata));
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDSIZE, strlen(pm->postdata)));
}
return pm;
}

Expand Down Expand Up @@ -482,6 +489,7 @@ static void power_cmd(zlistx_t *activecmds,

while ((hostname = hostlist_next(itr))) {
struct powermsg *pm = power_cmd_host(mh, hostname, cmd, path, postdata);
powermsg_init_curl(pm);
if (!(pm->handle = zlistx_add_end(activecmds, pm)))
err_exit(true, "zlistx_add_end");
free(hostname);
Expand Down Expand Up @@ -522,6 +530,9 @@ static void send_status_poll(zlistx_t *delayedcmds, struct powermsg *pm)
/* issue a follow on stat to wait until the on/off is complete.
* note that we set the initial start time of this new command to
* the original on/off, so we can timeout correctly
*
* in addition, the cmd is the original on/off, indicating the true
* purpose of this status query
*/
nextpm = powermsg_create(pm->mh,
pm->hostname,
Expand All @@ -531,8 +542,6 @@ static void send_status_poll(zlistx_t *delayedcmds, struct powermsg *pm)
&pm->start,
STATUS_POLLING_INTERVAL,
STATE_WAIT_UNTIL_ON_OFF);
if (!test_mode)
Curl_easy_setopt((nextpm->eh, CURLOPT_HTTPGET, 1));
if (!(nextpm->handle = zlistx_add_end(delayedcmds, nextpm)))
err_exit(true, "zlistx_add_end");
}
Expand Down Expand Up @@ -608,7 +617,7 @@ static void power_cmd_process(zlistx_t *delayedcmds, struct powermsg *pm)

static void power_cleanup(struct powermsg *pm)
{
if (!test_mode) {
if (!test_mode && pm->eh) {
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDS, ""));
Curl_easy_setopt((pm->eh, CURLOPT_POSTFIELDSIZE, 0));
}
Expand Down Expand Up @@ -801,6 +810,7 @@ static void shell(CURLM *mh)
if (timercmp(&delaypm->delaystart, &now, >))
break;
zlistx_detach_cur(delayedcmds);
powermsg_init_curl(delaypm);
if (!(delaypm->handle = zlistx_add_end(activecmds,
delaypm)))
err_exit(true, "zlistx_add_end");
Expand Down Expand Up @@ -965,9 +975,9 @@ static void shell(CURLM *mh)
else
power_cmd_process(delayedcmds, pm);
fflush(stdout);
zlistx_detach_cur(activecmds);
pm = zlistx_next(activecmds);
}
zlistx_purge(activecmds);
}
}
zlistx_destroy(&activecmds);
Expand Down

0 comments on commit 3d3abe8

Please sign in to comment.