File: main.c

package info (click to toggle)
xwrited 2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 188 kB
  • ctags: 152
  • sloc: ansic: 772; xml: 135; makefile: 131; sed: 26
file content (509 lines) | stat: -rw-r--r-- 13,290 bytes parent folder | download
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * Copyright (C) 2015 Guido Berhoerster <guido+xwrited@berhoerster.name>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define	_XOPEN_SOURCE	600

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <locale.h>
#include <libintl.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <libnotify/notify.h>
#include "xwrited-debug.h"
#include "xwrited-unique.h"
#include "xwrited-utmp.h"

#define BUFFER_TIMEOUT (250)

enum {
	PIPE_R_FD = 0,
	PIPE_W_FD
};

static int		signal_pipe_fd[2] = { -1, -1 };
static guint		notify_timeout_id;
static GMainLoop	*loop;
static GString		*buffer;

static void
on_signal(int signo)
{
	int		old_errno = errno;
	ssize_t		n;
	sigset_t	sigset;

	/* try to read unread signals from the pipe and add the new one to it */
	n = read(signal_pipe_fd[PIPE_R_FD], &sigset, sizeof (sigset));
	if ((n == -1) || ((size_t)n < sizeof (sigset))) {
		sigemptyset(&sigset);
	}
	sigaddset(&sigset, signo);
	write(signal_pipe_fd[PIPE_W_FD], &sigset, sizeof (sigset));

	errno = old_errno;
}

static gboolean
signal_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data)
{
	sigset_t	sigset;
	sigset_t	old_sigset;
	GIOStatus	status;
	gsize		n;
	GError		*error = NULL;

	/*
	 * deal with pending signals previously received in the signal handler,
	 * try to read a sigset from the pipe, avoid partial reads by blocking
	 * all signals during the read operation
	 */
	sigfillset(&sigset);
	sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
	status = g_io_channel_read_chars(source, (gchar *)&sigset,
	    sizeof (sigset), &n, &error);
	sigprocmask(SIG_SETMASK, &old_sigset, NULL);
	if (status != G_IO_STATUS_NORMAL) {
		if (status != G_IO_STATUS_AGAIN) {
			if (error != NULL) {
				g_critical("failed to read from signal pipe: "
				    "%s", error->message);
				g_error_free(error);
				g_main_loop_quit(loop);
			} else {
				g_critical("failed to read from signal pipe");
				g_main_loop_quit(loop);
			}
		}
	} else if (n != sizeof (sigset)) {
		g_critical("short read from signal pipe");
		g_main_loop_quit(loop);
	} else {
		if ((sigismember(&sigset, SIGINT) == 1) ||
		    (sigismember(&sigset, SIGTERM) == 1) ||
		    (sigismember(&sigset, SIGQUIT) == 1) ||
		    (sigismember(&sigset, SIGHUP) == 1)) {
			g_debug("received signal, exiting");
			g_main_loop_quit(loop);
		}
	}

	return (TRUE);
}

