File: Remctl.xs

package info (click to toggle)
remctl 3.18-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,612 kB
  • sloc: ansic: 19,504; sh: 5,386; perl: 1,778; java: 740; makefile: 715; xml: 502; python: 430
file content (356 lines) | stat: -rw-r--r-- 8,394 bytes parent folder | download | duplicates (5)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- c -*-
 * Perl bindings for the remctl client library.
 *
 * This is an XS source file, suitable for processing by xsubpp, that
 * generates Perl bindings for the libremctl client library.  It supports
 * both the simplified interface (via a simple remctl call) and the more
 * complex interface (returning a Net::Remctl object which is then used for
 * subsequent calls).
 *
 * The remctl opaque struct is mapped to a Net::Remctl object and supports
 * methods equivalent to the library functions prefixed by remctl_ that take
 * struct remctl * as their first argument.  remctl_new() is mapped to the
 * new method of the class.  remctl_output structs are returned as
 * Net::Remctl::Output objects with accessor functions that return the
 * elements of the remctl_output struct.  The types (output, status, error,
 * and done) are returned as lowercase strings rather than as numeric
 * constants.
 *
 * The simple interface is available via an exported remctl function which
 * returns a Net::Remctl::Result object with accessor functions for the
 * members of the struct.
 *
 * Written by Russ Allbery <eagle@eyrie.org>
 * Copyright 2018 Russ Allbery <eagle@eyrie.org>
 * Copyright 2007-2008, 2011-2012, 2014
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * SPDX-License-Identifier: MIT
 */

#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>

#include <errno.h>
#include <time.h>

#include <remctl.h>

/*
 * These typedefs are needed for xsubpp to work its magic with type
 * translation to Perl objects.
 */
typedef struct remctl *         Net__Remctl;
typedef struct remctl_result *  Net__Remctl__Result;
typedef struct remctl_output *  Net__Remctl__Output;

/* Map the remctl_output type constants to strings. */
static const struct {
    enum remctl_output_type type;
    const char *name;
} OUTPUT_TYPE[] = {
    { REMCTL_OUT_OUTPUT, "output" },
    { REMCTL_OUT_STATUS, "status" },
    { REMCTL_OUT_ERROR,  "error"  },
    { REMCTL_OUT_DONE,   "done"   },
    { 0,                 NULL     }
};

/* Used to check that an object argument to a function is not NULL. */
#define CROAK_NULL(o, t, f)                     \
    do {                                        \
        if ((o) == NULL)                        \
            croak(t " object is undef in " f);  \
    } while (0);
#define CROAK_NULL_SELF(o, t, f) CROAK_NULL((o), t, t "::" f)


/* XS code below this point. */

MODULE = Net::Remctl    PACKAGE = Net::Remctl   PREFIX = remctl_

PROTOTYPES: DISABLE

Net::Remctl::Result
remctl(host, port, principal, ...)
    const char *host
    unsigned short port
    const char *principal
  PREINIT:
    size_t count = items - 3;
    size_t i;
    const char **command;
  CODE:
    if (items <= 3)
        croak("Too few arguments to Net::Remctl::remctl");
    if (principal != NULL && *principal == '\0')
        principal = NULL;
    command = calloc(count + 1, sizeof(char *));
    if (command == NULL)
        croak("Error allocating memory in Net::Remctl::remctl: %s",
              strerror(errno));
    for (i = 0; i <= count; i++)
        command[i] = SvPV_nolen(ST(i + 3));
    command[count] = NULL;
    RETVAL = remctl(host, port, principal, command);
    if (RETVAL == NULL)
        croak("Error creating Net::Remctl::Result object: %s",
              strerror(errno));
    free(command);
  OUTPUT:
    RETVAL


Net::Remctl
remctl_new(class)
    const char *class
  CODE:
    RETVAL = remctl_new();
    if (RETVAL == NULL)
        croak("Error creating %s object: %s", class, strerror(errno));
  OUTPUT:
    RETVAL


void
DESTROY(self)
    Net::Remctl self
  CODE:
    if (self != NULL)
        remctl_close(self);


void
remctl_set_ccache(self, ccache)
    Net::Remctl self
    const char *ccache
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "set_ccache");
    if (remctl_set_ccache(self, ccache))
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


