| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2004, 2011 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <fcntl.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include <sys/wait.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include "ifile.h" |
| 25 |
#include "msg.h" |
| 26 |
|
| 27 |
char const * const ifile_modes[] = { [m_error] = "m_error", |
| 28 |
[m_read] = "m_read", |
| 29 |
[m_exec] = "m_exec" }; |
| 30 |
|
| 31 |
static int open_pipe(char const * s) |
| 32 |
{ |
| 33 |
int ps[2]; |
| 34 |
int r = pipe(ps); |
| 35 |
if (r == -1) { |
| 36 |
errno_msg(L_IMPORTANT, 0); |
| 37 |
return -1; |
| 38 |
} |
| 39 |
pid_t pid = fork(); |
| 40 |
switch (pid) { |
| 41 |
case -1: |
| 42 |
errno_msg(L_IMPORTANT, 0); |
| 43 |
close(ps[0]); |
| 44 |
close(ps[1]); |
| 45 |
return -1; |
| 46 |
case 0: |
| 47 |
// child |
| 48 |
close(STDIN_FILENO); |
| 49 |
r = dup2(ps[1], STDOUT_FILENO); |
| 50 |
if (r == -1) { |
| 51 |
fprintf(stderr, "%s (child): %s\n", |
| 52 |
get_progname(), strerror(errno)); |
| 53 |
_exit(1); |
| 54 |
} |
| 55 |
close(ps[0]); |
| 56 |
close(ps[1]); |
| 57 |
execl("/bin/sh", "/bin/sh", "-c", s, (char*)0); |
| 58 |
fprintf(stderr, _("%s (child): failed to exec /bin/sh: %s\n"), |
| 59 |
get_progname(), strerror(errno)); |
| 60 |
_exit(1); |
| 61 |
} |
| 62 |
// parent |
| 63 |
close(ps[1]); |
| 64 |
return ps[0]; |
| 65 |
} |
| 66 |
|
| 67 |
int open_ifile(struct ifile f) |
| 68 |
{ |
| 69 |
switch (f.mode) { |
| 70 |
case m_read: |
| 71 |
if (strcmp(f.s, "-") == 0) { |
| 72 |
return STDIN_FILENO; |
| 73 |
} |
| 74 |
{ |
| 75 |
int fd = open(f.s, O_RDONLY); |
| 76 |
if (fd == -1) { |
| 77 |
fprintf(stderr, "%s: %s: %s\n", |
| 78 |
get_progname(), f.s, strerror(errno)); |
| 79 |
record_error(); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
return fd; |
| 83 |
} |
| 84 |
case m_exec: |
| 85 |
return open_pipe(f.s); |
| 86 |
case m_error: |
| 87 |
abort(); |
| 88 |
} |
| 89 |
abort(); |
| 90 |
} |
| 91 |
|
| 92 |
void close_ifile(struct ifile f, int fd) |
| 93 |
{ |
| 94 |
close(fd); |
| 95 |
if (f.mode == m_exec) { |
| 96 |
int status; |
| 97 |
int r = wait(&status); |
| 98 |
if (r == -1) { |
| 99 |
errno_msg(L_IMPORTANT, f.s); |
| 100 |
return; |
| 101 |
} |
| 102 |
if (WIFEXITED(status)) { |
| 103 |
if (WEXITSTATUS(status) == 0) return; |
| 104 |
fprintf(stderr, _("%s: command (%s) failed " |
| 105 |
"(exit status %d)\n"), |
| 106 |
get_progname(), |
| 107 |
f.s, |
| 108 |
WEXITSTATUS(status)); |
| 109 |
record_error(); |
| 110 |
return; |
| 111 |
} |
| 112 |
if (WIFSIGNALED(status)) { |
| 113 |
fprintf(stderr, _("%s: command (%s) was killed " |
| 114 |
"by signal %d\n"), |
| 115 |
get_progname(), |
| 116 |
f.s, |
| 117 |
WTERMSIG(status)); |
| 118 |
record_error(); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
bool chk_ifile(struct ifile fname, int fd) |
| 125 |
{ |
| 126 |
struct stat stat; |
| 127 |
int r = fstat(fd, &stat); |
| 128 |
mode_t m = stat.st_mode; |
| 129 |
if (r == -1) { |
| 130 |
if (do_msg(L_IMPORTANT)) { |
| 131 |
fprintf(stderr, _("%s: %s: cannot stat: %s\n"), |
| 132 |
get_progname(), fname.s, strerror(errno)); |
| 133 |
} |
| 134 |
record_error(); |
| 135 |
close_ifile(fname, fd); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
if (!(S_ISREG(m) || S_ISCHR(m) || S_ISFIFO(m))) { |
| 139 |
if (do_msg(L_IMPORTANT)) { |
| 140 |
fprintf(stderr, "%s: %s: %s\n", |
| 141 |
get_progname(), fname.s, |
| 142 |
S_ISDIR(m) ? _("is a directory, skipping") : |
| 143 |
S_ISBLK(m) ? _("is a block device, skipping") : |
| 144 |
S_ISLNK(m) ? _("internal error") : |
| 145 |
S_ISSOCK(m) ? _("is a socket, skipping") : |
| 146 |
_("unknown file type, skipping")); |
| 147 |
} |
| 148 |
record_error(); |
| 149 |
close_ifile(fname, fd); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
return 1; |
| 153 |
} |