2006-09-07 15:51:24 +00:00
|
|
|
/*
|
2007-02-24 11:41:56 +00:00
|
|
|
* socket.c - this file is part of Geany, a fast and lightweight IDE
|
2006-09-07 15:51:24 +00:00
|
|
|
*
|
2012-06-18 01:13:05 +02:00
|
|
|
* Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2008-12-11 17:37:21 +00:00
|
|
|
* Copyright 2006 Hiroyuki Yamamoto (author of Sylpheed)
|
2006-09-07 15:51:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-08-24 19:25:57 +02:00
|
|
|
* 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.
|
2006-09-07 15:51:24 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-24 11:41:56 +00:00
|
|
|
/*
|
|
|
|
* Socket setup and messages handling.
|
|
|
|
* The socket allows detection and messages to be sent to the first running instance of Geany.
|
|
|
|
* Only the first instance loads session files at startup, and opens files from the command-line.
|
|
|
|
*/
|
|
|
|
|
2007-01-24 22:38:53 +00:00
|
|
|
/*
|
|
|
|
* Little dev doc:
|
|
|
|
* Each command which is sent between two instances (see send_open_command and
|
|
|
|
* socket_lock_input_cb) should have the following scheme:
|
|
|
|
* command name\n
|
|
|
|
* data\n
|
|
|
|
* data\n
|
|
|
|
* ...
|
|
|
|
* .\n
|
|
|
|
* The first thing should be the command name followed by the data belonging to the command and
|
|
|
|
* to mark the end of data send a single '.'. Each message should be ended with \n.
|
2009-01-22 20:31:35 +00:00
|
|
|
* The command window is only available on Windows and takes no additional data, instead it
|
|
|
|
* writes back a Windows handle (HWND) for the main window to set it to the foreground (focus).
|
2007-01-24 22:38:53 +00:00
|
|
|
*
|
2011-11-20 14:48:05 +01:00
|
|
|
* At the moment the commands window, doclist, open, openro, line and column are available.
|
2008-02-10 12:36:32 +00:00
|
|
|
*
|
|
|
|
* About the socket files on Unix-like systems:
|
2008-02-15 16:32:27 +00:00
|
|
|
* Geany creates a socket in /tmp (or any other directory returned by g_get_tmp_dir()) and
|
2008-02-10 12:36:32 +00:00
|
|
|
* a symlink in the current configuration to the created socket file. The symlink is named
|
2008-02-15 16:32:27 +00:00
|
|
|
* geany_socket_<hostname>_<displayname> (displayname is the name of the active X display).
|
|
|
|
* If the socket file cannot be created in the temporary directory, Geany creates the socket file
|
2008-02-10 12:36:32 +00:00
|
|
|
* directly in the configuration directory as a fallback.
|
|
|
|
*
|
2007-01-24 22:38:53 +00:00
|
|
|
*/
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
#include "geany.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCKET
|
|
|
|
|
2006-09-11 07:41:37 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2006-10-08 21:43:54 +00:00
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/types.h>
|
2006-09-11 07:41:37 +00:00
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/un.h>
|
|
|
|
# include <netinet/in.h>
|
2008-02-07 15:53:02 +00:00
|
|
|
# include <glib/gstdio.h>
|
2006-09-11 07:41:37 +00:00
|
|
|
#else
|
2009-01-22 20:31:35 +00:00
|
|
|
# include <gdk/gdkwin32.h>
|
|
|
|
# include <windows.h>
|
2006-09-11 07:41:37 +00:00
|
|
|
# include <winsock2.h>
|
|
|
|
# include <ws2tcpip.h>
|
|
|
|
#endif
|
2010-04-11 21:56:50 +00:00
|
|
|
#include <string.h>
|
2007-01-24 22:38:53 +00:00
|
|
|
#include <stdlib.h>
|
2006-09-07 15:51:24 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2009-04-08 16:06:25 +00:00
|
|
|
#ifdef GDK_WINDOWING_X11
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#endif
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "document.h"
|
2007-01-13 12:42:12 +00:00
|
|
|
#include "support.h"
|
2007-02-15 23:56:15 +00:00
|
|
|
#include "ui_utils.h"
|
2008-02-07 15:53:02 +00:00
|
|
|
#include "utils.h"
|
2010-04-19 21:20:15 +00:00
|
|
|
#include "dialogs.h"
|
2008-06-28 08:15:18 +00:00
|
|
|
#include "encodings.h"
|
2012-03-10 17:43:49 +04:00
|
|
|
#include "project.h"
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
|
2006-09-11 07:41:37 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#define REMOTE_CMD_PORT 49876
|
2011-03-24 22:00:18 +00:00
|
|
|
#define SOCKET_IS_VALID(s) ((s) != INVALID_SOCKET)
|
2006-09-11 07:41:37 +00:00
|
|
|
#else
|
2011-03-24 22:00:18 +00:00
|
|
|
#define SOCKET_IS_VALID(s) ((s) >= 0)
|
2006-09-11 07:41:37 +00:00
|
|
|
#define INVALID_SOCKET (-1)
|
|
|
|
#endif
|
2010-04-11 21:56:50 +00:00
|
|
|
#define BUFFER_LENGTH 4096
|
2006-09-11 07:41:37 +00:00
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
struct socket_info_struct socket_info;
|
|
|
|
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
static gint socket_fd_connect_inet (gushort port);
|
|
|
|
static gint socket_fd_open_inet (gushort port);
|
2008-02-20 11:24:23 +00:00
|
|
|
static void socket_init_win32 (void);
|
2006-09-11 07:41:37 +00:00
|
|
|
#else
|
2006-09-07 15:51:24 +00:00
|
|
|
static gint socket_fd_connect_unix (const gchar *path);
|
|
|
|
static gint socket_fd_open_unix (const gchar *path);
|
2006-09-11 07:41:37 +00:00
|
|
|
#endif
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
static gint socket_fd_write (gint sock, const gchar *buf, gint len);
|
|
|
|
static gint socket_fd_write_all (gint sock, const gchar *buf, gint len);
|
|
|
|
static gint socket_fd_gets (gint sock, gchar *buf, gint len);
|
|
|
|
static gint socket_fd_check_io (gint fd, GIOCondition cond);
|
|
|
|
static gint socket_fd_read (gint sock, gchar *buf, gint len);
|
|
|
|
static gint socket_fd_recv (gint fd, gchar *buf, gint len, gint flags);
|
|
|
|
static gint socket_fd_close (gint sock);
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-25 17:43:39 +00:00
|
|
|
static void send_open_command(gint sock, gint argc, gchar **argv)
|
2007-01-13 12:42:12 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gchar *filename;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(argc > 1);
|
2007-01-13 12:42:12 +00:00
|
|
|
geany_debug("using running instance of Geany");
|
|
|
|
|
2007-01-24 22:38:53 +00:00
|
|
|
if (cl_options.goto_line >= 0)
|
|
|
|
{
|
|
|
|
gchar *line = g_strdup_printf("%d\n", cl_options.goto_line);
|
|
|
|
socket_fd_write_all(sock, "line\n", 5);
|
|
|
|
socket_fd_write_all(sock, line, strlen(line));
|
|
|
|
socket_fd_write_all(sock, ".\n", 2);
|
|
|
|
g_free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cl_options.goto_column >= 0)
|
|
|
|
{
|
|
|
|
gchar *col = g_strdup_printf("%d\n", cl_options.goto_column);
|
|
|
|
socket_fd_write_all(sock, "column\n", 7);
|
|
|
|
socket_fd_write_all(sock, col, strlen(col));
|
|
|
|
socket_fd_write_all(sock, ".\n", 2);
|
|
|
|
g_free(col);
|
|
|
|
}
|
|
|
|
|
2011-11-03 22:27:49 +01:00
|
|
|
if (cl_options.readonly) /* append "ro" to denote readonly status for new docs */
|
|
|
|
socket_fd_write_all(sock, "openro\n", 7);
|
|
|
|
else
|
|
|
|
socket_fd_write_all(sock, "open\n", 5);
|
2007-01-13 12:42:12 +00:00
|
|
|
|
2008-07-21 14:12:15 +00:00
|
|
|
for (i = 1; i < argc && argv[i] != NULL; i++)
|
2007-01-13 12:42:12 +00:00
|
|
|
{
|
2008-09-24 12:07:22 +00:00
|
|
|
filename = main_get_argv_filename(argv[i]);
|
2007-01-13 12:42:12 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if the filename is valid or if a new file should be opened is check on the other side */
|
2009-04-15 22:47:33 +00:00
|
|
|
if (filename != NULL)
|
2007-01-13 12:42:12 +00:00
|
|
|
{
|
|
|
|
socket_fd_write_all(sock, filename, strlen(filename));
|
|
|
|
socket_fd_write_all(sock, "\n", 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_printerr(_("Could not find file '%s'."), filename);
|
2008-02-27 13:17:29 +00:00
|
|
|
g_printerr("\n"); /* keep translation from open_cl_files() in main.c. */
|
2007-01-13 12:42:12 +00:00
|
|
|
}
|
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
socket_fd_write_all(sock, ".\n", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-18 17:17:55 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2008-02-20 11:24:23 +00:00
|
|
|
static void remove_socket_link_full(void)
|
2008-02-10 12:36:32 +00:00
|
|
|
{
|
|
|
|
gchar real_path[512];
|
|
|
|
gsize len;
|
|
|
|
|
|
|
|
real_path[0] = '\0';
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* read the contents of the symbolic link socket_info.file_name and delete it
|
|
|
|
* readlink should return something like "/tmp/geany_socket.499602d2" */
|
2008-02-10 12:36:32 +00:00
|
|
|
len = readlink(socket_info.file_name, real_path, sizeof(real_path) - 1);
|
|
|
|
if ((gint) len > 0)
|
|
|
|
{
|
|
|
|
real_path[len] = '\0';
|
|
|
|
g_unlink(real_path);
|
|
|
|
}
|
|
|
|
g_unlink(socket_info.file_name);
|
|
|
|
}
|
2008-02-18 17:17:55 +00:00
|
|
|
#endif
|
2008-02-10 12:36:32 +00:00
|
|
|
|
|
|
|
|
2010-04-11 21:56:50 +00:00
|
|
|
static void socket_get_document_list(gint sock)
|
|
|
|
{
|
|
|
|
gchar doc_list[BUFFER_LENGTH];
|
|
|
|
gint doc_list_len;
|
|
|
|
|
|
|
|
if (sock < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
socket_fd_write_all(sock, "doclist\n", 8);
|
|
|
|
|
|
|
|
doc_list_len = socket_fd_read(sock, doc_list, sizeof(doc_list));
|
|
|
|
if (doc_list_len >= BUFFER_LENGTH)
|
|
|
|
doc_list_len = BUFFER_LENGTH -1;
|
|
|
|
doc_list[doc_list_len] = '\0';
|
|
|
|
/* if we received ETX (end-of-text), there were no open files, so print only otherwise */
|
|
|
|
if (! utils_str_equal(doc_list, "\3"))
|
|
|
|
printf("%s", doc_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 18:50:48 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2010-04-19 21:20:15 +00:00
|
|
|
static void check_socket_permissions(void)
|
|
|
|
{
|
|
|
|
struct stat socket_stat;
|
|
|
|
|
|
|
|
if (g_lstat(socket_info.file_name, &socket_stat) == 0)
|
|
|
|
{ /* If the user id of the process is not the same as the owner of the socket
|
|
|
|
* file, then ignore this socket and start a new session. */
|
|
|
|
if (socket_stat.st_uid != getuid())
|
|
|
|
{
|
|
|
|
const gchar *msg = _(
|
|
|
|
/* TODO maybe this message needs a rewording */
|
|
|
|
"Geany tried to access the Unix Domain socket of another instance running as another user.\n"
|
|
|
|
"This is a fatal error and Geany will now quit.");
|
|
|
|
g_warning("%s", msg);
|
|
|
|
dialogs_show_msgbox(GTK_MESSAGE_ERROR, "%s", msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-21 18:50:48 +00:00
|
|
|
#endif
|
2010-04-19 21:20:15 +00:00
|
|
|
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
/* (Unix domain) socket support to replace the old FIFO code
|
2008-02-12 12:04:55 +00:00
|
|
|
* (taken from Sylpheed, thanks)
|
|
|
|
* Returns the created socket, -1 if an error occurred or -2 if another socket exists and files
|
|
|
|
* were sent to it. */
|
2006-09-07 15:51:24 +00:00
|
|
|
gint socket_init(gint argc, gchar **argv)
|
|
|
|
{
|
|
|
|
gint sock;
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
HANDLE hmutex;
|
2009-01-22 20:31:35 +00:00
|
|
|
HWND hwnd;
|
2006-09-11 07:41:37 +00:00
|
|
|
socket_init_win32();
|
2008-12-11 19:05:17 +00:00
|
|
|
hmutex = CreateMutexA(NULL, FALSE, "Geany");
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! hmutex)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
geany_debug("cannot create Mutex\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (GetLastError() != ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
2008-12-11 19:05:17 +00:00
|
|
|
/* To support multiple instances with different configuration directories (as we do on
|
|
|
|
* non-Windows systems) we would need to use different port number s but it might be
|
|
|
|
* difficult to get a port number which is unique for a configuration directory (path)
|
|
|
|
* and which is unused. This port number has to be guessed by the first and new instance
|
|
|
|
* and the only data is the configuration directory path.
|
|
|
|
* For now we use one port number, that is we support only one instance at all. */
|
2006-09-07 15:51:24 +00:00
|
|
|
sock = socket_fd_open_inet(REMOTE_CMD_PORT);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (sock < 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
return 0;
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket_fd_connect_inet(REMOTE_CMD_PORT);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (sock < 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
return -1;
|
|
|
|
#else
|
2008-02-10 17:55:00 +00:00
|
|
|
gchar *display_name = gdk_get_display();
|
|
|
|
gchar *hostname = utils_get_hostname();
|
|
|
|
gchar *p;
|
2008-02-07 17:27:05 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (display_name == NULL)
|
2008-02-10 17:55:00 +00:00
|
|
|
display_name = g_strdup("NODISPLAY");
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* these lines are taken from dcopc.c in kdelibs */
|
2008-02-10 17:55:00 +00:00
|
|
|
if ((p = strrchr(display_name, '.')) > strrchr(display_name, ':') && p != NULL)
|
|
|
|
*p = '\0';
|
2012-02-10 01:18:30 +01:00
|
|
|
/* remove characters that may not be acceptable in a filename */
|
|
|
|
for (p = display_name; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == ':' || *p == '/')
|
|
|
|
*p = '_';
|
|
|
|
}
|
2006-09-07 15:51:24 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (socket_info.file_name == NULL)
|
2010-10-07 14:10:32 +00:00
|
|
|
socket_info.file_name = g_strdup_printf("%s%cgeany_socket_%s_%s",
|
|
|
|
app->configdir, G_DIR_SEPARATOR, hostname, display_name);
|
2008-02-10 17:55:00 +00:00
|
|
|
|
|
|
|
g_free(display_name);
|
|
|
|
g_free(hostname);
|
2006-09-07 15:51:24 +00:00
|
|
|
|
2010-04-19 21:20:15 +00:00
|
|
|
/* check whether the real user id is the same as this of the socket file */
|
|
|
|
check_socket_permissions();
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
sock = socket_fd_connect_unix(socket_info.file_name);
|
|
|
|
if (sock < 0)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
remove_socket_link_full(); /* deletes the socket file and the symlink */
|
2006-09-07 15:51:24 +00:00
|
|
|
return socket_fd_open_unix(socket_info.file_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* remote command mode, here we have another running instance and want to use it */
|
2009-01-22 20:31:35 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* first we send a request to retrieve the window handle and focus the window */
|
|
|
|
socket_fd_write_all(sock, "window\n", 7);
|
|
|
|
if (socket_fd_read(sock, (gchar *)&hwnd, sizeof(hwnd)) == sizeof(hwnd))
|
|
|
|
SetForegroundWindow(hwnd);
|
|
|
|
#endif
|
|
|
|
/* now we send the command line args */
|
2006-09-07 15:51:24 +00:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
2007-01-13 12:42:12 +00:00
|
|
|
send_open_command(sock, argc, argv);
|
2006-09-07 15:51:24 +00:00
|
|
|
}
|
|
|
|
|
2010-04-11 21:56:50 +00:00
|
|
|
if (cl_options.list_documents)
|
|
|
|
{
|
|
|
|
socket_get_document_list(sock);
|
|
|
|
}
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
socket_fd_close(sock);
|
2008-02-12 12:04:55 +00:00
|
|
|
return -2;
|
2006-09-07 15:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gint socket_finalize(void)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
if (socket_info.lock_socket < 0)
|
2009-04-05 21:07:40 +00:00
|
|
|
return -1;
|
2006-09-07 15:51:24 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (socket_info.lock_socket_tag > 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
g_source_remove(socket_info.lock_socket_tag);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (socket_info.read_ioc)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
g_io_channel_shutdown(socket_info.read_ioc, FALSE, NULL);
|
|
|
|
g_io_channel_unref(socket_info.read_ioc);
|
|
|
|
socket_info.read_ioc = NULL;
|
|
|
|
}
|
|
|
|
|
2006-09-11 07:41:37 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#else
|
2009-04-15 22:47:33 +00:00
|
|
|
if (socket_info.file_name != NULL)
|
2006-12-05 10:23:51 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
remove_socket_link_full(); /* deletes the socket file and the symlink */
|
2006-12-05 10:23:51 +00:00
|
|
|
g_free(socket_info.file_name);
|
|
|
|
}
|
2006-09-07 15:51:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-11 07:41:37 +00:00
|
|
|
#ifdef G_OS_UNIX
|
2006-09-07 15:51:24 +00:00
|
|
|
static gint socket_fd_connect_unix(const gchar *path)
|
|
|
|
{
|
|
|
|
gint sock;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (sock < 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
perror("fd_connect_unix(): socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
|
|
|
|
|
|
|
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
|
|
{
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_open_unix(const gchar *path)
|
|
|
|
{
|
|
|
|
gint sock;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
gint val;
|
2008-02-07 15:53:02 +00:00
|
|
|
gchar *real_path;
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (sock < 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
perror("sock_open_unix(): socket");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* fix for #1888561:
|
|
|
|
* in case the configuration directory is located on a network file system or any other
|
|
|
|
* file system which doesn't support sockets, we just link the socket there and create the
|
|
|
|
* real socket in the system's tmp directory assuming it supports sockets */
|
2008-02-10 12:36:32 +00:00
|
|
|
real_path = g_strdup_printf("%s%cgeany_socket.%08x",
|
|
|
|
g_get_tmp_dir(), G_DIR_SEPARATOR, g_random_int());
|
2008-02-07 15:53:02 +00:00
|
|
|
|
2011-02-06 16:47:18 +00:00
|
|
|
if (utils_is_file_writable(real_path) != 0)
|
2008-11-13 14:37:47 +00:00
|
|
|
{ /* if real_path is not writable for us, fall back to ~/.config/geany/geany_socket_*_* */
|
2008-02-27 13:17:29 +00:00
|
|
|
/* instead of creating a symlink and print a warning */
|
2008-02-07 15:53:02 +00:00
|
|
|
g_warning("Socket %s could not be written, using %s as fallback.", real_path, path);
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(real_path, g_strdup(path));
|
2008-02-07 15:53:02 +00:00
|
|
|
}
|
2008-11-13 14:37:47 +00:00
|
|
|
/* create a symlink in e.g. ~/.config/geany/geany_socket_hostname__0 to /tmp/geany_socket.499602d2 */
|
2008-02-07 15:53:02 +00:00
|
|
|
else if (symlink(real_path, path) != 0)
|
|
|
|
{
|
|
|
|
perror("symlink");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
2008-02-07 15:53:02 +00:00
|
|
|
strncpy(addr.sun_path, real_path, sizeof(addr.sun_path) - 1);
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
|
|
{
|
|
|
|
perror("bind");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(sock, 1) < 0)
|
|
|
|
{
|
|
|
|
perror("listen");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-03-16 14:03:20 +00:00
|
|
|
g_chmod(real_path, 0600);
|
2009-03-15 07:50:05 +00:00
|
|
|
|
|
|
|
g_free(real_path);
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
return sock;
|
|
|
|
}
|
2006-09-11 07:41:37 +00:00
|
|
|
#endif
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
static gint socket_fd_close(gint fd)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return closesocket(fd);
|
|
|
|
#else
|
|
|
|
return close(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
static gint socket_fd_open_inet(gushort port)
|
|
|
|
{
|
2009-01-22 20:31:35 +00:00
|
|
|
SOCKET sock;
|
2006-09-07 15:51:24 +00:00
|
|
|
struct sockaddr_in addr;
|
2006-09-11 07:41:37 +00:00
|
|
|
gchar val;
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2011-03-24 22:00:18 +00:00
|
|
|
if (G_UNLIKELY(! SOCKET_IS_VALID(sock)))
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
2006-10-24 18:02:38 +00:00
|
|
|
geany_debug("fd_open_inet(): socket() failed: %d\n", WSAGetLastError());
|
2006-09-07 15:51:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
|
|
{
|
|
|
|
perror("bind");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(sock, 1) < 0)
|
|
|
|
{
|
|
|
|
perror("listen");
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_connect_inet(gushort port)
|
|
|
|
{
|
2009-01-22 20:31:35 +00:00
|
|
|
SOCKET sock;
|
2006-09-07 15:51:24 +00:00
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
2011-03-24 22:00:18 +00:00
|
|
|
if (G_UNLIKELY(! SOCKET_IS_VALID(sock)))
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
2006-10-24 18:02:38 +00:00
|
|
|
geany_debug("fd_connect_inet(): socket() failed: %d\n", WSAGetLastError());
|
2006-09-07 15:51:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
|
|
|
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
|
|
|
{
|
|
|
|
socket_fd_close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
2006-09-11 07:41:37 +00:00
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
static void socket_init_win32(void)
|
2006-09-11 07:41:37 +00:00
|
|
|
{
|
|
|
|
WSADATA wsadata;
|
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsadata) != NO_ERROR)
|
|
|
|
geany_debug("WSAStartup() failed\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2006-09-07 15:51:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-06-28 08:15:18 +00:00
|
|
|
static void handle_input_filename(const gchar *buf)
|
|
|
|
{
|
|
|
|
gchar *utf8_filename, *locale_filename;
|
|
|
|
|
|
|
|
/* we never know how the input is encoded, so do the best auto detection we can */
|
|
|
|
if (! g_utf8_validate(buf, -1, NULL))
|
2012-01-03 13:30:38 +00:00
|
|
|
utf8_filename = encodings_convert_to_utf8(buf, -1, NULL);
|
2008-06-28 08:15:18 +00:00
|
|
|
else
|
|
|
|
utf8_filename = g_strdup(buf);
|
|
|
|
|
|
|
|
locale_filename = utils_get_locale_from_utf8(utf8_filename);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (locale_filename)
|
2009-11-29 19:59:13 +00:00
|
|
|
{
|
|
|
|
if (g_str_has_suffix(locale_filename, ".geany"))
|
2012-03-10 17:43:49 +04:00
|
|
|
{
|
|
|
|
if (project_ask_close())
|
|
|
|
main_load_project_from_command_line(locale_filename, TRUE);
|
|
|
|
}
|
2009-11-29 19:59:13 +00:00
|
|
|
else
|
|
|
|
main_handle_filename(locale_filename);
|
|
|
|
}
|
2009-04-05 21:07:40 +00:00
|
|
|
g_free(utf8_filename);
|
2008-06-28 08:15:18 +00:00
|
|
|
g_free(locale_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-11 21:56:50 +00:00
|
|
|
static gchar *build_document_list(void)
|
|
|
|
{
|
|
|
|
GString *doc_list = g_string_new(NULL);
|
|
|
|
guint i;
|
|
|
|
const gchar *filename;
|
|
|
|
|
|
|
|
foreach_document(i)
|
|
|
|
{
|
|
|
|
filename = DOC_FILENAME(documents[i]);
|
|
|
|
g_string_append(doc_list, filename);
|
|
|
|
g_string_append_c(doc_list, '\n');
|
|
|
|
}
|
|
|
|
return g_string_free(doc_list, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpointer data)
|
|
|
|
{
|
|
|
|
gint fd, sock;
|
2010-04-11 21:56:50 +00:00
|
|
|
gchar buf[BUFFER_LENGTH];
|
2006-09-07 15:51:24 +00:00
|
|
|
struct sockaddr_in caddr;
|
2011-09-30 11:11:29 +00:00
|
|
|
socklen_t caddr_len = sizeof(caddr);
|
2009-01-22 20:31:35 +00:00
|
|
|
GtkWidget *window = data;
|
2009-03-16 14:41:10 +00:00
|
|
|
gboolean popup = FALSE;
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
fd = g_io_channel_unix_get_fd(source);
|
|
|
|
sock = accept(fd, (struct sockaddr *)&caddr, &caddr_len);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* first get the command */
|
2007-01-24 22:38:53 +00:00
|
|
|
while (socket_fd_gets(sock, buf, sizeof(buf)) != -1)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
2007-01-24 22:38:53 +00:00
|
|
|
if (strncmp(buf, "open", 4) == 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
2011-11-21 20:33:37 +02:00
|
|
|
cl_options.readonly = strncmp(buf+4, "ro", 2) == 0; /* open in readonly? */
|
2007-01-24 22:38:53 +00:00
|
|
|
while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
|
|
|
|
{
|
2012-08-08 15:33:45 +02:00
|
|
|
gsize buf_len = strlen(buf);
|
|
|
|
|
|
|
|
/* remove trailing newline */
|
|
|
|
if (buf_len > 0 && buf[buf_len - 1] == '\n')
|
|
|
|
buf[buf_len - 1] = '\0';
|
|
|
|
|
|
|
|
handle_input_filename(buf);
|
2007-01-24 22:38:53 +00:00
|
|
|
}
|
2009-03-16 14:41:10 +00:00
|
|
|
popup = TRUE;
|
2007-01-24 22:38:53 +00:00
|
|
|
}
|
2010-04-11 21:56:50 +00:00
|
|
|
else if (strncmp(buf, "doclist", 7) == 0)
|
|
|
|
{
|
|
|
|
gchar *doc_list = build_document_list();
|
|
|
|
if (NZV(doc_list))
|
|
|
|
socket_fd_write_all(sock, doc_list, strlen(doc_list));
|
|
|
|
else
|
|
|
|
/* send ETX (end-of-text) in case we have no open files, we must send anything
|
|
|
|
* otherwise the client would hang on reading */
|
|
|
|
socket_fd_write_all(sock, "\3", 1);
|
|
|
|
g_free(doc_list);
|
|
|
|
}
|
2007-01-24 22:38:53 +00:00
|
|
|
else if (strncmp(buf, "line", 4) == 0)
|
|
|
|
{
|
|
|
|
while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
g_strstrip(buf); /* remove \n char */
|
|
|
|
/* on any error we get 0 which should be safe enough as fallback */
|
2007-01-24 22:38:53 +00:00
|
|
|
cl_options.goto_line = atoi(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strncmp(buf, "column", 6) == 0)
|
|
|
|
{
|
|
|
|
while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
g_strstrip(buf); /* remove \n char */
|
|
|
|
/* on any error we get 0 which should be safe enough as fallback */
|
2007-01-24 22:38:53 +00:00
|
|
|
cl_options.goto_column = atoi(buf);
|
|
|
|
}
|
|
|
|
}
|
2009-01-22 20:31:35 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
else if (strncmp(buf, "window", 6) == 0)
|
|
|
|
{
|
2011-11-06 18:23:01 -08:00
|
|
|
HWND hwnd = (HWND) gdk_win32_drawable_get_handle(
|
|
|
|
GDK_DRAWABLE(gtk_widget_get_window(window)));
|
2009-01-22 20:31:35 +00:00
|
|
|
socket_fd_write(sock, (gchar *)&hwnd, sizeof(hwnd));
|
|
|
|
}
|
|
|
|
#endif
|
2006-09-07 15:51:24 +00:00
|
|
|
}
|
2009-04-08 16:06:25 +00:00
|
|
|
|
2009-03-16 14:41:10 +00:00
|
|
|
if (popup)
|
|
|
|
{
|
2009-04-08 16:06:25 +00:00
|
|
|
#ifdef GDK_WINDOWING_X11
|
|
|
|
/* Set the proper interaction time on the window. This seems necessary to make
|
|
|
|
* gtk_window_present() really bring the main window into the foreground on some
|
|
|
|
* window managers like Gnome's metacity.
|
|
|
|
* Code taken from Gedit. */
|
2011-11-06 18:23:01 -08:00
|
|
|
gdk_x11_window_set_user_time(gtk_widget_get_window(window),
|
|
|
|
gdk_x11_get_server_time(gtk_widget_get_window(window)));
|
2009-04-08 16:06:25 +00:00
|
|
|
#endif
|
2009-03-16 14:41:10 +00:00
|
|
|
gtk_window_present(GTK_WINDOW(window));
|
2009-01-22 20:32:04 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2011-11-06 18:23:01 -08:00
|
|
|
gdk_window_show(gtk_widget_get_window(window));
|
2009-01-22 20:32:04 +00:00
|
|
|
#endif
|
2009-03-16 14:41:10 +00:00
|
|
|
}
|
2009-01-22 20:32:04 +00:00
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
socket_fd_close(sock);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_gets(gint fd, gchar *buf, gint len)
|
|
|
|
{
|
|
|
|
gchar *newline, *bp = buf;
|
|
|
|
gint n;
|
|
|
|
|
|
|
|
if (--len < 1)
|
|
|
|
return -1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((n = socket_fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
|
|
|
|
return -1;
|
|
|
|
if ((newline = memchr(bp, '\n', n)) != NULL)
|
|
|
|
n = newline - bp + 1;
|
|
|
|
if ((n = socket_fd_read(fd, bp, n)) < 0)
|
|
|
|
return -1;
|
|
|
|
bp += n;
|
|
|
|
len -= n;
|
|
|
|
} while (! newline && len);
|
|
|
|
|
|
|
|
*bp = '\0';
|
|
|
|
return bp - buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_recv(gint fd, gchar *buf, gint len, gint flags)
|
|
|
|
{
|
|
|
|
if (socket_fd_check_io(fd, G_IO_IN) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return recv(fd, buf, len, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_read(gint fd, gchar *buf, gint len)
|
|
|
|
{
|
|
|
|
if (socket_fd_check_io(fd, G_IO_IN) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return recv(fd, buf, len, 0);
|
|
|
|
#else
|
|
|
|
return read(fd, buf, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_check_io(gint fd, GIOCondition cond)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fds;
|
2006-09-11 07:41:37 +00:00
|
|
|
#ifdef G_OS_UNIX
|
2006-09-07 15:51:24 +00:00
|
|
|
gint flags;
|
2006-09-11 07:41:37 +00:00
|
|
|
#endif
|
2006-09-07 15:51:24 +00:00
|
|
|
|
|
|
|
timeout.tv_sec = 60;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
2008-02-27 13:17:29 +00:00
|
|
|
/* checking for non-blocking mode */
|
2006-09-07 15:51:24 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (flags < 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
perror("fcntl");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((flags & O_NONBLOCK) != 0)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
|
|
|
if (cond == G_IO_IN)
|
|
|
|
{
|
|
|
|
select(fd + 1, &fds, NULL, NULL, &timeout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
select(fd + 1, NULL, &fds, NULL, &timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(fd, &fds))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-22 20:31:35 +00:00
|
|
|
geany_debug("Socket IO timeout");
|
2006-09-07 15:51:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gint socket_fd_write_all(gint fd, const gchar *buf, gint len)
|
|
|
|
{
|
|
|
|
gint n, wrlen = 0;
|
|
|
|
|
|
|
|
while (len)
|
|
|
|
{
|
|
|
|
n = socket_fd_write(fd, buf, len);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (n <= 0)
|
2006-09-07 15:51:24 +00:00
|
|
|
return -1;
|
|
|
|
len -= n;
|
|
|
|
wrlen += n;
|
|
|
|
buf += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gint socket_fd_write(gint fd, const gchar *buf, gint len)
|
|
|
|
{
|
|
|
|
if (socket_fd_check_io(fd, G_IO_OUT) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return send(fd, buf, len, 0);
|
|
|
|
#else
|
|
|
|
return write(fd, buf, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-09-16 14:13:38 +00:00
|
|
|
|
2006-09-07 15:51:24 +00:00
|
|
|
#endif
|