File: unixsocket.c

package info (click to toggle)
lxterminal 0.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,780 kB
  • sloc: ansic: 2,521; sh: 1,270; xml: 79; makefile: 74; python: 14
file content (267 lines) | stat: -rw-r--r-- 8,874 bytes parent folder | download | duplicates (3)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
 *      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"

typedef struct _client_info {
    LXTermWindow * lxtermwin;
    int fd;
} ClientInfo;

static gboolean init(LXTermWindow* lxtermwin, gint argc, gchar** argv);
static void start_controller(struct sockaddr_un* sock_addr, LXTermWindow* lxtermwin, int fd);
static void send_msg_to_controller(int fd, gint argc, gchar** argv);
static gboolean handle_client(GIOChannel* source, GIOCondition condition, LXTermWindow* lxtermwin);
static void accept_client(GIOChannel* source, LXTermWindow* lxtermwin);
static gboolean handle_request(GIOChannel* gio, GIOCondition condition, ClientInfo* info);

static gboolean init(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
     * g_get_user_runtime_dir().  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 function returns TRUE if this process should keep running and FALSE
     * if it should exit. */

    /* Formulate the path for the Unix domain socket. */
#if GLIB_CHECK_VERSION (2, 28, 0)
    gchar * socket_path = g_strdup_printf("%s/.lxterminal-socket-%s",
            g_get_user_runtime_dir(),
            gdk_display_get_name(gdk_display_get_default()));
#else
    gchar * socket_path = g_strdup_printf("%s/.lxterminal-socket-%s",
            g_get_user_cache_dir(),
            gdk_display_get_name(gdk_display_get_default()));
#endif

    /* Create socket. */
    int fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        g_warning("Socket create failed: %s\n", g_strerror(errno));
        goto err_socket;
    }

    /* 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) {
        g_free(socket_path);
        send_msg_to_controller(fd, argc, argv);
        return FALSE;
    }

    unlink(socket_path);
    g_free(socket_path);
    start_controller(&sock_addr, lxtermwin, fd);
    return TRUE;

err_socket:
    g_free(socket_path);
    return TRUE;
}

static void start_controller(struct sockaddr_un* sock_addr, LXTermWindow* lxtermwin, int fd) {
    /* 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));
        goto err_close_fd;
    }

    /* Listen on socket. */
    if (listen(fd, 5) < 0) {
        g_warning("Listen on socket failed: %s\n", g_strerror(errno));
        goto err_close_fd;
    }

    /* Create a glib I/O channel. */
    GIOChannel * gio = g_io_channel_unix_new(fd);
    if (gio == NULL) {
        g_warning("Cannot create GIOChannel\n");
        goto err_close_fd;
    }

    /* 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) handle_client, lxtermwin)) {
        g_warning("Cannot add watch on GIOChannel\n");
        goto err_gio_watch;
    }

    /* Channel will automatically shut down when the watch returns FALSE. */
    g_io_channel_unref(gio);
    return;

err_gio_watch:
    g_io_channel_unref(gio);

err_close_fd:
    close(fd);
    return;
}

static void send_msg_to_controller(int fd, gint argc, gchar** argv) {
    /* 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);
    close(fd);
}

static gboolean handle_client(GIOChannel* source, GIOCondition condition, LXTermWindow* lxtermwin) {
    if (condition & G_IO_IN) {
        accept_client(source, lxtermwin);
    }

    if (condition & G_IO_HUP) {
        g_error("Server listening socket closed unexpectedly\n");
    }

    return TRUE;
}

static void accept_client(GIOChannel* source, LXTermWindow* lxtermwin) {
    /* 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));
        return;
    }

    /* 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");
        return;
    }

    ClientInfo* info = g_malloc(sizeof(ClientInfo));
    info->lxtermwin = lxtermwin;
    info->fd = fd;

    /* 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) handle_request, info);
    g_io_channel_unref(gio);
}

static gboolean handle_request(GIOChannel* gio, GIOCondition condition, ClientInfo* info) {
    LXTermWindow * lxtermwin = info->lxtermwin;
    int fd = info->fd;
    /* 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);
    }

    if (condition & G_IO_HUP) {
        g_free(msg);
        g_free(info);
        close(fd);
        return FALSE;
    }

    return TRUE;
}

gboolean lxterminal_socket_initialize(LXTermWindow* lxtermwin, gint argc, gchar** argv) {
    return init(lxtermwin, argc, argv);
}