static gboolean
send_notification(void)
{
	gboolean	retval = FALSE;
	GString		*utf8_str = NULL;
	gchar		*startp = buffer->str;
	gchar		*endp;
	GRegex		*regex = NULL;
	GError		*error = NULL;
	gchar		*body = NULL;
	GList		*capabilities = NULL;
	gchar		*tmp;
	NotifyNotification *notification = NULL;

	utf8_str = g_string_sized_new(buffer->len);
	while (!g_utf8_validate(startp, buffer->str + buffer->len -
	    startp, (const gchar **)&endp)) {
		g_string_append_len(utf8_str, startp, endp - startp);
		/*
		 * replace each byte that does not belong to a UTF-8-encoded
		 * character with the Unicode REPLACEMENT CHARACTER (U+FFFD)
		 */
		g_string_append(utf8_str, "\357\277\275");

		startp = endp + ((endp < buffer->str + buffer->len) ? 1 : 0);
	}
	g_string_append_len(utf8_str, startp, buffer->str + buffer->len -
	    startp);

	/* remove any CR, BEL and trailing space and tabs */
	regex = g_regex_new("([\r\a]+|[ \t\r\a]+$)", G_REGEX_MULTILINE, 0,
	    &error);
	if (error != NULL) {
		g_critical("failed to create regex object: %s",
		    error->message);
		g_error_free(error);
		goto out;
	}
	body = g_regex_replace_literal(regex, utf8_str->str, -1, 0, "", 0,
	    &error);
	if (error != NULL) {
		g_critical("failed to replace control and space characters: "
		    "%s", error->message);
		g_error_free(error);
		goto out;
	}

	/*
	 * skip empty messages or messages only consisting of whitespace and
	 * control characters
	 */
	if ((strlen(body) == 0) ||
	    !g_regex_match_simple("[^[:space:][:cntrl:]]", body, 0, 0)) {
		retval = TRUE;
		goto out;
	}

	/*
	 * if the notification daemon supports markup the message needs to be
	 * escaped
	 */
	capabilities = notify_get_server_caps();
	if (g_list_find_custom(capabilities, "body-markup",
	    (GCompareFunc)strcmp) != NULL) {
		tmp = g_markup_escape_text(body, -1);
		g_free(body);
		body = tmp;
	}

	/* show notification */
	notification = notify_notification_new(_("Message received"),
	    body, "utilities-terminal"
#if !defined(NOTIFY_VERSION_MINOR) || \
    (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
	    , NULL
#endif
	    );
	if (notification == NULL) {
		g_critical("failed to create a notification object");
		g_main_loop_quit(loop);
		goto out;
	}
	notify_notification_set_timeout(notification, NOTIFY_EXPIRES_NEVER);
	retval = notify_notification_show(notification, NULL);

out:
	if (notification != NULL) {
		g_object_unref(G_OBJECT(notification));
	}
	if (capabilities != NULL) {
		g_list_free_full(capabilities, g_free);
	}
	g_free(body);
	if (regex != NULL) {
		g_regex_unref(regex);
	}
	if (utf8_str != NULL) {
		g_string_free(utf8_str, TRUE);
	}
	/* prevent a permanently large buffer */
	g_string_free(buffer, FALSE);
	buffer = g_string_sized_new(BUFSIZ);

	return (retval);
}

static gboolean
notify_timeout_cb(gpointer user_data)
{
	if (!send_notification()) {
		g_warning("failed to send notification");
	}

	notify_timeout_id = 0;

	return (FALSE);
}

static gboolean
master_pty_read_cb(GIOChannel *source, GIOCondition cond, gpointer user_data)
{
	gchar		buf[BUFSIZ];
	GIOStatus	status;
	gsize		buf_len;
	GError		*error = NULL;

	if ((cond & G_IO_IN) || (cond & G_IO_PRI)) {
		/* read message from master pty */
		while ((status = g_io_channel_read_chars(source, buf, BUFSIZ,
		    &buf_len, &error)) == G_IO_STATUS_NORMAL) {
			if (buf_len > 0) {
				g_debug("read %" G_GSSIZE_FORMAT " bytes from "
				    "master pty", buf_len);
				g_string_append_len(buffer, buf,
				    (gssize)buf_len);
			}
		}
		if (error != NULL) {
			g_critical("failed to read from master pty: %s",
			    error->message);
			g_error_free(error);
			g_main_loop_quit(loop);
			return (FALSE);
		}

		/*
		 * schedule a timeout for sending a notification with the
		 * buffered message
		 */
		if (notify_timeout_id <= 0) {
			notify_timeout_id = g_timeout_add(BUFFER_TIMEOUT,
			    notify_timeout_cb, NULL);
		}
	}

	if ((cond & G_IO_ERR) || (cond & G_IO_HUP)) {
		g_critical("connection to master pty broken");
		g_main_loop_quit(loop);
		return (FALSE);
	}

	return (TRUE);
}

int
main(int argc, char *argv[])
{
	int		status = EXIT_FAILURE;
	GError		*error = NULL;
	XWritedUnique	*app = NULL;
	GOptionContext	*context = NULL;
	struct sigaction sigact;
	GIOChannel	*signal_channel = NULL;
	GIOChannel	*master_pty_channel = NULL;
	int		masterfd = -1;
	int		slavefd = -1;
	char		*slave_name = NULL;
	gboolean	vflag = FALSE;
	gboolean	dflag = FALSE;
	const GOptionEntry options[] = {
	    { "debug", 'd', 0, G_OPTION_ARG_NONE, &dflag,
	    N_("Show extra debugging information"), NULL },
	    { "version", 'V', 0, G_OPTION_ARG_NONE, &vflag,
	    N_("Print the current version and exit"), NULL },
	    { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	bind_textdomain_codeset(PACKAGE, "UTF-8");
	textdomain(PACKAGE);

#if !GLIB_CHECK_VERSION(2, 35, 0)
	/* deprecated in glib >= 2.35 */
	g_type_init();
#endif

	context = g_option_context_new("- display write and wall messages as "
	    "desktop notifications");
	g_option_context_add_main_entries(context, options, PACKAGE);
	g_option_context_set_translation_domain(context, PACKAGE);
	g_option_context_parse(context, &argc, &argv, &error);
	if (error != NULL) {
		g_printerr("%s.\n", error->message);
		g_error_free(error);
		goto out;
	}

	xwrited_debug_init(dflag);

	if (vflag) {
		g_print("%s %s\n", PACKAGE, VERSION);
		status = EXIT_SUCCESS;
		goto out;
	}

	app = xwrited_unique_new("org.guido-berhoerster.code.xwrited");
	if (app == NULL) {
		g_critical("failed to initialize application");
		goto out;
	}
	if (!xwrited_unique_is_unique(app)) {
		g_printerr(_("xwrited is already running in this session.\n"));
		goto out;
	}

	if (!notify_init(APP_NAME)) {
		g_critical("failed to initialize libnotify");
		goto out;
	}

	loop = g_main_loop_new(NULL, FALSE);
	if (loop == NULL) {
		g_critical("failed to create main loop");
		goto out;
	}

	buffer = g_string_sized_new(BUFSIZ);

	/* open master pty */
	masterfd = posix_openpt(O_RDWR | O_NOCTTY);
	if (masterfd == -1) {
		g_critical("failed to open master pty: %s", g_strerror(errno));
		goto out;
	}

	/* create slave pty */
	if ((grantpt(masterfd) == -1) || (unlockpt(masterfd) == -1)) {
		g_critical("failed to create slave pty: %s", g_strerror(errno));
		goto out;
	}
	slave_name = ptsname(masterfd);
	if (slave_name == NULL) {
		g_critical("failed to obtain name of slave pty");
		goto out;
	}

	/*
	 * keep an open fd around order to prevent closing the master fd when
	 * receiving an EOF
	 */
	slavefd = open(slave_name, O_RDWR);
	if (slavefd == -1) {
		g_critical("failed to open slave pty: %s", g_strerror(errno));
		goto out;
	}

	/* create a GIOChannel for monitoring the master pty for messages */
	master_pty_channel = g_io_channel_unix_new(masterfd);
	g_io_channel_set_flags(master_pty_channel,
	    g_io_channel_get_flags(master_pty_channel) | G_IO_FLAG_NONBLOCK,
	    &error);
	if (error != NULL) {
		g_critical("failed set flags on the master pty channel: %s",
		    error->message);
		g_error_free(error);
		goto out;
	}
	if (!g_io_add_watch(master_pty_channel, G_IO_IN | G_IO_PRI | G_IO_HUP |
	    G_IO_ERR, master_pty_read_cb, NULL)) {
		g_critical("failed to add watch on signal channel");
		goto out;
	}

	/* create pipe for delivering signals to a listener in the main loop */
	if (pipe(signal_pipe_fd) == -1) {
		g_critical("failed to create signal pipe: %s",
		    g_strerror(errno));
		goto out;
	}
	if (fcntl(signal_pipe_fd[PIPE_W_FD], F_SETFL, O_NONBLOCK) == -1) {
		g_critical("failed to set flags on signal pipe: %s",
		    g_strerror(errno));
		goto out;
	}

	/* create GIO channel for reading from the signal_pipe */
	signal_channel = g_io_channel_unix_new(signal_pipe_fd[PIPE_R_FD]);
	g_io_channel_set_encoding(signal_channel, NULL, &error);
	if (error != NULL) {
		g_critical("failed to set binary encoding for signal channel: "
		    "%s", error->message);
		g_error_free(error);
		goto out;
	}
	g_io_channel_set_buffered(signal_channel, FALSE);
	g_io_channel_set_flags(signal_channel,
	    g_io_channel_get_flags(signal_channel) | G_IO_FLAG_NONBLOCK,
	    &error);
	if (error != NULL) {
		g_critical("failed set flags on signal channel: %s",
		    error->message);
		g_error_free(error);
		goto out;
	}
	if (g_io_add_watch(signal_channel, G_IO_IN | G_IO_PRI | G_IO_HUP |
	    G_IO_ERR, signal_read_cb, NULL) == 0) {
		g_critical("failed to add watch on the signal channel");
		goto out;
	}

	/* set up signal handler */
	sigact.sa_handler = on_signal;
	sigact.sa_flags = SA_RESTART;
	sigemptyset(&sigact.sa_mask);
	if ((sigaction(SIGINT, &sigact, NULL) < 0) ||
	    (sigaction(SIGTERM, &sigact, NULL) < 0) ||
	    (sigaction(SIGQUIT, &sigact, NULL) < 0) ||
	    (sigaction(SIGHUP, &sigact, NULL) < 0)) {
		g_critical("failed to set up signal handler");
		goto out;
	}

	xwrited_utmp_add_entry(masterfd);

	/* main loop */
	g_main_loop_run(loop);

	xwrited_utmp_remove_entry(masterfd);

	status = EXIT_SUCCESS;

out:
	if (context != NULL) {
		g_option_context_free(context);
	}

	if (signal_channel != NULL) {
		g_io_channel_shutdown(signal_channel, FALSE, NULL);
		g_io_channel_unref(signal_channel);
	}

	if (signal_pipe_fd[PIPE_R_FD] != -1) {
		close(signal_pipe_fd[PIPE_R_FD]);
	}
	if (signal_pipe_fd[PIPE_W_FD] != -1) {
		close(signal_pipe_fd[PIPE_W_FD]);
	}

	if (master_pty_channel != NULL) {
		g_io_channel_shutdown(master_pty_channel, FALSE, NULL);
		g_io_channel_unref(master_pty_channel);
	}

	if (slavefd != -1) {
		close(slavefd);
	}

	if (masterfd != -1) {
		close(masterfd);
	}

	if (buffer != NULL) {
		g_string_free(buffer, FALSE);
	}

	if (app != NULL) {
		g_object_unref(app);
	}

	if (loop != NULL) {
		g_main_loop_unref(loop);
	}

	if (notify_is_initted()) {
		notify_uninit();
	}

	exit(status);
}