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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2013. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Module: run_erl.c
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
/* System includes */
#include <aio.h>
#include <errno.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
/* OSE includes */
#include "ose.h"
#include "ose_spi/ose_spi.h"
#include "efs.h"
#include "pm.h"
#include "ose_spi/fm.sig"
/* erts includes */
#include "run_erl.h"
#include "run_erl_common.h"
#include "safe_string.h" /* sn_printf, strn_cpy, strn_cat, etc */
typedef struct RunErlSetup_ {
SIGSELECT signo;
int run_daemon;
char *logdir;
char *command;
char *pipename;
char *blockname;
} RunErlSetup;
typedef struct ProgramState_ {
/* child process */
int ifd, ofd;
OSDOMAIN domain;
PROCESS progpid, mainbid;
struct PmProgramInfo *info;
/* to_erl */
char w_pipe[FILENAME_BUFSIZ],
r_pipe[FILENAME_BUFSIZ];
} ProgramState;
union SIGNAL {
SIGSELECT signo;
RunErlSetup setup;
struct FmReadPtr fm_read_ptr;
struct FmWritePtr fm_write_ptr;
};
static OSBOOLEAN hunt_in_block(char *block_name,
char *process_name,
PROCESS *pid);
static int create_child_process(char *command_string, char *blockname,
ProgramState *state);
static OSBOOLEAN hunt_in_block(char *block_name,
char *process_name,
PROCESS *pid) {
struct OS_pid_list *list;
PROCESS block_id = OSE_ILLEGAL_PROCESS;
int i;
char *name;
*pid = OSE_ILLEGAL_PROCESS;
list = get_bid_list(0);
if (!list)
return 0;
for (i = 0; i < list->count; i++) {
if (list->list[i] == get_bid(current_process()))
continue;
name = (char*)get_pid_info(list->list[i], OSE_PI_NAME);
if (name) {
if (strcmp(name,block_name) == 0) {
block_id = list->list[i];
free_buf((union SIGNAL**)&name);
break;
}
free_buf((union SIGNAL**)&name);
}
}
free_buf((union SIGNAL**)&list);
if (block_id == OSE_ILLEGAL_PROCESS)
return 0;
list = get_pid_list(block_id);
if (!list)
return 0;
for (i = 0; i < list->count; i++) {
name = (char*)get_pid_info(list->list[i], OSE_PI_NAME);
if (name) {
if (strcmp(name,process_name) == 0) {
*pid = list->list[i];
free_buf((union SIGNAL**)&name);
break;
}
free_buf((union SIGNAL**)&name);
}
}
free_buf((union SIGNAL**)&list);
if (*pid == OSE_ILLEGAL_PROCESS)
return 0;
return 1;
}
static int create_child_process(char *command_string, char *blockname,
ProgramState *state) {
char *command = command_string;
char *argv;
int i = 0;
int ret_status;
PmStatus pm_status;
int tmp_io[2];
int fd_arr[3];
int ifd[2], ofd[2];
char *handle;
struct PmLoadModuleInfoReply *mod_info;
/* Parse out cmd and argv from the command string */
while (1) {
if (command[i] == ' ' || command[i] == '\0') {
if (command[i] == '\0')
argv = NULL;
else {
command[i] = '\0';
argv = command_string + i + 1;
}
break;
}
i++;
}
if (blockname)
handle = blockname;
else
handle = simple_basename(command);
if (ose_pm_load_module_info(handle,&mod_info) == PM_SUCCESS) {
/* Already installed */
free_buf((union SIGNAL**)&mod_info);
} else if ((pm_status = ose_pm_install_load_module(0,"ELF",command,handle,0,0,NULL))
!= PM_SUCCESS) {
ERROR1(LOG_ERR,"ose_pm_install_load_module failed - pmstatus: 0x%08x\n",
pm_status);
return 0;
}
state->domain = PM_NEW_DOMAIN;
pm_status = ose_pm_create_program(&state->domain, handle, 0, 0 , NULL,
&state->progpid, &state->mainbid);
if (pm_status != PM_SUCCESS) {
if (pm_status == PM_EINSTALL_HANDLE_IN_USE)
ERROR1(LOG_ERR,"ose_pm_create_program failed - "
"install handle \"%s\" is in use. You can specify another "
"install handle by using the -block option to run_erl.\n",handle);
else
ERROR1(LOG_ERR,"ose_pm_create_program failed - pmstatus: 0x%08x\n",
pm_status);
return 0;
}
pm_status = ose_pm_program_info(state->progpid, &state->info);
/* FIXME don't forget to free this ((union SIGNAL **)&info) */
if (pm_status != PM_SUCCESS) {
ERROR1(LOG_ERR,"ose_pm_program_info failed - pmstatus: 0x%08x\n",
pm_status);
return 0;
}
/* We only clone stdin+stdout, what about stderr? */
/* create pipes */
if (pipe(ifd) < 0) {
if (errno == ENOENT)
ERRNO_ERR0(LOG_ERR,"The /pipe file system is not available\n");
else
ERRNO_ERR0(LOG_ERR,"pipe ifd failed\n");
return 0;
}
if (pipe(ofd) < 0) {
ERRNO_ERR0(LOG_ERR,"pipe ofd failed\n");
return 0;
}
/* FIXME Lock? */
/* backup our stdin stdout */
if ((tmp_io[0] = dup(0)) < 0) {
ERRNO_ERR0(LOG_ERR,"dup 0 failed\n");
return 0;
}
if ((tmp_io[1] = dup(1)) < 0) {
ERRNO_ERR0(LOG_ERR,"dup 1 failed\n");
return 0;
}
/* set new pipe to fd 0,1 */
if (dup2(ifd[1], 1) < 0) {
ERRNO_ERR0(LOG_ERR,"dup2 1 failed\n");
return 0;
}
if (dup2(ofd[0], 0) < 0) {
ERRNO_ERR0(LOG_ERR,"dup2 0 failed\n");
return 0;
}
/* clone array to newly created */
fd_arr[0] = 2; /* Number of fd's */
fd_arr[1] = 0;
fd_arr[2] = 1;
if ((ret_status = efs_clone_array(state->info->main_process, fd_arr))
!= EFS_SUCCESS) {
ERROR1(LOG_ERR,"efs_close_array filed, errcode: %d\n", ret_status);
return 0;
}
if (dup2(tmp_io[1], 1) < 0) {
ERRNO_ERR0(LOG_ERR,"restoring dup2 1 failed\n");
return 0;
}
if (dup2(tmp_io[0], 0) < 0) {
ERRNO_ERR0(LOG_ERR,"restoring dup2 1 failed\n");
return 0;
}
/* close loose-ends */
sf_close(tmp_io[0]);
sf_close(tmp_io[1]);
sf_close(ifd[1]);
sf_close(ofd[0]);
state->ifd = ifd[0];
state->ofd = ofd[1];
if (argv && set_env(state->progpid, "ARGV", argv)) {
ERRNO_ERR0(LOG_ERR,"something went wrong with set_env\n");
}
/*
* Start the program.
*/
pm_status = ose_pm_start_program(state->progpid);
if (pm_status != PM_SUCCESS) {
ERROR1(LOG_ERR,"ose_pm_install_load_module failed - pmstatus: 0x%08x\n",
pm_status);
return 0;
}
return 1;
}
#define SET_AIO(REQ,FD,SIZE,BUFF) \
/* Make sure to clean data structure of previous request */ \
memset(&(REQ),0,sizeof(REQ)); \
(REQ).aio_fildes = FD; \
(REQ).aio_offset = FM_POSITION_CURRENT; \
(REQ).aio_nbytes = SIZE; \
(REQ).aio_buf = BUFF; \
(REQ).aio_sigevent.sigev_notify = SIGEV_NONE
#define READ_AIO(REQ,FD,SIZE,BUFF) do { \
SET_AIO(REQ,FD,SIZE,BUFF); \
if (aio_read(&(REQ)) != 0) \
ERRNO_ERR1(LOG_ERR,"aio_read of child_read_req(%d) failed\n",FD); \
} while (0)
#define WRITE_AIO(FD,SIZE,BUFF) do { \
struct aiocb *write_req = malloc(sizeof(struct aiocb)); \
char *write_buff = malloc(sizeof(char)*SIZE); \
memcpy(write_buff,BUFF,SIZE); \
SET_AIO(*write_req,FD,SIZE,write_buff); \
if (aio_write(write_req) != 0) \
ERRNO_ERR1(LOG_ERR,"aio_write of write_req(%d) failed\n",FD); \
} while(0)
int pass_on(ProgramState *state);
int pass_on(ProgramState *s) {
SIGSELECT sigsel[] = {0,FM_READ_PTR_REPLY};
union SIGNAL *sig;
char child_read_buff[BUFSIZ], pipe_read_buff[BUFSIZ];
struct aiocb child_read_req, pipe_read_req;
int rfd, wfd = 0;
FmHandle rfh, child_rfh;
int outstanding_writes = 0, got_some = 0, child_done = 0;
if ((rfd = sf_open(s->r_pipe, O_RDONLY, 0)) < 0) {
ERRNO_ERR1(LOG_ERR,"Could not open FIFO '%s' for reading.\n", s->r_pipe);
rfd = 0;
return 1;
}
attach(NULL,s->progpid);
/* Open the log file */
erts_run_erl_log_open();
efs_examine_fd(rfd,FLIB_FD_HANDLE,&rfh);
efs_examine_fd(s->ifd,FLIB_FD_HANDLE,&child_rfh);
READ_AIO(child_read_req,s->ifd,BUFSIZ,child_read_buff);
READ_AIO(pipe_read_req,rfd,BUFSIZ,pipe_read_buff);
while (1) {
time_t now,last_activity;
time(&last_activity);
sig = receive_w_tmo(erts_run_erl_log_alive_minutes*60000,sigsel);
time(&now);
if (sig) {
erts_run_erl_log_activity(0,now,last_activity);
} else {
/* timeout */
erts_run_erl_log_activity(1,now,last_activity);
continue;
}
switch (sig->signo) {
case OS_ATTACH_SIG: {
if (rfd) { sf_close(rfd); rfd = 0; }
free_buf(&sig);
child_done = 1;
/* Make sure to to let all outstanding write request finish */
if (outstanding_writes)
break;
if (wfd) sf_close(wfd);
return 0;
}
case FM_WRITE_PTR_REPLY: {
if (sig->fm_write_ptr.status == EFS_SUCCESS) {
if (sig->fm_write_ptr.actual < sig->fm_write_ptr.requested) {
WRITE_AIO(wfd, sig->fm_write_ptr.requested-sig->fm_write_ptr.actual,
sig->fm_write_ptr.buffer+sig->fm_write_ptr.actual);
}
} else {
/* Assume to_erl has terminated. */
sf_close(wfd);
wfd = 0;
}
free((char*)sig->fm_write_ptr.buffer);
aio_dispatch(sig);
if ((--outstanding_writes == 0) && child_done) {
if (wfd) sf_close(wfd);
return 0;
}
break;
}
case FM_READ_PTR_REPLY: {
/* Child fd */
if (sig->fm_read_ptr.handle == child_rfh) {
/* Child terminated */
if (sig->fm_read_ptr.status != EFS_SUCCESS ||
sig->fm_read_ptr.actual == 0) {
if (rfd) { sf_close(rfd); rfd = 0; }
if (sig->fm_read_ptr.status != EFS_SUCCESS) {
ERROR0(LOG_ERR,"Erlang closed the connection.");
aio_dispatch(sig);
return 1;
}
/* child closed connection gracefully */
aio_dispatch(sig);
if (outstanding_writes) {
child_done = 1;
break;
}
if (wfd) sf_close(wfd);
return 0;
} else {
erts_run_erl_log_write(sig->fm_read_ptr.buffer,
sig->fm_read_ptr.actual);
if (wfd) {
WRITE_AIO(wfd, sig->fm_read_ptr.actual, sig->fm_read_ptr.buffer);
outstanding_writes++;
}
aio_dispatch(sig);
READ_AIO(child_read_req, s->ifd,BUFSIZ, child_read_buff);
}
/* pipe fd */
} else if (sig->fm_read_ptr.handle == rfh) {
if (sig->fm_read_ptr.status != EFS_SUCCESS) {
if(rfd) sf_close(rfd);
if(wfd) sf_close(wfd);
aio_dispatch(sig);
ERRNO_ERR0(LOG_ERR,"Error in reading from FIFO.");
return 1;
}
if (sig->fm_read_ptr.actual == 0) {
/* to_erl closed its end of the pipe */
aio_dispatch(sig);
sf_close(rfd);
rfd = sf_open(s->r_pipe,O_RDONLY|DONT_BLOCK_PLEASE, 0);
if (rfd < 0) {
ERRNO_ERR1(LOG_ERR,"Could not open FIFO '%s' for reading.",
s->r_pipe);
rfd = 0;
} else {
READ_AIO(pipe_read_req,rfd,BUFSIZ,pipe_read_buff);
}
got_some = 0; /* reset for next session */
} else {
int len = sig->fm_read_ptr.actual;
char *buffer = sig->fm_read_ptr.buffer;
if (!wfd) {
/* Try to open the write pipe to to_erl. Now that we got some data
* from to_erl, to_erl should already be reading this pipe - open
* should succeed. But in case of error, we just ignore it.
*/
if ((wfd = sf_open(s->w_pipe, O_WRONLY|DONT_BLOCK_PLEASE, 0)) < 0) {
erts_run_erl_log_status("Client expected on FIFO %s, "
"but can't open (len=%d)\n",
s->w_pipe, sig->fm_read_ptr.actual);
sf_close(rfd);
rfd = sf_open(s->r_pipe, O_RDONLY|DONT_BLOCK_PLEASE, 0);
if (rfd < 0) {
ERRNO_ERR1(LOG_ERR,"Could not open FIFO '%s' for reading.",
s->r_pipe);
return 1;
}
wfd = 0;
} else {
#ifdef DEBUG
erts_run_erl_log_status("run_erl: %s opened for writing\n",
s->w_pipe);
#endif
}
}
if (!got_some && wfd && buffer[0] == '\014') {
char wbuf[30];
int wlen = sn_printf(wbuf,sizeof(wbuf),"[run_erl v%u-%u]\n",
RUN_ERL_HI_VER, RUN_ERL_LO_VER);
/* For some reason this, the first write aio seems to
not get an FM_WRITE_PTR_REPLY, so we do not do:
outstanding_writes++;
*/
WRITE_AIO(wfd, wlen, wbuf);
}
got_some = 1;
/* Write the message */
#ifdef DEBUG
erts_run_erl_log_status("Pty master write; ");
#endif
len = erts_run_erl_extract_ctrl_seq(buffer,len);
if (len > 0) {
int wlen = erts_run_erl_write_all(s->ofd, buffer, len);
if (wlen != len) {
aio_dispatch(sig);
ERRNO_ERR0(LOG_ERR,"Error in writing to terminal.");
if(rfd) sf_close(rfd);
if(wfd) sf_close(wfd);
return 1;
}
}
#ifdef DEBUG
erts_run_erl_log_status("OK\n");
#endif
aio_dispatch(sig);
READ_AIO(pipe_read_req,rfd,BUFSIZ,pipe_read_buff);
}
}
break;
}
default: {
free_buf(&sig);
break;
}
}
}
}
OS_PROCESS(run_erl_process) {
char *logdir, *command, *blockname;
SIGSELECT sigsel[] = {1,ERTS_SIGNAL_RUN_ERL_SETUP};
union SIGNAL *sig = receive(sigsel);
ProgramState state;
char pipename[FILENAME_BUFSIZ];
state.info = NULL;
logdir = strdup(sig->setup.logdir);
command = strdup(sig->setup.command);
strn_cpy(pipename,sizeof(pipename),sig->setup.pipename);
if (sig->setup.blockname)
blockname = strdup(sig->setup.blockname);
else
blockname = NULL;
erts_run_erl_log_init(sig->setup.run_daemon, logdir);
free_buf(&sig);
if (erts_run_erl_open_fifo(pipename,state.w_pipe,state.r_pipe))
kill_proc(current_process());
if (create_child_process(command,blockname,&state))
pass_on(&state);
free(logdir);
free(command);
if (blockname)
free(blockname);
if (state.info)
free_buf(((union SIGNAL**)&state.info));
sf_close(state.ifd);
sf_close(state.ofd);
unlink(state.w_pipe);
unlink(state.r_pipe);
kill_proc(current_process());
}
int run_erl(int argc,char **argv) {
char *pipename, *logdir, *command, *blockname = NULL;
int pipename_len, logdir_len, command_len, blockname_len = 0;
int i = 1, run_daemon = 0;
PROCESS pid;
SIGSELECT sigsel[] = {0};
union SIGNAL *sig;
if(argc < 4) {
fprintf(stderr,RUN_ERL_USAGE,"run_erl");
return 1;
}
while (1) {
if (argv[i][0] != '-')
break;
if (!strcmp(argv[i],"-daemon")) {
run_daemon = 1;
i++;
continue;
}
if (!strcmp(argv[i],"-block")) {
blockname = argv[i+1];
blockname_len = strlen(argv[i+1]) + 1;
i+=2;
continue;
}
fprintf(stderr,RUN_ERL_USAGE,"run_erl");
return 1;
}
pipename = argv[i++];
logdir = argv[i++];
command = argv[i++];
/* + 1 to include NULL at end */
logdir_len = strlen(logdir) + 1;
command_len = strlen(command) + 1;
pipename_len = strlen(pipename) + 1;
if (run_daemon) {
/* We request that the run_erl_process should be started from the
main process so that it does not die when the shell command
returns */
PROCESS main_pid;
hunt_in_block("run_erl","main",&main_pid);
sig = alloc(sizeof(sig),ERTS_SIGNAL_RUN_ERL_DAEMON);
send(&sig,main_pid);
sig = receive(sigsel);
pid = sender(&sig);
free_buf(&sig);
} else {
pid = create_process(OS_BG_PROC,"run_erl_process",
run_erl_process, 0x800,
0, 0, 0, NULL, 0, 0);
}
sig = alloc(sizeof(RunErlSetup)+
logdir_len+command_len+pipename_len+blockname_len,
ERTS_SIGNAL_RUN_ERL_SETUP);
sig->setup.run_daemon = run_daemon;
sig->setup.logdir = ((char*)sig)+sizeof(RunErlSetup);
sig->setup.command = ((char*)sig)+sizeof(RunErlSetup)+logdir_len;
sig->setup.pipename = ((char*)sig)+sizeof(RunErlSetup)+logdir_len+command_len;
if (blockname)
sig->setup.blockname = ((char*)sig)+sizeof(RunErlSetup)+
logdir_len+command_len+pipename_len;
else
sig->setup.blockname = NULL;
strcpy(sig->setup.logdir,logdir);
strcpy(sig->setup.command,command);
strcpy(sig->setup.pipename,pipename);
if (blockname) strcpy(sig->setup.blockname,blockname);
send(&sig,pid);
if (run_daemon) {
/* We are a daemon, error msgs will be sent to ramlog */
start(pid);
return 1;
}
/* We are not daemon, error msgs will be sent to stderr and we block here */
efs_clone(pid);
start(pid);
attach(NULL,pid);
sig = receive(sigsel);
return 1;
}
|