summaryrefslogtreecommitdiffstats
path: root/erlang/erl-guestfs-proto.c
blob: a1f057753a4cb14b2e82b3b18beb3990bc5cb48d (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* libguestfs Erlang bindings.
 * Copyright (C) 2011 Red Hat Inc.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <erl_interface.h>
/* We should switch over to using
  #include <ei.h>
instead of erl_interface.
*/

#include "error.h"
#include "full-read.h"
#include "full-write.h"

#include "guestfs.h"

guestfs_h *g;

extern ETERM *dispatch (ETERM *message);
extern int atom_equals (ETERM *atom, const char *name);
extern ETERM *make_error (const char *funname);
extern ETERM *unknown_optarg (const char *funname, ETERM *optargname);
extern ETERM *unknown_function (ETERM *fun);
extern ETERM *make_string_list (char **r);
extern ETERM *make_table (char **r);
extern ETERM *make_bool (int r);
extern char **get_string_list (ETERM *term);
extern int get_bool (ETERM *term);
extern int get_int (ETERM *term);
extern int64_t get_int64 (ETERM *term);
extern void free_strings (char **r);

/* This stops things getting out of hand, but also lets us detect
 * protocol problems quickly.
 */
#define MAX_MESSAGE_SIZE (32*1024*1024)

static unsigned char *read_message (void);
static void write_reply (ETERM *);

int
main (void)
{
  unsigned char *buf;
  ETERM *ret, *message;

  erl_init (NULL, 0);

  /* This process has a single libguestfs handle.  If the Erlang
   * system creates more than one handle, then more than one of these
   * processes will be running.
   */
  g = guestfs_create ();
  if (g == NULL)
    error (EXIT_FAILURE, 0, "could not create guestfs handle");

  guestfs_set_error_handler (g, NULL, NULL);

  while ((buf = read_message ()) != NULL) {
    message = erl_decode (buf);
    free (buf);

    ret = dispatch (message);
    erl_free_term (message);

    write_reply (ret);
    erl_free_term (ret);
  }

  guestfs_close (g);

  exit (EXIT_SUCCESS);
}

/* The Erlang port always sends the length of the buffer as 4
 * bytes in network byte order, followed by the message buffer.
 */
static unsigned char *
read_message (void)
{
  unsigned char buf[4];
  size_t size;
  unsigned char *r;

  errno = 0;
  if (full_read (0, buf, 4) != 4) {
    if (errno == 0) /* ok - closed connection normally */
      return NULL;
    else
      error (EXIT_FAILURE, errno, "read message size");
  }

  size = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];

  if (size > MAX_MESSAGE_SIZE)
    error (EXIT_FAILURE, 0, "message larger than MAX_MESSAGE_SIZE");

  r = malloc (size);
  if (r == NULL)
    error (EXIT_FAILURE, errno, "malloc");

  if (full_read (0, r, size) != size)
    error (EXIT_FAILURE, errno, "read message content");

  return r;
}

static void
write_reply (ETERM *term)
{
  size_t size;
  unsigned char sbuf[4];
  unsigned char *buf;

  size = erl_term_len (term);

  buf = malloc (size);
  if (buf == NULL)
    error (EXIT_FAILURE, errno, "malloc");

  erl_encode (term, buf);

  sbuf[0] = (size >> 24) & 0xff;
  sbuf[1] = (size >> 16) & 0xff;
  sbuf[2] = (size >> 8) & 0xff;
  sbuf[3] = size & 0xff;

  if (full_write (1, sbuf, 4) != 4)
    error (EXIT_FAILURE, errno, "write message size");

  if (full_write (1, buf, size) != size)
    error (EXIT_FAILURE, errno, "write message content");

  free (buf);
}

/* Note that all published Erlang code/examples etc uses strncmp in
 * a buggy way.  This is the right way to do it.
 */
int
atom_equals (ETERM *atom, const char *name)
{
  size_t namelen = strlen (name);
  size_t atomlen = ERL_ATOM_SIZE (atom);
  if (namelen != atomlen) return 0;
  return strncmp (ERL_ATOM_PTR (atom), name, atomlen) == 0;
}

