From: Karel Zak <kzak@redhat.com>
To: Rick van Rein <rick@openfortress.nl>
Cc: util-linux@vger.kernel.org
Subject: Re: PATCH: script: Introduced a streaming output command
Date: Mon, 10 Dec 2018 12:46:34 +0100 [thread overview]
Message-ID: <20181210114634.ee5jamwllimdmrdc@ws.net.home> (raw)
In-Reply-To: <5C090B46.5090200@openfortress.nl>
On Thu, Dec 06, 2018 at 12:43:02PM +0100, Rick van Rein wrote:
> Besides output to a file, this patch introduces commands for
> interactive streaming, such as over a network connection.
Nice idea ;-)
> A default "scriptstream" command may be locally installed with
> all the bells and whistles that make sense locally.
[...]
> #include <stdio.h>
> @@ -97,12 +100,16 @@ UL_DEBUG_DEFINE_MASKNAMES(script) =
> UL_DEBUG_EMPTY_MASKNAMES;
> #endif
>
> #define DEFAULT_TYPESCRIPT_FILENAME "typescript"
> +#define DEFAULT_TYPESCRIPT_COMMAND "scriptstream"
Do we really need the default command? ;-)
I think the example with nc(1) in the man page is good enough. We do
not need hardcode any path/name in the code.
> struct script_control {
> char *shell; /* shell to be executed */
> char *command; /* command to be executed */
> char *fname; /* output file path */
> FILE *typescriptfp; /* output file pointer */
> + int stream; /* output to streaming command */
It would be better to use one bit for this boolean,
unsigned int
stream :1
see and of the struct script_control where are already another
options.
[...]
> +static void wait_for_stream_cmd(struct script_control *ctl, int wait)
> +{
> + int options = wait ? 0 : WNOHANG;
> + if (ctl->stream && ctl->typescriptfp) {
> + DBG(MISC, ul_debug("closing stream subcommand"));
> + fclose (ctl->typescriptfp);
> + ctl->typescriptfp = NULL;
> + kill(ctl->streampid, SIGTERM);
> + }
> +
> + DBG(MISC, ul_debug("waiting for stream subcommand"));
> +
> + waitpid(ctl->streampid, &ctl->streamstatus, options);
> }
>
> static void write_output(struct script_control *ctl, char *obuf,
> @@ -483,6 +505,7 @@ static void handle_signal(struct script_control
> *ctl, int fd)
> || info.ssi_code == CLD_KILLED
> || info.ssi_code == CLD_DUMPED) {
> wait_for_child(ctl, 0);
> + wait_for_stream_cmd(ctl, 0);
if (ctl->stream)
wait_for_stream_cmd(ctl, 0);
I guess it would be more robust, than call wait_for_stream_cmd()
in all situations.
> ctl->poll_timeout = 10;
>
> /* In case of ssi_code is CLD_TRAPPED, CLD_STOPPED, or CLD_CONTINUED */
> @@ -519,6 +542,7 @@ static void handle_signal(struct script_control
> *ctl, int fd)
> static void do_io(struct script_control *ctl)
> {
> int ret, eof = 0;
> + int cmdio[2];
It's detail, but stream_pipe[] wold be more readable :-)
> time_t tvec = script_time((time_t *)NULL);
> enum {
> POLLFD_SIGNAL = 0,
> @@ -533,9 +557,33 @@ static void do_io(struct script_control *ctl)
> };
>
>
> - if ((ctl->typescriptfp =
> - fopen(ctl->fname, ctl->append ? "a" UL_CLOEXECSTR : "w"
> UL_CLOEXECSTR)) == NULL) {
> -
> + if (ctl->stream) {
> + if (pipe(cmdio) == -1) {
> + warn(_("pipe failed"));
> + fail(ctl);
> + exit(1);
> + }
> + ctl->streampid = fork();
> + switch (ctl->streampid) {
> + case -1: /* error */
> + warn(_("fork failed"));
> + fail(ctl);
> + exit(1);
> + case 0: /* child */
> + close(cmdio[1]);
> + dup2(cmdio[0],0);
> + execvp(ctl->streamcmd[0], ctl->streamcmd);
> + fprintf(stderr,_("streaming command failed: %s\r\n"),strerror(errno));
> + exit(1);
err(1, _("streaming command failed"));
err() seems better than fprintf() + strerror() + exit();
[...]
> + /* streaming may be ordered with option --stream or with argc > 1 */
> + if (argc == 0) {
> + if (ctl.stream) {
> + ctl.minicmd[0] = DEFAULT_TYPESCRIPT_COMMAND;
> + ctl.minicmd[1] = NULL;
> + ctl.streamcmd = &ctl.minicmd[0];
> + } else {
> + /* No default implementation to make this pluggable */
> + ctl.fname = DEFAULT_TYPESCRIPT_FILENAME;
> + die_if_link(&ctl);
> + }
> + } else if (argc == 1) {
> + if (ctl.stream) {
> + ctl.minicmd[0] = argv[0];
> + ctl.minicmd[1] = NULL;
> + ctl.streamcmd = &ctl.minicmd[0];
> + } else {
> + ctl.fname = argv[0];
> + }
> + } else {
> + /* implicit streaming for more than 1 command argument */
> + ctl.streamcmd = argv;
> + ctl.stream = 1;
> + ctl.flush = 1;
> }
Frankly, all the command-line semantic seems complicated and a little
bit like over-engineering. It would be nice to keep it simple and
stupid :)
script
script <filename>
script --stream <command> [arg ...]
The first two ways write to "typescript" or to <filename>. The last
writes to <command> [arg ...].
The options --stream should be no_argument (as you already have) and
<command> [arg ...] is all argv[] after argv += optind.
All you need is
struct script_control {
char **stream_argv;
...
};
main() {
argv += optind;
if (argc > 0) {
if (ctl.stream)
ctl.stream_argv = &argv[0];
else
ctl.fname = argv[0];
}
if (ctl.stream && !ctl.stream_argv)
err(EXIT_FAILURE, _("streaming command not specified"));
}
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
next prev parent reply other threads:[~2018-12-10 11:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-06 11:43 PATCH: script: Introduced a streaming output command Rick van Rein
2018-12-10 11:46 ` Karel Zak [this message]
2018-12-10 12:47 ` Rick van Rein
2018-12-10 14:47 ` Rick van Rein
2018-12-17 9:43 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181210114634.ee5jamwllimdmrdc@ws.net.home \
--to=kzak@redhat.com \
--cc=rick@openfortress.nl \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).