[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r30847 - msh/src
From: |
gnunet |
Subject: |
[GNUnet-SVN] r30847 - msh/src |
Date: |
Fri, 22 Nov 2013 14:13:06 +0100 |
Author: harsha
Date: 2013-11-22 14:13:06 +0100 (Fri, 22 Nov 2013)
New Revision: 30847
Modified:
msh/src/Makefile.am
msh/src/msh.c
msh/src/mshd.c
msh/src/server.c
Log:
- fix protocol
Modified: msh/src/Makefile.am
===================================================================
--- msh/src/Makefile.am 2013-11-22 09:15:07 UTC (rev 30846)
+++ msh/src/Makefile.am 2013-11-22 13:13:06 UTC (rev 30847)
@@ -8,7 +8,7 @@
ttymodes.h ttymodes.c
mshd_LDADD = -lgnunetutil -lm
-msh_SOURCES = msh.c mtypes.h
+msh_SOURCES = msh.c mtypes.h ttymodes.c ttymodes.h
msh_LDADD = -lgnunetutil util.$(OBJEXT)
check_PROGRAMS = \
Modified: msh/src/msh.c
===================================================================
--- msh/src/msh.c 2013-11-22 09:15:07 UTC (rev 30846)
+++ msh/src/msh.c 2013-11-22 13:13:06 UTC (rev 30847)
@@ -214,6 +214,11 @@
static int need_pty;
/**
+ * Did we set our terminal to raw mode
+ */
+static int tty_rawed;
+
+/**
* Destroys a connection context
*
* @param ctx connection context
@@ -261,6 +266,17 @@
LOG_DEBUG ("Shutting down\n");
destroy_conn_ctx (&ctx_local);
destroy_conn_ctx (&ctx_remote);
+ if ( can_pty && tty_rawed )
+ {
+ if (tcsetattr(0, TCSADRAIN, &tio) == -1)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "tcsetattr");
+ LOG_ERROR ("Failed to set terminal back from raw mode. "
+ "The terminal may behave wierdly\n");
+ }
+ }
+ if (NULL != fh_stdin)
+ GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh_stdin));
}
@@ -384,7 +400,6 @@
task_fwd_stdin = GNUNET_SCHEDULER_NO_TASK;
if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
{
- GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh_stdin));
return;
}
size = GNUNET_DISK_file_read (fh_stdin, recv_buf,
@@ -393,14 +408,11 @@
if (GNUNET_SYSERR == size)
{
GNUNET_break (0);
- GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh_stdin));
GNUNET_SCHEDULER_shutdown ();
return;
}
if (0 == size)
{
- GNUNET_DISK_file_close (fh_stdin);
- fh_stdin = NULL;
GNUNET_SCHEDULER_shutdown ();
return;
}
@@ -472,6 +484,94 @@
/**
+ * Set the terminal into raw mode
+ */
+void
+enter_raw_mode(int quiet)
+{
+ struct termios raw_tio;
+
+ raw_tio = tio;
+ raw_tio.c_iflag |= IGNPAR;
+ raw_tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
+#ifdef IUCLC
+ raw_tio.c_iflag &= ~IUCLC;
+#endif
+ raw_tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
+#ifdef IEXTEN
+ raw_tio.c_lflag &= ~IEXTEN;
+#endif
+ raw_tio.c_oflag &= ~OPOST;
+ raw_tio.c_cc[VMIN] = 1;
+ raw_tio.c_cc[VTIME] = 0;
+ if (tcsetattr(fileno(stdin), TCSADRAIN, &raw_tio) == -1) {
+ if (!quiet)
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "tcsetattr");
+ } else
+ tty_rawed = 1;
+}
+
+
+/**
+ * Generate PTY_MODE message containing the settings to be used for the created
+ * pseudo-tty
+ *
+ * @return the PTY_MODE message; NULL upon error
+ */
+static struct MSH_MSG_PtyMode *
+gen_pty_msg ()
+{
+ struct MSH_MSG_PtyMode *tmsg;
+ char *cp;
+ uint16_t *ts;
+ size_t size;
+ unsigned int count;
+
+ cp = getenv ("TERM");
+
+ /* count the number of terminal settings */
+ count = 0;
+#define TTYCHAR(NAME,OP) \
+ count++;
+#define TTYMODE(NAME,FIELD,OP) \
+ count++;
+#include "ttymodes.h"
+#undef TTYCHAR
+#undef TTYMODE
+
+ /* Build the terminal mode message */
+ size = sizeof (struct MSH_MSG_PtyMode) +
+ (sizeof (uint16_t) * 2 * count) + ((NULL != cp) ? (strlen(cp) + 1) : 0);
+ tmsg = GNUNET_malloc (size);
+ tmsg->header.type = htons (MSH_MTYPE_PTY_MODE);
+ tmsg->header.size = htons (size);
+ tmsg->ws_row = htons (ws.ws_row);
+ tmsg->ws_col = htons (ws.ws_col);
+ tmsg->ws_xpixel = htons (ws.ws_xpixel);
+ tmsg->ws_ypixel = htons (ws.ws_ypixel);
+ tmsg->ospeed = htonl (speed_to_baud (cfgetospeed (&tio)));
+ tmsg->ispeed = htonl (speed_to_baud (cfgetispeed (&tio)));
+ tmsg->nsettings = htons (count);
+ /* Populate the message with settings */
+ ts = (uint16_t *) &tmsg[1];
+#define TTYCHAR(NAME,OP) \
+ *(ts++) = htons (OP); \
+ *(ts++) = htons (tio.c_cc[NAME]);
+
+#define TTYMODE(NAME, FIELD, OP) \
+ *(ts++) = htons (OP); \
+ *(ts++) = htons ((tio.FIELD & NAME) != 0);
+#include "ttymodes.h"
+#undef TTYCHAR
+#undef TTYMODE
+ /* copy the TERM env variable's value */
+ if (NULL != cp)
+ (void) memcpy ((void *)ts, cp, strlen (cp));
+ return tmsg;
+}
+
+
+/**
* Type of a function to call when we receive a message
* from the service.
*
@@ -484,11 +584,27 @@
const struct GNUNET_MessageHeader *msg_)
{
struct GNUNET_MessageHeader *msg;
-
+ struct MSH_MSG_RunCmd *rmsg;
+ struct MSH_MSG_PtyMode *ptymsg;
+ uint16_t size;
+
LOG_DEBUG ("Received CHALLENGE_RESPONSE. Forwarding it to remote MSHD\n");
msg = GNUNET_copy_message (msg_);
queue_message (&ctx_remote, msg);
destroy_conn_ctx (&ctx_local);
+ if (can_pty && !disable_pty)
+ {
+ ptymsg = gen_pty_msg ();
+ queue_message (&ctx_remote, &ptymsg->header);
+ enter_raw_mode (1);
+ }
+ /* now queue RUN_CMD message */
+ size = sizeof (struct MSH_MSG_RunCmd) + cmdstr_len;
+ rmsg = GNUNET_malloc (size);
+ rmsg->header.size = htons (size);
+ rmsg->header.type = htons (MSH_MTYPE_RUNCMD);
+ (void) memcpy (rmsg->cmd, cmdstr, cmdstr_len);
+ queue_message (&ctx_remote, &rmsg->header);
return GNUNET_OK;
}
@@ -542,7 +658,7 @@
msg->header.size = htons (size);
msg->header.type = htons (MSH_MTYPE_SESSION_OPEN);
//(void) memcpy (msg->cmd, cmdstr, cmdstr_len);
- if (!disable_pty && can_pty)
+ if (can_pty)
msg->type = htonl (MSH_SESSION_TYPE_INTERACTIVE);
else
msg->type = htonl (MSH_SESSION_TYPE_NONINTERACTIVE);
@@ -684,7 +800,7 @@
static int
read_tty_settings ()
{
- if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
+ if (ioctl(0, TIOCGWINSZ, &ws) < 0)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "ioctl");
return GNUNET_SYSERR;
@@ -715,14 +831,14 @@
size_t arg_len;
unsigned int cnt;
- if (isatty (0))
+ if (!disable_pty && isatty (0))
can_pty = 1;
if (need_pty && !can_pty)
{
LOG_ERROR ("Not attached to a terminal but pseudo-tty is requested (-t)
option");
return;
}
- if (!disable_pty && can_pty)
+ if (can_pty)
{
if (GNUNET_OK != read_tty_settings ())
return;
Modified: msh/src/mshd.c
===================================================================
--- msh/src/mshd.c 2013-11-22 09:15:07 UTC (rev 30846)
+++ msh/src/mshd.c 2013-11-22 13:13:06 UTC (rev 30847)
@@ -390,6 +390,7 @@
break;
case MODE_WORKER:
shutdown_daemon_server ();
+ MSH_pmonitor_shutdown ();
break;
}
if (GNUNET_SCHEDULER_NO_TASK != accept_task)
@@ -736,6 +737,7 @@
daemon_server_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
init_daemon_server ();
+ MSH_pmonitor_init ();
daemon_server_add_connection (client_conn);
shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
&do_shutdown, NULL);
Modified: msh/src/server.c
===================================================================
--- msh/src/server.c 2013-11-22 09:15:07 UTC (rev 30846)
+++ msh/src/server.c 2013-11-22 13:13:06 UTC (rev 30847)
@@ -142,9 +142,9 @@
int authenticated;
/**
- * If this is an interactive session, is a pseudo-tty created?
+ * If this is an interactive session, is the pseudo-tty opened?
*/
- int pty_created;
+ int pty_opened;
};
/*
@@ -516,10 +516,10 @@
{
return;
}
+ errno = 0;
size = GNUNET_DISK_file_read_non_blocking (exec_ctx->fout, data,
MAX_IO_DATA);
if (size <= 0)
{
- GNUNET_break (GNUNET_SYSERR != size);
GNUNET_SERVER_client_disconnect (exec_ctx->client);
return;
}
@@ -659,6 +659,7 @@
if (!ctx->interactive)
{
+ LOG_DEBUG ("Execing non interactively `%s'\n", ctx->args[0]);
pin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, 0, 0);
pout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_YES, 0, 0);
GNUNET_assert ((NULL != pin && (NULL != pout)));
@@ -683,6 +684,7 @@
ctx->fout, &read_fout, ctx);
return GNUNET_OK;
}
+ LOG_DEBUG ("Execing interactively `%s'\n", ctx->args[0]);
if (NULL == (fnpty = ptsname (ctx->master)))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "ptsname");
@@ -696,27 +698,31 @@
}
if (0 != ret)
{
- int fd;
+ int fd_in;
+ int fd_out;
LOG_DEBUG ("Forked child successfully\n");
ctx->child_pid = ret;
MSH_monitor_process_pid (ctx->child_pid, &proc_exit_cb, ctx);
/* forward streams to and from child */
- ctx->fin = GNUNET_DISK_get_handle_from_int_fd (ctx->master);
- fd = dup (ctx->master);
- if (-1 == fd)
+ fd_in = dup (ctx->master);
+ fd_out = dup (ctx->master);
+ GNUNET_break (0 == close (ctx->master));
+ if ( (-1 == fd_in) || (-1 == fd_out) )
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup");
GNUNET_assert (0 == kill (ret, SIGTERM));
return GNUNET_SYSERR;
}
- ctx->fout = GNUNET_DISK_get_handle_from_int_fd (fd);
+ ctx->fin = GNUNET_DISK_get_handle_from_int_fd (fd_in);
+ ctx->fout = GNUNET_DISK_get_handle_from_int_fd (fd_out);
ctx->fout_task =
GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
ctx->fout,
&read_fout, ctx);
return GNUNET_OK;
}
+ // GNUNET_log_setup ("mshd-server-child", NULL, NULL);
close (ctx->master);
LOG_DEBUG ("Opening slave PTY %s\n", fnpty);
slave = open (fnpty, O_RDWR);
@@ -760,14 +766,14 @@
close (1);
//close (2);
if ( (-1 == dup2 (slave, 0)) ||
- (-1 == dup2 (slave, 1)) ) //||
- //(-1 == dup2 (slave, 2)) )
+ (-1 == dup2 (slave, 1)) ||
+ (-1 == dup2 (slave, 2)) )
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "dup");
_exit (2);
}
close (slave);
- LOG_DEBUG ("Execing %s\n", ctx->args[0]);
+ LOG_DEBUG ("Execing `%s'\n", ctx->args[0]);
if (-1 == execvp (ctx->args[0], ctx->args))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "execvp");
@@ -802,7 +808,7 @@
GNUNET_break_op (0);
goto close_conn;
}
- if (exec_ctx->interactive && !exec_ctx->pty_created)
+ if (exec_ctx->interactive && !exec_ctx->pty_opened)
{
GNUNET_break_op (0);
goto close_conn;
@@ -843,7 +849,6 @@
return;
close_conn:
- GNUNET_break_op (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
}
@@ -882,6 +887,7 @@
goto err_ret;
}
exec_ctx->authenticated = GNUNET_YES;
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
return;
err_ret:
@@ -1035,7 +1041,7 @@
goto close_conn;
}
exec_ctx->master = master;
- exec_ctx->pty_created = GNUNET_YES;
+ exec_ctx->pty_opened = GNUNET_YES;
exec_ctx->pty_mode = (struct MSH_MSG_PtyMode *) GNUNET_copy_message
(message);
GNUNET_SERVER_receive_done (client, GNUNET_OK);
return;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r30847 - msh/src,
gnunet <=