ETERM *
make_error (const char *funname)
{
  ETERM *error = erl_mk_atom ("error");
  ETERM *msg = erl_mk_string (guestfs_last_error (g));
  ETERM *num = erl_mk_int (guestfs_last_errno (g));
  ETERM *t[3] = { error, msg, num };
  return erl_mk_tuple (t, 3);
}

ETERM *
unknown_function (ETERM *fun)
{
  ETERM *unknown = erl_mk_atom ("unknown");
  ETERM *funcopy = erl_copy_term (fun);
  ETERM *t[2] = { unknown, funcopy };
  return erl_mk_tuple (t, 2);
}

ETERM *
unknown_optarg (const char *funname, ETERM *optargname)
{
  ETERM *unknownarg = erl_mk_atom ("unknownarg");
  ETERM *copy = erl_copy_term (optargname);
  ETERM *t[2] = { unknownarg, copy };
  return erl_mk_tuple (t, 2);
}

ETERM *
make_string_list (char **r)
{
  size_t i, size;

  for (size = 0; r[size] != NULL; ++size)
    ;

  ETERM *t[size];

  for (i = 0; r[i] != NULL; ++i)
    t[i] = erl_mk_string (r[i]);

  return erl_mk_list (t, size);
}

/* Make a hash table.  The number of elements returned by the C
 * function is always even.
 */
ETERM *
make_table (char **r)
{
  size_t i, size;

  for (size = 0; r[size] != NULL; ++size)
    ;

  ETERM *t[size/2];
  ETERM *a[2];

  for (i = 0; r[i] != NULL; i += 2) {
    a[0] = erl_mk_string (r[i]);
    a[1] = erl_mk_string (r[i+1]);
    t[i/2] = erl_mk_tuple (a, 2);
  }

  return erl_mk_list (t, size/2);
}

ETERM *
make_bool (int r)
{
  if (r)
    return erl_mk_atom ("true");
  else
    return erl_mk_atom ("false");
}

char **
get_string_list (ETERM *term)
{
  ETERM *t;
  size_t i, size;
  char **r;

  for (size = 0, t = term; !ERL_IS_EMPTY_LIST (t);
       size++, t = ERL_CONS_TAIL (t))
    ;

  r = malloc ((size+1) * sizeof (char *));
  if (r == NULL)
    error (EXIT_FAILURE, errno, "malloc");

  for (i = 0, t = term; !ERL_IS_EMPTY_LIST (t); i++, t = ERL_CONS_TAIL (t))
    r[i] = erl_iolist_to_string (ERL_CONS_HEAD (t));
  r[size] = NULL;

  return r;
}

int
get_bool (ETERM *term)
{
  if (atom_equals (term, "true"))
    return 1;
  else
    return 0;
}

int
get_int (ETERM *term)
{
  switch (ERL_TYPE (term)) {
  case ERL_INTEGER:
    return ERL_INT_VALUE (term);
  case ERL_U_INTEGER:
    return (int) ERL_INT_UVALUE (term);
  case ERL_LONGLONG:
    /* XXX check for overflow */
    return (int) ERL_LL_VALUE (term);
  case ERL_U_LONGLONG:
    /* XXX check for overflow */
    return (int) ERL_LL_UVALUE (term);
  default:
    /* XXX fail in some way */
    return -1;
  }
}

int64_t
get_int64 (ETERM *term)
{
  switch (ERL_TYPE (term)) {
  case ERL_INTEGER:
    return ERL_INT_VALUE (term);
  case ERL_U_INTEGER:
    return ERL_INT_UVALUE (term);
  case ERL_LONGLONG:
    return ERL_LL_VALUE (term);
  case ERL_U_LONGLONG:
    return (int64_t) ERL_LL_UVALUE (term);
  default:
    /* XXX fail in some way */
    return -1;
  }
}

void
free_strings (char **r)
{
  size_t i;

  for (i = 0; r[i] != NULL; ++i)
    free (r[i]);
  free (r);
}