void
remctl_set_source_ip(self, source)
    Net::Remctl self
    const char *source
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "set_source_ip");
    if (remctl_set_source_ip(self, source))
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


void
remctl_set_timeout(self, timeout)
    Net::Remctl self
    time_t timeout
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "set_source_timeout");
    if (remctl_set_timeout(self, timeout))
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


void
remctl_open(self, host, ...)
    Net::Remctl self
    const char *host
  PREINIT:
    size_t count = items - 2;
    unsigned short port = 0;
    const char *principal = NULL;
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "open");
    if (count > 2)
        croak("Too many arguments to Net::Remctl::open");
    if (count >= 1)
        port = SvUV(ST(2));
    if (count >= 2 && ST(3) != &PL_sv_undef) {
        principal = SvPV_nolen(ST(3));
        if (*principal == '\0')
            principal = NULL;
    }
    if (remctl_open(self, host, port, principal))
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


void
remctl_command(self, ...)
    Net::Remctl self
  PREINIT:
    struct iovec *args;
    size_t count = items - 1;
    size_t i;
    int status;
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "command");
    if (count == 0)
        croak("Too few arguments to Net::Remctl::command");
    args = calloc(count, sizeof(struct iovec));
    if (args == NULL)
        croak("Error allocating memory in Net::Remctl::command: %s",
              strerror(errno));
    for (i = 1; i <= count; i++)
        args[i - 1].iov_base = SvPV(ST(i), args[i - 1].iov_len);
    status = remctl_commandv(self, args, count);
    free(args);
    if (status)
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


Net::Remctl::Output
remctl_output(self)
    Net::Remctl self


void
remctl_noop(self)
    Net::Remctl self
  PPCODE:
    CROAK_NULL_SELF(self, "Net::Remctl", "noop");
    if (remctl_noop(self))
        XSRETURN_YES;
    else
        XSRETURN_UNDEF;


const char *
remctl_error(self)
    Net::Remctl self


MODULE = Net::Remctl    PACKAGE = Net::Remctl::Result

void
DESTROY(self)
    Net::Remctl::Result self
  CODE:
    remctl_result_free(self);


char *
error(self)
    Net::Remctl::Result self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Result", "error");
    RETVAL = self->error;
  OUTPUT:
    RETVAL


SV *
stdout(self)
    Net::Remctl::Result self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Result", "stdout");
    if (self->stdout_buf == NULL)
        XSRETURN_UNDEF;
    else
        RETVAL = newSVpvn(self->stdout_buf, self->stdout_len);
  OUTPUT:
    RETVAL


SV *
stderr(self)
    Net::Remctl::Result self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Result", "stderr");
    if (self->stderr_buf == NULL)
        XSRETURN_UNDEF;
    else
        RETVAL = newSVpvn(self->stderr_buf, self->stderr_len);
  OUTPUT:
    RETVAL


int
status(self)
    Net::Remctl::Result self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Result", "status");
    RETVAL = self->status;
  OUTPUT:
    RETVAL


MODULE = Net::Remctl    PACKAGE = Net::Remctl::Output

const char *
type(self)
    Net::Remctl::Output self
  PREINIT:
    size_t i;
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "type");
    RETVAL = NULL;
    for (i = 0; OUTPUT_TYPE[i].name != NULL; i++)
        if (OUTPUT_TYPE[i].type == self->type) {
            RETVAL = OUTPUT_TYPE[self->type].name;
            break;
        }
  OUTPUT:
    RETVAL


SV *
data(self)
    Net::Remctl::Output self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "data");
    if (self->data == NULL)
        XSRETURN_UNDEF;
    else
        RETVAL = newSVpvn(self->data, self->length);
  OUTPUT:
    RETVAL


size_t
length(self)
    Net::Remctl::Output self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "length");
    RETVAL = self->length;
  OUTPUT:
    RETVAL


int
stream(self)
    Net::Remctl::Output self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "stream");
    RETVAL = self->stream;
  OUTPUT:
    RETVAL


int
status(self)
    Net::Remctl::Output self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "status");
    RETVAL = self->status;
  OUTPUT:
    RETVAL


int
error(self)
    Net::Remctl::Output self
  CODE:
    CROAK_NULL_SELF(self, "Net::Remctl::Output", "error");
    RETVAL = self->error;
  OUTPUT:
    RETVAL