2 * (c) Oleg Puchinin 2007
3 * graycardinalster@gmail.com
11 #include <gc_strings_low.h>
21 __export
void Dexec_init (struct __djob_t
* ctx
)
23 memset (ctx
, 0, sizeof (struct __djob_t
));
29 ctx
->pipe_out
[0] = -1;
30 ctx
->pipe_out
[1] = -1;
31 ctx
->pipe_err
[0] = -1;
32 ctx
->pipe_err
[1] = -1;
33 ctx
->otmp_name
= (char *) malloc (128);
34 ctx
->etmp_name
= (char *) malloc (128);
35 ctx
->otmp_name
[0] = '\0';
36 ctx
->etmp_name
[0] = '\0';
37 ctx
->shared_mem
= NULL
;
41 int Dexec_done (struct __djob_t
*ctx
)
47 free (ctx
->otmp_name
);
50 free (ctx
->etmp_name
);
53 munmap (ctx
->shared_mem
, ctx
->shm_size
);
55 fdclose (&ctx
->stdIn
);
56 fdclose (&ctx
->stdOut
);
57 fdclose (&ctx
->stdErr
);
62 void __dexec_init_pipes (__djob_t
* ctx
, uint32_t opts
)
66 if (opts
& DEXEC_IPIPE
)
68 if (opts
& DEXEC_OPIPE
)
70 if (opts
& DEXEC_EPIPE
)
74 void __dexec_parent (__djob_t
* ctx
, int opts
, char * cmd
)
80 if (opts
& DEXEC_IPIPE
)
81 fdclose (&ctx
->pipe_in
[0]);
82 if (opts
& DEXEC_OPIPE
)
83 fdclose (&ctx
->pipe_out
[1]);
84 if (opts
& DEXEC_EPIPE
)
85 fdclose (&ctx
->pipe_err
[1]);
86 if (opts
& DEXEC_WAIT
)
87 waitpid (ctx
->pid
, &status
, 0);
90 void __dexec_child (__djob_t
* ctx
, int opts
, char * cmd
)
97 if (opts
& DEXEC_IPIPE
) {
98 fdclose (&ctx
->pipe_in
[1]);
99 dup2 (ctx
->pipe_in
[0], fileno (stdin
));
102 if (opts
& DEXEC_OPIPE
) {
103 fdclose (&ctx
->pipe_out
[0]);
104 dup2 (ctx
->pipe_out
[1], fileno (stdout
));
107 if (opts
& DEXEC_EPIPE
) {
108 fdclose (&ctx
->pipe_err
[0]);
109 dup2 (ctx
->pipe_err
[1], fileno (stderr
));
112 if (opts
& DEXEC_INULL
) {
113 fd
= open ("/dev/null", O_RDONLY
);
114 dup2 (fd
, fileno (stdin
));
117 if (opts
& DEXEC_ONULL
) {
118 fd
= open ("/dev/null", O_WRONLY
);
119 dup2 (fd
, fileno (stdout
));
122 if (opts
& DEXEC_ENULL
) {
123 fd
= open ("/dev/null", O_WRONLY
);
124 dup2 (fd
, fileno (stderr
));
128 exit (execlp ("/bin/sh", "/bin/sh", "-c", cmd
, NULL
));
131 __export __djob_t
* Dexec (unsigned int opts
, char * cmd
)
137 ctx
= CNEW (__djob_t
, 1);
139 __dexec_init_pipes (ctx
, opts
);
141 if (opts
& DEXEC_OTMP
)
142 fdo
= Dtmpfd (ctx
->otmp_name
);
143 if (opts
& DEXEC_ETMP
)
144 fde
= Dtmpfd (ctx
->etmp_name
);
148 if (opts
& DEXEC_OTMP
) {
149 dup2 (fdo
, fileno (stdout
));
152 if (opts
& DEXEC_ETMP
) {
153 dup2 (fde
, fileno (stderr
));
156 __dexec_child (ctx
, opts
, cmd
);
157 } else if (ctx
->pid
> 0) {
160 __dexec_parent (ctx
, opts
, cmd
);