1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/**
* Copyright 2008 Fred Chien <cfsghost@gmail.com>
* Copyright (c) 2010 LxDE Developers, see the file AUTHORS for details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include "lxterminal.h"
#include "unixsocket.h"
static gboolean lxterminal_socket_read_channel(GIOChannel * gio, GIOCondition condition, LXTermWindow * lxtermwin);
static gboolean lxterminal_socket_accept_client(GIOChannel * source, GIOCondition condition, LXTermWindow * lxtermwin);
/* Handler for successful read on communication socket. */
static gboolean lxterminal_socket_read_channel(GIOChannel * gio, GIOCondition condition, LXTermWindow * lxtermwin)
{
/* Read message. */
gchar * msg = NULL;
gsize len = 0;
GError * err = NULL;
GIOStatus ret = g_io_channel_read_to_end(gio, &msg, &len, &err);
if (ret == G_IO_STATUS_ERROR)
{
g_warning("Error reading socket: %s\n", err->message);
}
/* Process message. */
if (len > 0)
{
/* Skip the the first (cur_dir) and last '\0' for argument count */
gint argc = -1;
gsize i;
for (i = 0; i < len; i ++)
{
if (msg[i] == '\0')
{
argc ++;
}
}
gchar * cur_dir = msg;
gchar * * argv = g_malloc(argc * sizeof(char *));
gint nul_count = 0;
for (i = 0; i < len; i ++)
{
if (msg[i] == '\0' && nul_count < argc)
{
argv[nul_count] = &msg[i + 1];
nul_count ++;
}
}
/* Parse arguments.
* Initialize a new LXTerminal and create a new window. */
CommandArguments arguments;
lxterminal_process_arguments(argc, argv, &arguments);
g_free(argv);
/* Make sure working directory matches that of the client process */
if (arguments.working_directory == NULL)
{
arguments.working_directory = g_strdup(cur_dir);
}
lxterminal_initialize(lxtermwin, &arguments);
}
g_free(msg);
/* If there was a disconnect, discontinue read. Otherwise, continue. */
if (condition & G_IO_HUP)
{
return FALSE;
}
return TRUE;
}
/* Handler for successful listen on communication socket. */
static gboolean lxterminal_socket_accept_client(GIOChannel * source, GIOCondition condition, LXTermWindow * lxtermwin)
{
if (condition & G_IO_IN)
{
/* Accept the new connection. */
int fd = accept(g_io_channel_unix_get_fd(source), NULL, NULL);
if (fd < 0)
g_warning("Accept failed: %s\n", g_strerror(errno));
/* Add O_NONBLOCK to the flags. */
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
/* Create a glib I/O channel. */
GIOChannel * gio = g_io_channel_unix_new(fd);
if (gio == NULL)
g_warning("Cannot create new GIOChannel\n");
else
{
/* Set up the glib I/O channel and add it to the event loop. */
g_io_channel_set_encoding(gio, NULL, NULL);
g_io_add_watch(gio, G_IO_IN | G_IO_HUP, (GIOFunc) lxterminal_socket_read_channel, lxtermwin);
g_io_channel_unref(gio);
}
}
/* Our listening socket hung up - we are dead. */
if (condition & G_IO_HUP)
g_error("Server listening socket closed unexpectedly\n");
return TRUE;
}
gboolean lxterminal_socket_initialize(LXTermWindow * lxtermwin, gint argc, gchar * * argv)
{
/* Normally, LXTerminal uses one process to control all of its windows.
* The first process to start will create a Unix domain socket in /tmp.
* It will then bind and listen on this socket.
* The subsequent processes will connect to the controller that owns the Unix domain socket.
* They will pass their command line over the socket and exit.
*
* If for any reason both the connect and bind fail, we will fall back to having that
* process be standalone; it will not be either the controller or a user of the controller.
* This behavior was introduced in response to a problem report (2973537).
*
* This function returns TRUE if this process should keep running and FALSE if it should exit. */
/* Formulate the path for the Unix domain socket. */
gchar * socket_path = g_strdup_printf("%s/.lxterminal-socket-%s", g_get_user_runtime_dir(), gdk_display_get_name(gdk_display_get_default()));
/* Create socket. */
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
{
g_warning("Socket create failed: %s\n", g_strerror(errno));
g_free(socket_path);
return TRUE;
}
/* Initialize socket address for Unix domain socket. */
struct sockaddr_un sock_addr;
memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sun_family = AF_UNIX;
snprintf(sock_addr.sun_path, sizeof(sock_addr.sun_path), "%s", socket_path);
/* Try to connect to an existing LXTerminal process. */
if (connect(fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0)
{
/* Connect failed. We are the controller, unless something fails. */
unlink(socket_path);
g_free(socket_path);
/* Bind to socket. */
if (bind(fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0)
{
g_warning("Bind on socket failed: %s\n", g_strerror(errno));
close(fd);
return TRUE;
}
/* Listen on socket. */
if (listen(fd, 5) < 0)
{
g_warning("Listen on socket failed: %s\n", g_strerror(errno));
close(fd);
return TRUE;
}
/* Create a glib I/O channel. */
GIOChannel * gio = g_io_channel_unix_new(fd);
if (gio == NULL)
{
g_warning("Cannot create GIOChannel\n");
close(fd);
return TRUE;
}
/* Set up GIOChannel. */
g_io_channel_set_encoding(gio, NULL, NULL);
g_io_channel_set_buffered(gio, FALSE);
g_io_channel_set_close_on_unref(gio, TRUE);
/* Add I/O channel to the main event loop. */
if ( ! g_io_add_watch(gio, G_IO_IN | G_IO_HUP, (GIOFunc) lxterminal_socket_accept_client, lxtermwin))
{
g_warning("Cannot add watch on GIOChannel\n");
close(fd);
g_io_channel_unref(gio);
return TRUE;
}
/* Channel will automatically shut down when the watch returns FALSE. */
g_io_channel_set_close_on_unref(gio, TRUE);
g_io_channel_unref(gio);
return TRUE;
}
else
{
g_free(socket_path);
/* Create a glib I/O channel. */
GIOChannel * gio = g_io_channel_unix_new(fd);
g_io_channel_set_encoding(gio, NULL, NULL);
/* Push current dir in case it is needed later */
gchar * cur_dir = g_get_current_dir();
g_io_channel_write_chars(gio, cur_dir, -1, NULL, NULL);
/* Use "" as a pointer to '\0' since g_io_channel_write_chars() won't accept NULL */
g_io_channel_write_chars(gio, "", 1, NULL, NULL);
g_free(cur_dir);
/* push all of argv. */
gint i;
for (i = 0; i < argc; i ++)
{
g_io_channel_write_chars(gio, argv[i], -1, NULL, NULL);
g_io_channel_write_chars(gio, "", 1, NULL, NULL);
}
g_io_channel_flush(gio, NULL);
g_io_channel_unref(gio);
return FALSE;
}
}
|