4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
28 * Runs a command without a window.
31 int cmd_run_shell_exec(struct cmd
*, struct cmd_ctx
*);
33 void cmd_run_shell_callback(struct job
*);
34 void cmd_run_shell_free(void *);
36 const struct cmd_entry cmd_run_shell_entry
= {
47 struct cmd_run_shell_data
{
53 cmd_run_shell_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
55 struct cmd_target_data
*data
= self
->data
;
56 struct cmd_run_shell_data
*cdata
;
59 cdata
= xmalloc(sizeof *cdata
);
60 cdata
->cmd
= xstrdup(data
->arg
);
61 memcpy(&cdata
->ctx
, ctx
, sizeof cdata
->ctx
);
63 if (ctx
->cmdclient
!= NULL
)
64 ctx
->cmdclient
->references
++;
65 if (ctx
->curclient
!= NULL
)
66 ctx
->curclient
->references
++;
68 job
= job_add(NULL
, 0, NULL
,
69 data
->arg
, cmd_run_shell_callback
, cmd_run_shell_free
, cdata
);
72 return (1); /* don't let client exit */
76 cmd_run_shell_callback(struct job
*job
)
78 struct cmd_run_shell_data
*cdata
= job
->data
;
79 struct cmd_ctx
*ctx
= &cdata
->ctx
;
80 char *cmd
, *msg
, *line
;
87 if ((line
= evbuffer_readline(job
->event
->input
)) != NULL
) {
88 ctx
->print(ctx
, "%s", line
);
91 } while (line
!= NULL
);
93 size
= EVBUFFER_LENGTH(job
->event
->input
);
95 line
= xmalloc(size
+ 1);
96 memcpy(line
, EVBUFFER_DATA(job
->event
->input
), size
);
99 ctx
->print(ctx
, "%s", line
);
108 if (WIFEXITED(job
->status
)) {
109 if ((retcode
= WEXITSTATUS(job
->status
)) != 0)
110 xasprintf(&msg
, "'%s' returned %d", cmd
, retcode
);
111 } else if (WIFSIGNALED(job
->status
)) {
112 retcode
= WTERMSIG(job
->status
);
113 xasprintf(&msg
, "'%s' terminated by signal %d", cmd
, retcode
);
117 ctx
->print(ctx
, "%s", msg
);
119 ctx
->info(ctx
, "%s", msg
);
125 cmd_run_shell_free(void *data
)
127 struct cmd_run_shell_data
*cdata
= data
;
128 struct cmd_ctx
*ctx
= &cdata
->ctx
;
130 if (ctx
->cmdclient
!= NULL
) {
131 ctx
->cmdclient
->references
--;
132 server_write_client(ctx
->cmdclient
, MSG_EXIT
, NULL
, 0);
134 if (ctx
->curclient
!= NULL
)
135 ctx
->curclient
->references
--;