aboutsummaryrefslogtreecommitdiffstats
path: root/codi/codi_launcher.c
blob: 10877615cc8ac7d3b911c63c6b365cea5cd9241b (plain)
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) 2016 Intel Corporation
 *
 * Author: Todor Minchev <todor.minchev@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, or (at your option) any later version, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope 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 "globals.h"
#include <jansson.h>
#include <string.h>
#include <curl/curl.h>
#include <ctype.h>
#include "codi_launcher.h"

/* execute the request and return an array of json objects */
json_t *request_json(char *request_method, char *url, char *api, char *data)
{
  char *in = 0, *j_start;
  json_t *root;
  json_error_t err;

  in = curl_request(request_method, url, api, data);

  /* skip the header and move to the json array */
  j_start = strchr(in, '[');
  root = json_loads(j_start, 0, &err);
  free(in);
  return root;
}

/* execute the request and return the raw server reply */
char *curl_request(char *http_method, char *url, char *api,  char *data)
{

  CURL *curl = NULL;
  CURLcode res;
  struct curl_slist *api_hdr = NULL;
  curl_mem_chunk_t srv_reply ;
  char *request_str ;

  srv_reply.mem = calloc(1, 1) ;

  if (srv_reply.mem == NULL)
    ERR("ERROR: Unable to allocate memory");

  srv_reply.size = 0 ;
  curl = curl_easy_init();

  if (curl) {
    /* check if url is a unix socket*/
    if (url[0] == '/') {
      /* map the unix socket as a connection endpoint */
      curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, url);
      asprintf(&request_str, "%s%s", "http:", api);
    } else {
        asprintf(&request_str, "%s%s%s",  "http://", url, api);
    }

    curl_easy_setopt(curl, CURLOPT_URL, request_str);

    if (!strcmp(http_method, POST_REQUEST)) {
      curl_easy_setopt(curl, CURLOPT_POST, 1L);
      api_hdr = curl_slist_append(api_hdr, "Content-Type: application/json");
      api_hdr = curl_slist_append(api_hdr, "Expect:");
      curl_easy_setopt(curl, CURLOPT_HTTPHEADER, api_hdr);

      if (data == NULL)
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
      else
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    }

    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &srv_reply);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, process_srv_reply);

#ifdef DBG
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
#endif

    res = curl_easy_perform(curl);

    if (res != CURLE_OK)
      INFO("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    if (!strcmp(http_method, POST_REQUEST))
      curl_slist_free_all(api_hdr);

    free(request_str);
    curl_easy_cleanup(curl);
  }

  return srv_reply.mem;
}

size_t process_srv_reply(void *ptr, size_t size, size_t nmemb, void *reply_p)
{
  size_t chunk_size = size * nmemb;

  curl_mem_chunk_t *mem_chunk = (curl_mem_chunk_t *)reply_p;

  /* append chunk_size memory block to the original memory block */
  mem_chunk->mem = realloc(mem_chunk->mem, mem_chunk->size + chunk_size + 1);
  if (mem_chunk->mem == NULL)
    ERR("not enough memory (realloc returned NULL)\n");

  INFO("%s", ptr);

  /* copy the data into the newly allocated chunk */
  memcpy(&(mem_chunk->mem[mem_chunk->size]), ptr, chunk_size);
  mem_chunk->size += chunk_size;
  mem_chunk->mem[mem_chunk->size] = 0;
  return chunk_size;
}

bool is_container_running(char *url, char *cont_name)
{
  char *api_str = NULL;
  json_t *result;

  asprintf(&api_str, RUNNING_NAMED_CONTAINER, cont_name);
  result = request_json(GET_REQUEST, url, api_str, NULL);
  free(api_str);

  if (json_array_size(result)) {
    json_decref(result);
    return true;
  } else {

#ifdef DBG
  INFO("Container : %s is not running\n", cont_name);
#endif

    json_decref(result);
    return false;
  }
}