2 * Copyright (c) 2013-2014 Devin Teske <dteske@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
39 extern char **environ
;
40 static char cmdbuf
[CMDBUFMAX
] = "";
41 static char shellcmd
[PATH_MAX
] = PATH_SHELL
;
42 static char *shellcmd_argv
[6] = {
44 __DECONST(char *, "-c"),
46 __DECONST(char *, "--"),
52 * Spawn a sh(1) command. Writes the resulting process ID to the pid_t pointed
53 * at by `pid'. Returns a file descriptor (int) suitable for writing data to
54 * the spawned command (data written to file descriptor is seen as standard-in
55 * by the spawned sh(1) command). Returns `-1' if unable to spawn command.
57 * If cmd contains a single "%s" sequence, replace it with label if non-NULL.
60 shell_spawn_pipecmd(const char *cmd
, const char *label
, pid_t
*pid
)
64 posix_spawn_file_actions_t action
;
68 int stdin_pipe
[2] = { -1, -1 };
70 /* Populate argument array */
71 if (label
!= NULL
&& fmtcheck(cmd
, "%s") == cmd
)
72 len
= snprintf(cmdbuf
, CMDBUFMAX
, cmd
, label
);
74 len
= snprintf(cmdbuf
, CMDBUFMAX
, "%s", cmd
);
75 if (len
>= CMDBUFMAX
) {
76 warnx("%s:%d:%s: cmdbuf[%u] too small to hold cmd argument",
77 __FILE__
, __LINE__
, __func__
, CMDBUFMAX
);
81 /* Open a pipe to communicate with [X]dialog(1) */
82 if (pipe(stdin_pipe
) < 0)
83 err(EXIT_FAILURE
, "%s: pipe(2)", __func__
);
85 /* Fork sh(1) process */
87 fprintf(stderr
, "%s: spawning `", __func__
);
88 for (i
= 0; shellcmd_argv
[i
] != NULL
; i
++) {
90 fprintf(stderr
, "%s", shellcmd_argv
[i
]);
92 fprintf(stderr
, " '%s'", shellcmd_argv
[i
]);
94 fprintf(stderr
, " %s", shellcmd_argv
[i
]);
96 fprintf(stderr
, "'\n");
98 posix_spawn_file_actions_init(&action
);
99 posix_spawn_file_actions_adddup2(&action
, stdin_pipe
[0], STDIN_FILENO
);
100 posix_spawn_file_actions_addclose(&action
, stdin_pipe
[1]);
101 error
= posix_spawnp(pid
, shellcmd
, &action
,
102 (const posix_spawnattr_t
*)NULL
, shellcmd_argv
, environ
);
104 err(EXIT_FAILURE
, "%s: posix_spawnp(3)", __func__
);
106 return stdin_pipe
[1];