-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathvcmdmodule.c
956 lines (759 loc) · 20.2 KB
/
vcmdmodule.c
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
/*
* CORE
* Copyright (c)2010-2012 the Boeing Company.
* See the LICENSE file included in this distribution.
*
* author: Tom Goff <thomas.goff@boeing.com>
*
* vcmdmodule.c
*
* C bindings for the vcmd Python module that allows a Python script to
* execute a program within a running namespace given by the specified channel.
*
*/
#include <Python.h>
#include <structmember.h>
#include <pthread.h>
#undef NDEBUG /* XXX force enabling asserts for now */
#include <assert.h>
#include "vnode_client.h"
/* #define DEBUG */
typedef struct vcmdentry {
PyObject_HEAD
vnode_client_t *_client;
} VCmd;
typedef struct {
PyObject_HEAD
int32_t _cmdid;
int _complete;
int _status;
pthread_mutex_t _mutex;
pthread_cond_t _cv;
VCmd *_vcmd;
} VCmdWait;
int verbose;
/* ev_default_loop(0) is not used because it interferes with SIGCHLD */
static struct ev_loop *loop;
static pthread_t evloopthread;
static TAILQ_HEAD(asyncreqhead, asyncreq) asyncreqlisthead;
static pthread_mutex_t asyncreqlist_mutex = PTHREAD_MUTEX_INITIALIZER;
static int asyncpipe[2];
static pthread_mutex_t asyncpipe_writemutex = PTHREAD_MUTEX_INITIALIZER;
static ev_io asyncwatcher;
typedef void (*asyncfunc_t)(struct ev_loop *loop, void *data);
typedef struct asyncreq {
TAILQ_ENTRY(asyncreq) entries;
pthread_mutex_t mutex;
pthread_cond_t cv;
int done;
asyncfunc_t asyncfunc;
void *data;
} vcmd_asyncreq_t;
static void vcmd_asyncreq_cb(struct ev_loop *loop, ev_io *w, int revents)
{
vcmd_asyncreq_t *asyncreq;
/* drain the event pipe */
for (;;)
{
ssize_t len;
char buf[BUFSIZ];
len = read(asyncpipe[0], buf, sizeof(buf));
if (len <= 0)
{
if (len == 0)
ERR(1, "asynchronous event pipe closed");
break;
}
}
for (;;)
{
pthread_mutex_lock(&asyncreqlist_mutex);
asyncreq = TAILQ_FIRST(&asyncreqlisthead);
if (asyncreq)
TAILQ_REMOVE(&asyncreqlisthead, asyncreq, entries);
pthread_mutex_unlock(&asyncreqlist_mutex);
if (!asyncreq)
break;
assert(asyncreq->asyncfunc);
asyncreq->asyncfunc(loop, asyncreq->data);
pthread_mutex_lock(&asyncreq->mutex);
asyncreq->done = 1;
pthread_cond_broadcast(&asyncreq->cv);
pthread_mutex_unlock(&asyncreq->mutex);
}
return;
}
static void call_asyncfunc(asyncfunc_t asyncfunc, void *data)
{
vcmd_asyncreq_t asyncreq = {
.asyncfunc = asyncfunc,
.data = data,
};
char zero = 0;
ssize_t len;
pthread_mutex_init(&asyncreq.mutex, NULL);
pthread_cond_init(&asyncreq.cv, NULL);
pthread_mutex_lock(&asyncreqlist_mutex);
TAILQ_INSERT_TAIL(&asyncreqlisthead, &asyncreq, entries);
pthread_mutex_unlock(&asyncreqlist_mutex);
pthread_mutex_lock(&asyncpipe_writemutex);
len = write(asyncpipe[1], &zero, sizeof(zero));
pthread_mutex_unlock(&asyncpipe_writemutex);
if (len == -1)
ERR(1, "write() failed");
if (len != sizeof(zero))
WARN("incomplete write: %d of %d", len, sizeof(zero));
pthread_mutex_lock(&asyncreq.mutex);
Py_BEGIN_ALLOW_THREADS
while (!asyncreq.done)
pthread_cond_wait(&asyncreq.cv, &asyncreq.mutex);
Py_END_ALLOW_THREADS
pthread_mutex_unlock(&asyncreq.mutex);
pthread_mutex_destroy(&asyncreq.mutex);
pthread_cond_destroy(&asyncreq.cv);
return;
}
static void *start_evloop(void *data)
{
struct ev_loop *loop = data;
#ifdef DEBUG
WARNX("starting event loop: %p", loop);
#endif
ev_loop(loop, 0);
#ifdef DEBUG
WARNX("event loop done: %p", loop);
#endif
return NULL;
}
static int init_evloop(void)
{
int err;
loop = ev_loop_new(0);
if (!loop)
{
WARN("ev_loop_new() failed");
return -1;
}
TAILQ_INIT(&asyncreqlisthead);
err = pipe(asyncpipe);
if (err)
{
WARN("pipe() failed");
return -1;
}
set_cloexec(asyncpipe[0]);
set_cloexec(asyncpipe[1]);
set_nonblock(asyncpipe[0]);
ev_io_init(&asyncwatcher, vcmd_asyncreq_cb, asyncpipe[0], EV_READ);
ev_io_start(loop, &asyncwatcher);
err = pthread_create(&evloopthread, NULL, start_evloop, loop);
if (err)
{
errno = err;
WARN("pthread_create() failed");
return -1;
}
return 0;
}
static PyObject *VCmdWait_new(PyTypeObject *type,
PyObject *args, PyObject *kwds)
{
VCmdWait *self;
#ifdef DEBUG
WARNX("enter");
#endif
self = (VCmdWait *)type->tp_alloc(type, 0);
if (!self)
return NULL;
self->_cmdid = -1;
self->_complete = 0;
self->_status = -1;
pthread_mutex_init(&self->_mutex, NULL);
pthread_cond_init(&self->_cv, NULL);
self->_vcmd = NULL;
#ifdef DEBUG
WARNX("%p: exit", self);
#endif
return (PyObject *)self;
}
static void VCmdWait_dealloc(VCmdWait *self)
{
#ifdef DEBUG
WARNX("%p: enter", self);
#endif
pthread_mutex_destroy(&self->_mutex);
pthread_cond_destroy(&self->_cv);
if (self->_vcmd != NULL)
Py_DECREF(self->_vcmd);
self->ob_type->tp_free((PyObject *)self);
return;
}
static PyObject *VCmdWait_wait(VCmdWait *self)
{
int status;
if (self->_vcmd == NULL)
{
PyErr_SetString(PyExc_ValueError, "unstarted command");
return NULL;
}
pthread_mutex_lock(&self->_mutex);
#ifdef DEBUG
WARNX("%p: waiting for cmd %d: complete: %d; status: %d",
self, self->_cmdid, self->_complete, self->_status);
#endif
Py_BEGIN_ALLOW_THREADS
while (!self->_complete)
pthread_cond_wait(&self->_cv, &self->_mutex);
Py_END_ALLOW_THREADS
status = self->_status;
pthread_mutex_unlock(&self->_mutex);
#ifdef DEBUG
WARNX("%p: done waiting for cmd %d: status: %d",
self, self->_cmdid, self->_status);
#endif
return Py_BuildValue("i", status);
}
static PyObject *VCmdWait_complete(VCmdWait *self,
PyObject *args, PyObject *kwds)
{
if (self->_vcmd == NULL)
{
PyErr_SetString(PyExc_ValueError, "unstarted command");
return NULL;
}
if (self->_complete)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject *VCmdWait_status(VCmdWait *self,
PyObject *args, PyObject *kwds)
{
if (self->_vcmd == NULL)
{
PyErr_SetString(PyExc_ValueError, "unstarted command");
return NULL;
}
if (self->_complete)
return Py_BuildValue("i", self->_status);
else
Py_RETURN_NONE;
}
static PyObject *VCmdWait_kill(VCmdWait *self,
PyObject *args, PyObject *kwds)
{
int sig;
if (!PyArg_ParseTuple(args, "i", &sig))
return NULL;
if (self->_vcmd == NULL)
{
PyErr_SetString(PyExc_ValueError, "unstarted command");
return NULL;
}
if (self->_complete)
{
PyErr_SetString(PyExc_ValueError, "command already complete");
return NULL;
}
if (vnode_send_cmdsignal(self->_vcmd->_client->serverfd, self->_cmdid, sig))
{
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
Py_RETURN_NONE;
}
static PyMemberDef VCmdWait_members[] = {
{"vcmd", T_OBJECT, offsetof(VCmdWait, _vcmd), READONLY,
"VCmd instance that created this object"},
{NULL, 0, 0, 0, NULL},
};
static PyMethodDef VCmdWait_methods[] = {
{"wait", (PyCFunction)VCmdWait_wait, METH_NOARGS,
"wait() -> int\n\n"
"Wait for command to complete and return exit status"},
{"complete", (PyCFunction)VCmdWait_complete, METH_NOARGS,
"complete() -> boolean\n\n"
"Return True if command has completed; return False otherwise."},
{"status", (PyCFunction)VCmdWait_status, METH_NOARGS,
"status() -> int\n\n"
"Return exit status if command has completed; return None otherwise."},
{"kill", (PyCFunction)VCmdWait_kill, METH_VARARGS,
"kill(signum) -> None\n\n"
"Send a signal to command.\n\n"
"signum: the signal to send"},
{NULL, NULL, 0, NULL},
};
static PyTypeObject vcmd_VCmdWaitType = {
PyObject_HEAD_INIT(NULL)
.tp_name = "vcmd.VCmdWait",
.tp_basicsize = sizeof(VCmdWait),
.tp_dealloc = (destructor)VCmdWait_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "VCmdWait objects",
.tp_methods = VCmdWait_methods,
.tp_members = VCmdWait_members,
.tp_new = VCmdWait_new,
};
static PyObject *VCmd_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
VCmd *self;
self = (VCmd *)type->tp_alloc(type, 0);
if (!self)
return NULL;
self->_client = NULL;
return (PyObject *)self;
}
static void vcmd_ioerrorcb(vnode_client_t *client)
{
VCmd *self;
PyGILState_STATE gstate = 0;
int pythreads;
pythreads = PyEval_ThreadsInitialized();
if (pythreads)
gstate = PyGILState_Ensure();
if (verbose)
WARNX("i/o error for client %p", client);
self = client->data;
assert(self);
assert(self->_client == client);
if (self->_client)
{
vnode_delclient(self->_client);
self->_client = NULL;
}
if (pythreads)
PyGILState_Release(gstate);
return;
}
typedef struct {
vnode_client_t *client;
const char *ctrlchnlname;
void *data;
} vcmd_newclientreq_t;
static void async_newclientreq(struct ev_loop *loop, void *data)
{
vcmd_newclientreq_t *newclreq = data;
newclreq->client = vnode_client(loop, newclreq->ctrlchnlname,
vcmd_ioerrorcb, newclreq->data);
return;
}
typedef struct {
VCmd *vcmd;
} vcmd_delclientreq_t;
static void async_delclientreq(struct ev_loop *loop, void *data)
{
vcmd_delclientreq_t *delclreq = data;
if (delclreq->vcmd->_client)
{
vnode_delclient(delclreq->vcmd->_client);
delclreq->vcmd->_client = NULL;
}
return;
}
static int VCmd_init(VCmd *self, PyObject *args, PyObject *kwds)
{
vcmd_newclientreq_t newclreq = {.data = self};
#ifdef DEBUG
WARNX("%p: enter", self);
#endif
if (!loop)
if (init_evloop())
return -1;
if (!PyArg_ParseTuple(args, "s", &newclreq.ctrlchnlname))
return -1;
call_asyncfunc(async_newclientreq, &newclreq);
self->_client = newclreq.client;
if (!self->_client)
{
WARN("vnode_client() failed");
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
}
static void VCmd_dealloc(VCmd *self)
{
#ifdef DEBUG
WARNX("%p: enter", self);
#endif
if (self->_client)
{
vcmd_delclientreq_t delclreq = {.vcmd = self};
call_asyncfunc(async_delclientreq, &delclreq);
}
self->ob_type->tp_free((PyObject *)self);
return;
}
static PyObject *VCmd_connected(VCmd *self, PyObject *args, PyObject *kwds)
{
if (self->_client != NULL)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static void vcmd_cmddonecb(int32_t cmdid, pid_t pid, int status, void *data)
{
VCmdWait *cmdwait = data;
PyGILState_STATE gstate = 0;
int pythreads;
#ifdef DEBUG
WARNX("cmdid %d; pid %d; status: 0x%x", cmdid, pid, status);
if (WIFEXITED(status))
WARNX("command %d terminated normally with status: %d",
cmdid, WEXITSTATUS(status));
else if (WIFSIGNALED(status))
WARNX("command %d terminated by signal: %d", cmdid, WTERMSIG(status));
else
WARNX("unexpected termination status for command %d: 0x%x", cmdid, status);
#endif
#ifdef DEBUG
WARNX("%p: waiting for lock", cmdwait);
#endif
pthread_mutex_lock(&cmdwait->_mutex);
cmdwait->_status = status;
cmdwait->_complete = 1;
#ifdef DEBUG
WARNX("%p: command callback done", cmdwait);
#endif
pthread_cond_broadcast(&cmdwait->_cv);
pthread_mutex_unlock(&cmdwait->_mutex);
pythreads = PyEval_ThreadsInitialized();
if (pythreads)
gstate = PyGILState_Ensure();
Py_DECREF(cmdwait);
if (pythreads)
PyGILState_Release(gstate);
return;
}
typedef struct {
int cmdid;
vnode_client_t *client;
vnode_client_cmdio_t *clientcmdio;
void *data;
int argc;
char **argv;
} vcmd_cmdreq_t;
static void async_cmdreq(struct ev_loop *loop, void *data)
{
vcmd_cmdreq_t *cmdreq = data;
cmdreq->cmdid = vnode_client_cmdreq(cmdreq->client, cmdreq->clientcmdio,
vcmd_cmddonecb, cmdreq->data,
cmdreq->argc, cmdreq->argv);
return;
}
static void free_string_array(char **array, Py_ssize_t count)
{
Py_ssize_t i;
for (i = 0; i < count; i++)
PyMem_Free(array[i]);
PyMem_Del(array);
}
static PyObject *_VCmd_cmd(VCmd *self, PyObject *args, PyObject *kwds,
vnode_client_cmdiotype_t iotype)
{
int status, infd, outfd, errfd;
PyObject *cmdargs;
char **argv = NULL;
Py_ssize_t i, argc;
PyObject *(*getitem)(PyObject *, Py_ssize_t);
VCmdWait *cmdwait;
vnode_client_cmdio_t *cmdio;
PyObject *pyinfile = NULL, *pyoutfile = NULL, *pyerrfile = NULL;
PyObject *pyptyfile = NULL;
PyObject *ret;
if (self->_client == NULL)
{
PyErr_SetString(PyExc_ValueError, "not connected");
return NULL;
}
if (iotype == VCMD_IO_FD)
{
char *kwlist[] = {"infd", "outfd", "errfd", "args", NULL};
status = PyArg_ParseTupleAndKeywords(args, kwds, "iiiO", kwlist,
&infd, &outfd, &errfd, &cmdargs);
}
else
{
char *kwlist[] = {"args", NULL};
status = PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &cmdargs);
}
if (!status)
return NULL;
/* cmdargs must be a list or tuple of strings */
if (PyList_Check(cmdargs))
{
argc = PyList_Size(cmdargs);
getitem = PyList_GetItem;
}
else if (PyTuple_Check(cmdargs))
{
argc = PyTuple_Size(cmdargs);
getitem = PyTuple_GetItem;
}
else
{
argc = -1;
}
if (argc <= 0)
{
PyErr_SetString(PyExc_TypeError,
"cmd arg must be a nonempty tuple or list");
return NULL;
}
argv = PyMem_New(char *, argc + 1);
if (argv == NULL)
return PyErr_NoMemory();
for (i = 0; i < argc; i++)
{
if (!PyArg_Parse((*getitem)(cmdargs, i), "et",
Py_FileSystemDefaultEncoding, &argv[i]))
{
free_string_array(argv, i);
PyErr_SetString(PyExc_TypeError, "cmd arg must contain only strings");
return NULL;
}
}
argv[argc] = NULL;
cmdwait = (VCmdWait *)VCmdWait_new(&vcmd_VCmdWaitType, NULL, NULL);
if (cmdwait == NULL)
{
free_string_array(argv, i);
return PyErr_NoMemory();
}
pthread_mutex_lock(&cmdwait->_mutex);
cmdwait->_cmdid = -1;
cmdio = vnode_open_clientcmdio(iotype);
if (cmdio)
{
int err = 0;
vcmd_cmdreq_t cmdreq = {
.client = self->_client,
.clientcmdio = cmdio,
.data = cmdwait,
.argc = argc,
.argv = argv,
};
#define PYFILE(obj, fd, name, mode) \
do { \
FILE *tmp; \
obj = NULL; \
tmp = fdopen(fd, mode); \
if (!tmp) \
{ \
WARN("fdopen() failed for fd %d", fd); \
break; \
} \
obj = PyFile_FromFile(tmp, name, mode, fclose); \
if (!obj) \
fclose(tmp); \
} while(0)
switch (iotype)
{
case VCMD_IO_NONE:
break;
case VCMD_IO_FD:
SET_STDIOFD(cmdio, infd, outfd, errfd);
break;
case VCMD_IO_PIPE:
PYFILE(pyinfile, cmdio->stdiopipe.infd[1], "<pipe>", "wb");
if (!pyinfile)
{
err = 1;
break;
}
PYFILE(pyoutfile, cmdio->stdiopipe.outfd[0], "<pipe>", "rb");
if (!pyoutfile)
{
PyObject_Del(pyinfile);
err = 1;
break;
}
PYFILE(pyerrfile, cmdio->stdiopipe.errfd[0], "<pipe>", "rb");
if (!pyerrfile)
{
PyObject_Del(pyoutfile);
PyObject_Del(pyinfile);
err = 1;
break;
}
break;
case VCMD_IO_PTY:
PYFILE(pyptyfile, cmdio->stdiopty.masterfd, "/dev/ptmx", "r+b");
if (!pyptyfile)
err = 1;
break;
default:
if (verbose)
WARNX("invalid iotype: 0x%x", iotype);
errno = EINVAL;
err = 1;
break;
}
#undef PYFILE
if (!err)
{
call_asyncfunc(async_cmdreq, &cmdreq);
cmdwait->_cmdid = cmdreq.cmdid;
}
}
free_string_array(argv, argc);
free(cmdio);
if (cmdwait->_cmdid < 0)
{
if (pyinfile)
PyObject_Del(pyinfile);
if (pyoutfile)
PyObject_Del(pyoutfile);
if (pyerrfile)
PyObject_Del(pyerrfile);
if (pyptyfile)
PyObject_Del(pyptyfile);
PyErr_SetFromErrno(PyExc_OSError);
pthread_mutex_unlock(&cmdwait->_mutex);
Py_DECREF(cmdwait);
return NULL;
}
/* don't do Py_DECREF(cmdwait) or VCmdWait_dealloc(cmdwait) if
* there's an error below since cmddonecb should still get called
*/
Py_INCREF(self);
cmdwait->_vcmd = self;
switch (iotype)
{
case VCMD_IO_NONE:
case VCMD_IO_FD:
ret = Py_BuildValue("O", (PyObject *)cmdwait);
break;
case VCMD_IO_PIPE:
ret = Py_BuildValue("(OOOO)", (PyObject *)cmdwait,
pyinfile, pyoutfile, pyerrfile);
break;
case VCMD_IO_PTY:
ret = Py_BuildValue("(OO)", (PyObject *)cmdwait, pyptyfile);
break;
default:
ret = NULL;
break;
}
pthread_mutex_unlock(&cmdwait->_mutex);
return ret;
}
static PyObject *VCmd_qcmd(VCmd *self, PyObject *args, PyObject *kwds)
{
return _VCmd_cmd(self, args, kwds, VCMD_IO_NONE);
}
static PyObject *VCmd_redircmd(VCmd *self, PyObject *args, PyObject *kwds)
{
return _VCmd_cmd(self, args, kwds, VCMD_IO_FD);
}
static PyObject *VCmd_popen(VCmd *self, PyObject *args, PyObject *kwds)
{
return _VCmd_cmd(self, args, kwds, VCMD_IO_PIPE);
}
static PyObject *VCmd_ptyopen(VCmd *self, PyObject *args, PyObject *kwds)
{
return _VCmd_cmd(self, args, kwds, VCMD_IO_PTY);
}
static PyObject *VCmd_kill(VCmd *self, PyObject *args, PyObject *kwds)
{
VCmdWait *cmdwait;
int sig;
if (!PyArg_ParseTuple(args, "O!i", &vcmd_VCmdWaitType, &cmdwait, &sig))
return NULL;
if (cmdwait->_complete)
{
PyErr_SetString(PyExc_ValueError, "command already complete");
return NULL;
}
if (vnode_send_cmdsignal(self->_client->serverfd, cmdwait->_cmdid, sig))
{
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *VCmd_close(VCmd *self, PyObject *args, PyObject *kwds)
{
if (self->_client)
{
vcmd_delclientreq_t delclreq = {.vcmd = self};
call_asyncfunc(async_delclientreq, &delclreq);
}
Py_RETURN_NONE;
}
static PyMemberDef VCmd_members[] = {
{NULL, 0, 0, 0, NULL},
};
static PyMethodDef VCmd_methods[] = {
{"connected", (PyCFunction)VCmd_connected, METH_NOARGS,
"connected() -> boolean\n\n"
"returns True if connected; False otherwise"},
{"popen", (PyCFunction)VCmd_popen, METH_VARARGS | METH_KEYWORDS,
"popen(args...) -> (VCmdWait, cmdin, cmdout, cmderr)\n\n"
"Send command request and use pipe I/O.\n\n"
"args: executable file name followed by command arguments"},
{"ptyopen", (PyCFunction)VCmd_ptyopen, METH_VARARGS| METH_KEYWORDS,
"ptyopen(args...) -> (VCmdWait, cmdpty)\n\n"
"Send command request and use pty I/O.\n\n"
"args: executable file name followed by command arguments"},
{"qcmd", (PyCFunction)VCmd_qcmd, METH_VARARGS | METH_KEYWORDS,
"qcmd(args...) -> VCmdWait\n\n"
"Send command request without I/O.\n\n"
"args: executable file name followed by command arguments"},
{"redircmd", (PyCFunction)VCmd_redircmd, METH_VARARGS | METH_KEYWORDS,
"redircmd(infd, outfd, errfd, args...) -> VCmdWait\n\n"
"Send command request with I/O redirected from/to the given fds.\n\n"
"infd: file descriptor for command standard input\n"
"outfd: file descriptor for command standard output\n"
"errfd: file descriptor for command standard error\n"
"args: executable file name followed by command arguments"},
{"kill", (PyCFunction)VCmd_kill, METH_VARARGS,
"kill(cmdwait, signum) -> None\n\n"
"Send signal to a command.\n\n"
"cmdwait: the VCmdWait object from an earlier command request\n"
"signum: the signal to send"},
{"close", (PyCFunction)VCmd_close, METH_NOARGS,
"close() -> None"},
{NULL, NULL, 0, NULL},
};
static PyTypeObject vcmd_VCmdType = {
PyObject_HEAD_INIT(NULL)
.tp_name = "vcmd.VCmd",
.tp_basicsize = sizeof(VCmd),
.tp_dealloc = (destructor)VCmd_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "VCmd objects",
.tp_methods = VCmd_methods,
.tp_members = VCmd_members,
.tp_init = (initproc)VCmd_init,
.tp_new = VCmd_new,
};
static PyObject *vcmd_verbose(PyObject *self, PyObject *args)
{
int oldval = verbose;
if (!PyArg_ParseTuple(args, "|i", &verbose))
return NULL;
return Py_BuildValue("i", oldval);
}
static PyMethodDef vcmd_methods[] = {
{"verbose", (PyCFunction)vcmd_verbose, METH_VARARGS,
"verbose([newval]) -> int\n\n"
"Get the current verbose level and optionally set it to newval."},
{NULL, NULL, 0, NULL},
};
PyMODINIT_FUNC initvcmd(void)
{
PyObject *m;
if (PyType_Ready(&vcmd_VCmdType) < 0)
return;
if (PyType_Ready(&vcmd_VCmdWaitType) < 0)
return;
m = Py_InitModule3("vcmd", vcmd_methods, "vcmd module that does stuff...");
if (!m)
return;
Py_INCREF(&vcmd_VCmdType);
PyModule_AddObject(m, "VCmd", (PyObject *)&vcmd_VCmdType);
Py_INCREF(&vcmd_VCmdWaitType);
PyModule_AddObject(m, "VCmdWait", (PyObject *)&vcmd_VCmdWaitType);
return;
}