-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathproc.c
1273 lines (1135 loc) · 31.6 KB
/
proc.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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set sw=4 sts=4 et: */
/**
* FILE: proc.c
* AUTHOR: Yukihiro Nakadaira <http://yukihiro.nakadaira.googlepages.com/#vimproc> (original)
* Nico Raffo <nicoraffo@gmail.com> (modified)
*/
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <stddef.h>
#include <dlfcn.h>
#include <ctype.h>
#include <dirent.h>
#if !defined __APPLE__
# include <sys/types.h>
# include <sys/ioctl.h>
#endif
#include <signal.h>
#include <fcntl.h>
/* for poll() */
#if defined __APPLE__
# include "fakepoll.h"
#else
# include <poll.h>
#endif
/* for forkpty() / login_tty() */
#if (defined __linux__ || defined __CYGWIN__ || defined __gnu_hurd__) && !defined __ANDROID__
# include <pty.h>
# include <utmp.h>
#elif defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
# include <util.h>
#elif defined __sun__ || defined __ANDROID__
# include "ptytty.h"
#else
# include <termios.h>
# include <libutil.h>
#endif
/* for ioctl() */
#ifdef __APPLE__
# include <sys/ioctl.h>
#endif
/* for tc* and ioctl */
#include <sys/types.h>
#include <termios.h>
#ifndef TIOCGWINSZ
# include <sys/ioctl.h> /* 4.3+BSD requires this too */
#endif
/* for waitpid() */
#include <sys/types.h>
#include <sys/wait.h>
#if defined __NetBSD__
# define WIFCONTINUED(x) (_WSTATUS(x) == _WSTOPPED && WSTOPSIG(x) == 0x13)
#elif defined __ANDROID__
# define WIFCONTINUED(x) (WIFSTOPPED(x) && WSTOPSIG(x) == 0x13)
#endif
/* for socket */
#if defined __FreeBSD__
# define __BSD_VISIBLE 1
# include <arpa/inet.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/* for ctermid */
#if defined __ANDROID__
# define ctermid(x) "/dev/tty"
#endif
#include "vimstack.c"
const int debug = 0;
/* API */
const char *vp_dlopen(char *args); /* [handle] (path) */
const char *vp_dlclose(char *args); /* [] (handle) */
const char *vp_dlversion(char *args); /* [version] () */
const char *vp_file_open(char *args); /* [fd] (path, flags, mode) */
const char *vp_file_close(char *args); /* [] (fd) */
const char *vp_file_read(char *args); /* [eof, hd] (fd, cnt, timeout) */
const char *vp_file_write(char *args); /* [nleft] (fd, timeout, hd) */
const char *vp_pipe_open(char *args); /* [pid, [fd] * npipe]
(npipe, hstdin, hstdout, hstderr, argc, [argv]) */
const char *vp_pipe_close(char *args); /* [] (fd) */
const char *vp_pipe_read(char *args); /* [eof, hd] (fd, cnt, timeout) */
const char *vp_pipe_write(char *args); /* [nleft] (fd, timeout, hd) */
const char *vp_pty_open(char *args);
/* [pid, stdin, stdout, stderr]
(npipe, width, height,hstdin, hstdout, hstderr, argc, [argv]) */
const char *vp_pty_close(char *args); /* [] (fd) */
const char *vp_pty_read(char *args); /* [eof, hd] (fd, cnt, timeout) */
const char *vp_pty_write(char *args); /* [nleft] (fd, timeout, hd) */
const char *vp_pty_get_winsize(char *args); /* [width, height] (fd) */
const char *vp_pty_set_winsize(char *args); /* [] (fd, width, height) */
const char *vp_kill(char *args); /* [] (pid, sig) */
const char *vp_waitpid(char *args); /* [cond, status] (pid) */
const char *vp_socket_open(char *args); /* [socket] (host, port) */
const char *vp_socket_close(char *args);/* [] (socket) */
const char *vp_socket_read(char *args); /* [eof, hd] (socket, cnt, timeout) */
const char *vp_socket_write(char *args);/* [nleft] (socket, hd, timeout) */
const char *vp_host_exists(char *args); /* [int] (host) */
const char *vp_decode(char *args); /* [decoded_str] (encode_str) */
const char *vp_get_signals(char *args); /* [signals] () */
/* --- */
#define VP_BUFSIZE (65536)
#define VP_READ_BUFSIZE (VP_BUFSIZE - (VP_HEADER_SIZE + 1) * 2 - 1)
static vp_stack_t _result = VP_STACK_NULL;
const char *
vp_dlopen(char *args)
{
vp_stack_t stack;
char *path;
void *handle;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_str(&stack, &path));
VP_RETURN_IF_FAIL(vp_stack_reserve(&_result, VP_BUFSIZE));
handle = dlopen(path, RTLD_LAZY);
if (handle == NULL)
return dlerror();
vp_stack_push_num(&_result, "%p", handle);
return vp_stack_return(&_result);
}
const char *
vp_dlclose(char *args)
{
vp_stack_t stack;
void *handle;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%p", &handle));
/* On FreeBSD6, to call dlclose() twice with same pointer causes SIGSEGV */
if (dlclose(handle) == -1)
return dlerror();
vp_stack_free(&_result);
return NULL;
}
const char *
vp_dlversion(char *args)
{
vp_stack_push_num(&_result, "%2d%02d", 9, 3);
return vp_stack_return(&_result);
}
static int
str_to_oflag(const char *flags)
{
int oflag = 0;
if (strchr("rwa", flags[0])) {
if (strchr(flags, '+')) {
oflag = O_RDWR;
} else {
oflag = flags[0] == 'r' ? O_RDONLY : O_WRONLY;
}
if (flags[0] == 'w' || flags[0] == 'a') {
oflag |= O_CREAT | (flags[0] == 'w' ? O_TRUNC : O_APPEND);
}
#define VP_CHR_TO_OFLAG(_c, _f) do { \
if (strchr(flags, (_c))) { oflag |= (_f); } \
} while (0)
#ifdef O_EXCL
VP_CHR_TO_OFLAG('x', O_EXCL);
#endif
#ifdef O_CLOEXEC
VP_CHR_TO_OFLAG('e', O_CLOEXEC);
#endif
#ifdef O_BINARY
VP_CHR_TO_OFLAG('b', O_BINARY);
#endif
#ifdef O_TEXT
VP_CHR_TO_OFLAG('t', O_TEXT);
#endif
#ifdef O_SEQUENTIAL
VP_CHR_TO_OFLAG('S', O_SEQUENTIAL);
#endif
#ifdef O_RANDOM
VP_CHR_TO_OFLAG('R', O_RANDOM);
#endif
#undef VP_CHR_TO_OFLAG
} else {
if (strstr(flags, "O_RDONLY")) {
oflag = O_RDONLY;
} else if (strstr(flags, "O_WRONLY")) {
oflag = O_WRONLY;
} else if (strstr(flags, "O_RDWR")) {
oflag = O_RDWR;
} else {
return -1;
}
#define VP_STR_TO_OFLAG(_f) do { \
if (strstr(flags, #_f)) { oflag |= (_f); } \
} while (0)
VP_STR_TO_OFLAG(O_APPEND);
VP_STR_TO_OFLAG(O_CREAT);
VP_STR_TO_OFLAG(O_TRUNC);
#ifdef O_EXCL
VP_STR_TO_OFLAG(O_EXCL);
#endif
#ifdef O_NONBLOCK
VP_STR_TO_OFLAG(O_NONBLOCK);
#endif
#ifdef O_SHLOCK
VP_STR_TO_OFLAG(O_SHLOCK);
#endif
#ifdef O_EXLOCK
VP_STR_TO_OFLAG(O_EXLOCK);
#endif
#ifdef O_DIRECT
VP_STR_TO_OFLAG(O_DIRECT);
#endif
#ifdef O_FSYNC
VP_STR_TO_OFLAG(O_FSYNC);
#endif
#ifdef O_NOFOLLOW
VP_STR_TO_OFLAG(O_NOFOLLOW);
#endif
#ifdef O_TEMPORARY
VP_STR_TO_OFLAG(O_TEMPORARY);
#endif
#ifdef O_RANDOM
VP_STR_TO_OFLAG(O_RANDOM);
#endif
#ifdef O_SEQUENTIAL
VP_STR_TO_OFLAG(O_SEQUENTIAL);
#endif
#ifdef O_BINARY
VP_STR_TO_OFLAG(O_BINARY);
#endif
#ifdef O_TEXT
VP_STR_TO_OFLAG(O_TEXT);
#endif
#ifdef O_INHERIT
VP_STR_TO_OFLAG(O_INHERIT);
#endif
#ifdef _O_SHORT_LIVED
VP_STR_TO_OFLAG(O_SHORT_LIVED);
#endif
#undef VP_STR_TO_OFLAG
}
return oflag;
}
static int
fd_set_nonblock(int fd)
{
#if defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK)
int flag;
if ((flag = fcntl(fd, F_GETFL, 0)) == -1)
return -1;
if (!(flag & O_NONBLOCK))
return fcntl(fd, F_SETFL, flag | O_NONBLOCK);
#endif
return 0;
}
#ifdef __linux__
# define VP_SET_NONBLOCK_IF_NEEDED(_fd) (void)fd_set_nonblock(_fd)
#else
# define VP_SET_NONBLOCK_IF_NEEDED(_fd) do { /* nop */ } while (0)
#endif
const char *
vp_fd_read(char *args, int is_pty_pipe)
{
#ifdef __linux__
# define VP_POLLIN (POLLIN | POLLHUP)
#else
# define VP_POLLIN (POLLIN)
#endif
vp_stack_t stack;
int fd;
int cnt;
int timeout;
int n;
char *buf;
char *eof;
unsigned int size = 0;
struct pollfd pfd = {0, POLLIN, 0};
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &cnt));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &timeout));
if (cnt < 0 || VP_READ_BUFSIZE < cnt) {
cnt = VP_READ_BUFSIZE;
}
/* initialize buffer */
_result.top = _result.buf;
vp_stack_push_num(&_result, "%d", 0); /* set eof to 0 */
eof = _result.top - 1;
buf = _result.top;
*(buf++) = VP_EOV;
buf += VP_HEADER_SIZE;
pfd.fd = fd;
while (cnt > 0) {
n = poll(&pfd, 1, timeout);
if (n == -1) {
/* eof or error */
*eof = '1';
break;
} else if (n == 0) {
/* timeout */
break;
}
if (pfd.revents & VP_POLLIN) {
n = read(fd, buf, cnt);
if (n == -1) {
if (pfd.revents & POLLERR
|| pfd.revents & POLLNVAL
|| pfd.revents & POLLWRNORM
/* Cygwin(after ver.2.0) fails pty read and returns
* POLLIN. */
|| (!is_pty_pipe && pfd.revents & POLLIN)
) {
return vp_stack_return_error(&_result,
"read() error: revents = %d, error = %s",
pfd.revents, strerror(errno));
}
/* eof */
*eof = '1';
break;
} else if (n == 0) {
/* eof */
*eof = '1';
break;
}
/* decrease stack top for concatenate. */
cnt -= n;
buf += n;
size += n;
/* try read more bytes without waiting */
timeout = 0;
continue;
} else if (pfd.revents & (POLLERR | POLLHUP)) {
/* eof or error */
*eof = '1';
break;
} else if (pfd.revents & POLLNVAL) {
return vp_stack_return_error(&_result, "poll() POLLNVAL: %d",
pfd.revents);
}
/* DO NOT REACH HERE */
return vp_stack_return_error(&_result, "poll() unknown status: %d",
pfd.revents);
}
vp_encode_size(size, _result.top + 1);
_result.top = buf;
return vp_stack_return(&_result);
#undef VP_POLLIN
}
const char *
vp_file_open(char *args)
{
vp_stack_t stack;
char *path;
char *flags;
int mode; /* used when flags have O_CREAT */
int oflag = 0;
int fd;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_str(&stack, &path));
VP_RETURN_IF_FAIL(vp_stack_pop_str(&stack, &flags));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &mode));
oflag = str_to_oflag(flags);
if (oflag == -1)
return vp_stack_return_error(&_result, "open flag error.");
fd = open(path, oflag, mode);
if (fd == -1)
return vp_stack_return_error(&_result, "open() error: %s",
strerror(errno));
vp_stack_push_num(&_result, "%d", fd);
return vp_stack_return(&_result);
}
const char *
vp_file_close(char *args)
{
vp_stack_t stack;
int fd;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
if (close(fd) == -1)
return vp_stack_return_error(&_result, "close() error: %s",
strerror(errno));
return NULL;
}
const char *
vp_file_read(char *args)
{
return vp_fd_read(args, 0);
}
const char *
vp_file_write(char *args)
{
vp_stack_t stack;
int fd;
char *buf;
size_t size;
int timeout;
size_t nleft;
int n;
struct pollfd pfd = {0, POLLOUT, 0};
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &timeout));
size = vp_decode_size(stack.top);
buf = stack.top + VP_HEADER_SIZE;
pfd.fd = fd;
nleft = 0;
while (nleft < size) {
n = poll(&pfd, 1, timeout);
if (n == -1) {
return vp_stack_return_error(&_result, "poll() error: %s",
strerror(errno));
} else if (n == 0) {
/* timeout */
break;
}
if (pfd.revents & POLLOUT) {
n = write(fd, buf + nleft, size - nleft);
if (n == -1) {
return vp_stack_return_error(&_result, "write() error: %s",
strerror(errno));
}
nleft += n;
/* try write more bytes without waiting */
timeout = 0;
continue;
} else if (pfd.revents & (POLLERR | POLLHUP)) {
/* eof or error */
break;
} else if (pfd.revents & POLLNVAL) {
return vp_stack_return_error(&_result, "poll() POLLNVAL: %d",
pfd.revents);
}
/* DO NOT REACH HERE */
return vp_stack_return_error(&_result, "poll() unknown status: %s",
pfd.revents);
}
vp_stack_push_num(&_result, "%zu", nleft);
return vp_stack_return(&_result);
}
static void
close_allfd(int fds[3][2])
{
int i;
for (i = 0; i < 6; ++i) {
int fd = fds[i / 2][i % 2];
if (fd > 0) {
(void)close(fd);
}
}
}
const char *
vp_pipe_open(char *args)
{
#define VP_GOTO_ERROR(_fmt) do { errfmt = (_fmt); goto error; } while(0)
vp_stack_t stack;
int npipe, hstdin, hstderr, hstdout;
int argc;
int fd[3][2] = {{0}};
pid_t pid;
int dummy;
char *errfmt;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &npipe));
if (npipe != 2 && npipe != 3)
return vp_stack_return_error(&_result, "npipe range error. wrong pipes.");
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstdin));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstdout));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstderr));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &argc));
if (hstdin > 0) {
fd[0][0] = hstdin;
fd[0][1] = 0;
} else {
if (pipe(fd[0]) < 0) {
VP_GOTO_ERROR("pipe() error: %s");
}
}
if (hstdout > 0) {
fd[1][1] = hstdout;
fd[1][0] = 0;
} else {
if (pipe(fd[1]) < 0) {
VP_GOTO_ERROR("pipe() error: %s");
}
}
if (hstderr > 0) {
fd[2][1] = hstderr;
fd[2][0] = 0;
} else if (npipe == 3 && hstderr == 0) {
if (pipe(fd[2]) < 0) {
VP_GOTO_ERROR("pipe() error: %s");
}
}
pid = fork();
if (pid < 0) {
VP_GOTO_ERROR("fork() error: %s");
} else if (pid == 0) {
/* child */
char **argv;
int i;
/* Set process group. */
setpgid(0, 0);
if (fd[0][1] > 0) {
close(fd[0][1]);
}
if (fd[1][0] > 0) {
close(fd[1][0]);
}
if (fd[2][0] > 0) {
close(fd[2][0]);
}
if (fd[0][0] > 0) {
if (dup2(fd[0][0], STDIN_FILENO) != STDIN_FILENO) {
goto child_error;
}
close(fd[0][0]);
}
if (fd[1][1] > 0) {
if (dup2(fd[1][1], STDOUT_FILENO) != STDOUT_FILENO) {
goto child_error;
}
close(fd[1][1]);
}
if (fd[2][1] > 0) {
if (dup2(fd[2][1], STDERR_FILENO) != STDERR_FILENO) {
goto child_error;
}
close(fd[2][1]);
} else if (npipe == 2) {
if (dup2(STDOUT_FILENO, STDERR_FILENO) != STDERR_FILENO) {
goto child_error;
}
}
{
#ifndef TIOCNOTTY
setsid();
#else
/* Ignore tty. */
char name[L_ctermid];
if (ctermid(name)[0] != '\0') {
int tfd;
if ((tfd = open(name, O_RDONLY)) != -1) {
ioctl(tfd, TIOCNOTTY, NULL);
close(tfd);
}
}
#endif
}
argv = malloc(sizeof(char *) * (argc+1));
if (argv == NULL) {
goto child_error;
}
for (i = 0; i < argc; ++i) {
if (vp_stack_pop_str(&stack, &(argv[i]))) {
free(argv);
goto child_error;
}
}
argv[argc] = NULL;
execv(argv[0], argv);
/* error */
goto child_error;
} else {
/* parent */
if (fd[0][0] > 0) {
close(fd[0][0]);
}
if (fd[1][1] > 0) {
close(fd[1][1]);
}
if (fd[2][1] > 0) {
close(fd[2][1]);
}
vp_stack_push_num(&_result, "%d", pid);
vp_stack_push_num(&_result, "%d", fd[0][1]);
vp_stack_push_num(&_result, "%d", fd[1][0]);
if (npipe == 3) {
vp_stack_push_num(&_result, "%d", fd[2][0]);
}
return vp_stack_return(&_result);
}
/* DO NOT REACH HERE */
return NULL;
/* error */
error:
close_allfd(fd);
return vp_stack_return_error(&_result, errfmt, strerror(errno));
child_error:
dummy = write(STDOUT_FILENO, strerror(errno), strlen(strerror(errno)));
_exit(EXIT_FAILURE);
#undef VP_GOTO_ERROR
}
const char *
vp_pipe_close(char *args)
{
return vp_file_close(args);
}
const char *
vp_pipe_read(char *args)
{
return vp_fd_read(args, 1);
}
const char *
vp_pipe_write(char *args)
{
return vp_file_write(args);
}
const char *
vp_pty_open(char *args)
{
#define VP_GOTO_ERROR(_fmt) do { errfmt = (_fmt); goto error; } while(0)
vp_stack_t stack;
int argc;
int fd[3][2] = {{0}};
pid_t pid;
struct winsize ws = {0, 0, 0, 0};
int dummy;
int hstdin, hstderr, hstdout;
int fdm;
int npipe;
char *errfmt;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &npipe));
if (npipe != 2 && npipe != 3)
return vp_stack_return_error(&_result, "npipe range error. wrong pipes.");
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%hu", &(ws.ws_col)));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%hu", &(ws.ws_row)));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstdin));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstdout));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &hstderr));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &argc));
/* Set pipe */
if (hstdin > 0) {
fd[0][0] = hstdin;
fd[0][1] = 0;
}
if (hstdout > 1) {
fd[1][1] = hstdout;
fd[1][0] = 0;
} else if (hstdout == 1) {
if (pipe(fd[1]) < 0) {
VP_GOTO_ERROR("pipe() error: %s");
}
}
if (hstderr > 1) {
fd[2][1] = hstderr;
fd[2][0] = 0;
} else if (npipe == 3) {
if (hstderr == 1){
if (pipe(fd[2]) < 0) {
VP_GOTO_ERROR("pipe() error: %s");
}
} else if (hstderr == 0) {
if (openpty(&fd[2][0], &fd[2][1], NULL, NULL, &ws) < 0) {
VP_GOTO_ERROR("openpty() error: %s");
}
VP_SET_NONBLOCK_IF_NEEDED(fd[2][0]);
}
}
pid = forkpty(&fdm, NULL, NULL, &ws);
if (pid < 0) {
VP_GOTO_ERROR("fork() error: %s");
} else if (pid == 0) {
/* child */
char **argv;
int i;
/* Close pipe */
if (fd[1][0] > 0) {
close(fd[1][0]);
}
if (fd[2][0] > 0) {
close(fd[2][0]);
}
if (fd[0][0] > 0) {
if (dup2(fd[0][0], STDIN_FILENO) != STDIN_FILENO) {
goto child_error;
}
close(fd[0][0]);
}
if (fd[1][1] > 0) {
if (dup2(fd[1][1], STDOUT_FILENO) != STDOUT_FILENO) {
goto child_error;
}
close(fd[1][1]);
}
if (fd[2][1] > 0) {
if (dup2(fd[2][1], STDERR_FILENO) != STDERR_FILENO) {
goto child_error;
}
close(fd[2][1]);
}
argv = malloc(sizeof(char *) * (argc+1));
if (argv == NULL) {
goto child_error;
}
for (i = 0; i < argc; ++i) {
if (vp_stack_pop_str(&stack, &(argv[i]))) {
free(argv);
goto child_error;
}
}
argv[argc] = NULL;
execv(argv[0], argv);
/* error */
goto child_error;
} else {
/* parent */
if (fd[1][1] > 0) {
close(fd[1][1]);
}
if (fd[2][1] > 0) {
close(fd[2][1]);
}
if (hstdin == 0) {
fd[0][1] = fdm;
}
if (hstdout == 0) {
fd[1][0] = hstdin == 0 ? dup(fdm) : fdm;
VP_SET_NONBLOCK_IF_NEEDED(fd[1][0]);
}
vp_stack_push_num(&_result, "%d", pid);
vp_stack_push_num(&_result, "%d", fd[0][1]);
vp_stack_push_num(&_result, "%d", fd[1][0]);
if (npipe == 3) {
vp_stack_push_num(&_result, "%d", fd[2][0]);
}
return vp_stack_return(&_result);
}
/* DO NOT REACH HERE */
return NULL;
/* error */
error:
close_allfd(fd);
return vp_stack_return_error(&_result, errfmt, strerror(errno));
child_error:
dummy = write(STDOUT_FILENO, strerror(errno), strlen(strerror(errno)));
_exit(EXIT_FAILURE);
#undef VP_GOTO_ERROR
}
const char *
vp_pty_close(char *args)
{
return vp_file_close(args);
}
const char *
vp_pty_read(char *args)
{
return vp_fd_read(args, 1);
}
const char *
vp_pty_write(char *args)
{
return vp_file_write(args);
}
const char *
vp_pty_get_winsize(char *args)
{
vp_stack_t stack;
int fd;
struct winsize ws = {0, 0, 0, 0};
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return vp_stack_return_error(&_result, "ioctl() error: %s",
strerror(errno));
vp_stack_push_num(&_result, "%hu", ws.ws_col);
vp_stack_push_num(&_result, "%hu", ws.ws_row);
return vp_stack_return(&_result);
}
const char *
vp_pty_set_winsize(char *args)
{
vp_stack_t stack;
int fd;
struct winsize ws = {0, 0, 0, 0};
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &fd));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%hu", &(ws.ws_col)));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%hu", &(ws.ws_row)));
if (ioctl(fd, TIOCSWINSZ, &ws) < 0)
return vp_stack_return_error(&_result, "ioctl() error: %s",
strerror(errno));
return NULL;
}
const char *
vp_kill(char *args)
{
vp_stack_t stack;
pid_t pid, pgid;
int sig;
int ret;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &pid));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &sig));
ret = kill(pid, sig);
if (ret < 0)
return vp_stack_return_error(&_result, "kill() error: %s",
strerror(errno));
if (sig != 0) {
/* Kill by the process group. */
pgid = getpgid(pid);
if (pid == pgid) {
kill(-pgid, sig);
}
}
vp_stack_push_num(&_result, "%d", ret);
return vp_stack_return(&_result);
}
const char *
vp_waitpid(char *args)
{
vp_stack_t stack;
pid_t pid, pgid;
pid_t n;
int status;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_num(&stack, "%d", &pid));
n = waitpid(pid, &status, WNOHANG | WUNTRACED);
if (n == -1)
return vp_stack_return_error(&_result, "waitpid() error: %s",
strerror(errno));
if (n == 0 || WIFCONTINUED(status)) {
vp_stack_push_str(&_result, "run");
vp_stack_push_num(&_result, "%d", 0);
} else if (WIFEXITED(status)) {
/* Kill by the process group. */
pgid = getpgid(pid);
if (pgid > 0) {
kill(-pgid, 15);
}
vp_stack_push_str(&_result, "exit");
vp_stack_push_num(&_result, "%d", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
vp_stack_push_str(&_result, "signal");
vp_stack_push_num(&_result, "%d", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
vp_stack_push_str(&_result, "stop");
vp_stack_push_num(&_result, "%d", WSTOPSIG(status));
} else {
return vp_stack_return_error(&_result,
"waitpid() unknown status: status=%d", status);
}
return vp_stack_return(&_result);
}
/*
* This is based on socket.diff.gz written by Yasuhiro Matsumoto.
* see: http://marc.theaimsgroup.com/?l=vim-dev&m=105289857008664&w=2
*/
const char *
vp_socket_open(char *args)
{
vp_stack_t stack;
char *host;
char *port;
char *p;
int n;
unsigned short nport;
int sock;
struct sockaddr_in sockaddr;
struct hostent *hostent;
struct servent *servent;
VP_RETURN_IF_FAIL(vp_stack_from_args(&stack, args));
VP_RETURN_IF_FAIL(vp_stack_pop_str(&stack, &host));
VP_RETURN_IF_FAIL(vp_stack_pop_str(&stack, &port));
n = strtol(port, &p, 10);
if (p == port + strlen(port)) {
nport = htons(n);
} else {
servent = getservbyname(port, NULL);
if (servent == NULL)
return vp_stack_return_error(&_result, "getservbyname() error: %s",
port);
nport = servent->s_port;
}
sock = socket(PF_INET, SOCK_STREAM, 0);
hostent = gethostbyname(host);
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = nport;
sockaddr.sin_addr = *((struct in_addr*)*hostent->h_addr_list);
if (connect(sock, (struct sockaddr*)&sockaddr, sizeof(struct sockaddr_in))
== -1)
return vp_stack_return_error(&_result, "connect() error: %s",
strerror(errno));
vp_stack_push_num(&_result, "%d", sock);
return vp_stack_return(&_result);
}
const char *
vp_socket_close(char *args)
{
return vp_file_close(args);