code style: change tabs to spaces

This commit is contained in:
Marek Marczykowski-Górecki 2013-12-27 05:31:11 +01:00
parent 6e47f12118
commit 0ba692c85a
2 changed files with 744 additions and 747 deletions

View File

@ -36,107 +36,107 @@ int replace_esc_stderr = 0;
int connect_unix_socket(const char *domname) int connect_unix_socket(const char *domname)
{ {
int s, len; int s, len;
struct sockaddr_un remote; struct sockaddr_un remote;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket"); perror("socket");
return -1; return -1;
} }
remote.sun_family = AF_UNIX; remote.sun_family = AF_UNIX;
snprintf(remote.sun_path, sizeof remote.sun_path, snprintf(remote.sun_path, sizeof remote.sun_path,
QREXEC_DAEMON_SOCKET_DIR "/qrexec.%s", domname); QREXEC_DAEMON_SOCKET_DIR "/qrexec.%s", domname);
len = strlen(remote.sun_path) + sizeof(remote.sun_family); len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *) &remote, len) == -1) { if (connect(s, (struct sockaddr *) &remote, len) == -1) {
perror("connect"); perror("connect");
exit(1); exit(1);
} }
return s; return s;
} }
void do_exec(const char *prog) void do_exec(const char *prog)
{ {
execl("/bin/bash", "bash", "-c", prog, NULL); execl("/bin/bash", "bash", "-c", prog, NULL);
} }
static int local_stdin_fd, local_stdout_fd; static int local_stdin_fd, local_stdout_fd;
void do_exit(int code) void do_exit(int code)
{ {
int status; int status;
// sever communication lines; wait for child, if any // sever communication lines; wait for child, if any
// so that qrexec-daemon can count (recursively) spawned processes correctly // so that qrexec-daemon can count (recursively) spawned processes correctly
close(local_stdin_fd); close(local_stdin_fd);
close(local_stdout_fd); close(local_stdout_fd);
waitpid(-1, &status, 0); waitpid(-1, &status, 0);
exit(code); exit(code);
} }
void prepare_local_fds(const char *cmdline) void prepare_local_fds(const char *cmdline)
{ {
int pid; int pid;
if (!cmdline) { if (!cmdline) {
local_stdin_fd = 1; local_stdin_fd = 1;
local_stdout_fd = 0; local_stdout_fd = 0;
return; return;
} }
do_fork_exec(cmdline, &pid, &local_stdin_fd, &local_stdout_fd, do_fork_exec(cmdline, &pid, &local_stdin_fd, &local_stdout_fd,
NULL); NULL);
} }
void send_cmdline(int s, int type, const char *cmdline) void send_cmdline(int s, int type, const char *cmdline)
{ {
struct client_header hdr; struct client_header hdr;
hdr.type = type; hdr.type = type;
hdr.len = strlen(cmdline) + 1; hdr.len = strlen(cmdline) + 1;
if (!write_all(s, &hdr, sizeof(hdr)) if (!write_all(s, &hdr, sizeof(hdr))
|| !write_all(s, cmdline, hdr.len)) { || !write_all(s, cmdline, hdr.len)) {
perror("write daemon"); perror("write daemon");
do_exit(1); do_exit(1);
} }
} }
void handle_input(int s) void handle_input(int s)
{ {
char buf[MAX_DATA_CHUNK]; char buf[MAX_DATA_CHUNK];
int ret; int ret;
ret = read(local_stdout_fd, buf, sizeof(buf)); ret = read(local_stdout_fd, buf, sizeof(buf));
if (ret < 0) { if (ret < 0) {
perror("read"); perror("read");
do_exit(1); do_exit(1);
} }
if (ret == 0) { if (ret == 0) {
close(local_stdout_fd); close(local_stdout_fd);
local_stdout_fd = -1; local_stdout_fd = -1;
shutdown(s, SHUT_WR); shutdown(s, SHUT_WR);
if (local_stdin_fd == -1) { if (local_stdin_fd == -1) {
// if pipe in opposite direction already closed, no need to stay alive // if pipe in opposite direction already closed, no need to stay alive
do_exit(0); do_exit(0);
} }
} }
if (!write_all(s, buf, ret)) { if (!write_all(s, buf, ret)) {
if (errno == EPIPE) { if (errno == EPIPE) {
// daemon disconnected its end of socket, so no future data will be // daemon disconnected its end of socket, so no future data will be
// send there; there is no sense to read from child stdout // send there; there is no sense to read from child stdout
// //
// since AF_UNIX socket is buffered it doesn't mean all data was // since AF_UNIX socket is buffered it doesn't mean all data was
// received from the agent // received from the agent
close(local_stdout_fd); close(local_stdout_fd);
local_stdout_fd = -1; local_stdout_fd = -1;
if (local_stdin_fd == -1) { if (local_stdin_fd == -1) {
// since child does no longer accept data on its stdin, doesn't // since child does no longer accept data on its stdin, doesn't
// make sense to process the data from the daemon // make sense to process the data from the daemon
// //
// we don't know real exit VM process code (exiting here, before // we don't know real exit VM process code (exiting here, before
// MSG_SERVER_TO_CLIENT_EXIT_CODE message) // MSG_SERVER_TO_CLIENT_EXIT_CODE message)
do_exit(1); do_exit(1);
} }
} else } else
perror("write daemon"); perror("write daemon");
} }
} }
void do_replace_esc(char *buf, int len) { void do_replace_esc(char *buf, int len) {
@ -149,170 +149,170 @@ void do_replace_esc(char *buf, int len) {
void handle_daemon_data(int s) void handle_daemon_data(int s)
{ {
int status; int status;
struct client_header hdr; struct client_header hdr;
char buf[MAX_DATA_CHUNK], *bufptr=buf; char buf[MAX_DATA_CHUNK], *bufptr=buf;
if (!read_all(s, &hdr, sizeof hdr)) { if (!read_all(s, &hdr, sizeof hdr)) {
perror("read daemon"); perror("read daemon");
do_exit(1); do_exit(1);
} }
if (hdr.len > MAX_DATA_CHUNK) { if (hdr.len > MAX_DATA_CHUNK) {
fprintf(stderr, "client_header.len=%d\n", hdr.len); fprintf(stderr, "client_header.len=%d\n", hdr.len);
do_exit(1); do_exit(1);
} }
if (!read_all(s, buf, hdr.len)) { if (!read_all(s, buf, hdr.len)) {
perror("read daemon"); perror("read daemon");
do_exit(1); do_exit(1);
} }
switch (hdr.type) { switch (hdr.type) {
case MSG_SERVER_TO_CLIENT_STDOUT: case MSG_SERVER_TO_CLIENT_STDOUT:
if (replace_esc_stdout) if (replace_esc_stdout)
do_replace_esc(buf, hdr.len); do_replace_esc(buf, hdr.len);
if (local_stdin_fd == -1) if (local_stdin_fd == -1)
break; break;
if (hdr.len == 0) { if (hdr.len == 0) {
close(local_stdin_fd); close(local_stdin_fd);
local_stdin_fd = -1; local_stdin_fd = -1;
} else if (!write_all(local_stdin_fd, buf, hdr.len)) { } else if (!write_all(local_stdin_fd, buf, hdr.len)) {
if (errno == EPIPE) { if (errno == EPIPE) {
// remote side have closed its stdin, handle data in oposite // remote side have closed its stdin, handle data in oposite
// direction (if any) before exit // direction (if any) before exit
local_stdin_fd = -1; local_stdin_fd = -1;
} else { } else {
perror("write local stdout"); perror("write local stdout");
do_exit(1); do_exit(1);
} }
} }
break; break;
case MSG_SERVER_TO_CLIENT_STDERR: case MSG_SERVER_TO_CLIENT_STDERR:
if (replace_esc_stderr) if (replace_esc_stderr)
do_replace_esc(buf, hdr.len); do_replace_esc(buf, hdr.len);
write_all(2, buf, hdr.len); write_all(2, buf, hdr.len);
break; break;
case MSG_SERVER_TO_CLIENT_EXIT_CODE: case MSG_SERVER_TO_CLIENT_EXIT_CODE:
status = *(unsigned int *) bufptr; status = *(unsigned int *) bufptr;
if (WIFEXITED(status)) if (WIFEXITED(status))
do_exit(WEXITSTATUS(status)); do_exit(WEXITSTATUS(status));
else else
do_exit(255); do_exit(255);
break; break;
default: default:
fprintf(stderr, "unknown msg %d\n", hdr.type); fprintf(stderr, "unknown msg %d\n", hdr.type);
do_exit(1); do_exit(1);
} }
} }
// perhaps we could save a syscall if we include both sides in both // perhaps we could save a syscall if we include both sides in both
// rdset and wrset; to be investigated // rdset and wrset; to be investigated
void handle_daemon_only_until_writable(int s) void handle_daemon_only_until_writable(int s)
{ {
fd_set rdset, wrset; fd_set rdset, wrset;
do { do {
FD_ZERO(&rdset); FD_ZERO(&rdset);
FD_ZERO(&wrset); FD_ZERO(&wrset);
FD_SET(s, &rdset); FD_SET(s, &rdset);
FD_SET(s, &wrset); FD_SET(s, &wrset);
if (select(s + 1, &rdset, &wrset, NULL, NULL) < 0) { if (select(s + 1, &rdset, &wrset, NULL, NULL) < 0) {
perror("select"); perror("select");
do_exit(1); do_exit(1);
} }
if (FD_ISSET(s, &rdset)) if (FD_ISSET(s, &rdset))
handle_daemon_data(s); handle_daemon_data(s);
} while (!FD_ISSET(s, &wrset)); } while (!FD_ISSET(s, &wrset));
} }
void select_loop(int s) void select_loop(int s)
{ {
fd_set select_set; fd_set select_set;
int max; int max;
for (;;) { for (;;) {
handle_daemon_only_until_writable(s); handle_daemon_only_until_writable(s);
FD_ZERO(&select_set); FD_ZERO(&select_set);
FD_SET(s, &select_set); FD_SET(s, &select_set);
max = s; max = s;
if (local_stdout_fd != -1) { if (local_stdout_fd != -1) {
FD_SET(local_stdout_fd, &select_set); FD_SET(local_stdout_fd, &select_set);
if (s < local_stdout_fd) if (s < local_stdout_fd)
max = local_stdout_fd; max = local_stdout_fd;
} }
if (select(max + 1, &select_set, NULL, NULL, NULL) < 0) { if (select(max + 1, &select_set, NULL, NULL, NULL) < 0) {
perror("select"); perror("select");
do_exit(1); do_exit(1);
} }
if (FD_ISSET(s, &select_set)) if (FD_ISSET(s, &select_set))
handle_daemon_data(s); handle_daemon_data(s);
if (local_stdout_fd != -1 if (local_stdout_fd != -1
&& FD_ISSET(local_stdout_fd, &select_set)) && FD_ISSET(local_stdout_fd, &select_set))
handle_input(s); handle_input(s);
} }
} }
void usage(const char *name) void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: %s -d domain_num [-l local_prog] -e -t -T -c remote_cmdline\n" "usage: %s -d domain_num [-l local_prog] -e -t -T -c remote_cmdline\n"
"-e means exit after sending cmd, -c: connect to existing process\n" "-e means exit after sending cmd, -c: connect to existing process\n"
"-t enables replacing ESC character with '_' in command output, -T is the same for stderr\n", "-t enables replacing ESC character with '_' in command output, -T is the same for stderr\n",
name); name);
exit(1); exit(1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int opt; int opt;
char *domname = NULL; char *domname = NULL;
int s; int s;
int just_exec = 0; int just_exec = 0;
int connect_existing = 0; int connect_existing = 0;
char *local_cmdline = NULL; char *local_cmdline = NULL;
while ((opt = getopt(argc, argv, "d:l:ectT")) != -1) { while ((opt = getopt(argc, argv, "d:l:ectT")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
domname = strdup(optarg); domname = strdup(optarg);
break; break;
case 'l': case 'l':
local_cmdline = strdup(optarg); local_cmdline = strdup(optarg);
break; break;
case 'e': case 'e':
just_exec = 1; just_exec = 1;
break; break;
case 'c': case 'c':
connect_existing = 1; connect_existing = 1;
break; break;
case 't': case 't':
replace_esc_stdout = 1; replace_esc_stdout = 1;
break; break;
case 'T': case 'T':
replace_esc_stderr = 1; replace_esc_stderr = 1;
break; break;
default: default:
usage(argv[0]); usage(argv[0]);
} }
} }
if (optind >= argc || !domname) if (optind >= argc || !domname)
usage(argv[0]); usage(argv[0]);
register_exec_func(&do_exec); register_exec_func(&do_exec);
s = connect_unix_socket(domname); s = connect_unix_socket(domname);
setenv("QREXEC_REMOTE_DOMAIN", domname, 1); setenv("QREXEC_REMOTE_DOMAIN", domname, 1);
prepare_local_fds(local_cmdline); prepare_local_fds(local_cmdline);
if (just_exec) if (just_exec)
send_cmdline(s, MSG_CLIENT_TO_SERVER_JUST_EXEC, send_cmdline(s, MSG_CLIENT_TO_SERVER_JUST_EXEC,
argv[optind]); argv[optind]);
else { else {
int cmd; int cmd;
if (connect_existing) if (connect_existing)
cmd = MSG_CLIENT_TO_SERVER_CONNECT_EXISTING; cmd = MSG_CLIENT_TO_SERVER_CONNECT_EXISTING;
else else
cmd = MSG_CLIENT_TO_SERVER_EXEC_CMDLINE; cmd = MSG_CLIENT_TO_SERVER_EXEC_CMDLINE;
send_cmdline(s, cmd, argv[optind]); send_cmdline(s, cmd, argv[optind]);
select_loop(s); select_loop(s);
} }
return 0; return 0;
} }

File diff suppressed because it is too large Load Diff