-
Notifications
You must be signed in to change notification settings - Fork 2
/
mauncher.c
331 lines (270 loc) · 7.63 KB
/
mauncher.c
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
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include "mauncher-win.h"
#include "mauncher-ipc.h"
#include "sysutil.h"
struct daemon_conn;
struct daemon_ctx {
GtkApplication *app;
int sockfd;
struct sockaddr_un addr;
GIOChannel *channels[16];
};
struct daemon_conn {
struct daemon_ctx *ctx;
GIOChannel **channel;
GSource *source;
};
struct daemon_invocation {
struct daemon_conn *conn;
struct daemon_message msg;
};
//static void daemon_free_channel(struct daemon_ctx *ctx, GIOChannel *channel) {
static void daemon_free_conn(struct daemon_conn *conn) {
GError *err = NULL;
g_io_channel_shutdown(*conn->channel, TRUE, &err);
if (err != NULL) {
fprintf(stderr, "Closing IO channel failed: %s\n", err->message);
g_error_free(err);
}
g_io_channel_unref(*conn->channel);
g_source_destroy(conn->source);
g_source_unref(conn->source);
*conn->channel = NULL;
}
static void win_callback(const char *output, int status, void *data) {
struct daemon_invocation *invocation = (struct daemon_invocation *)data;
struct daemon_reply reply = {
.reply = (char *)output,
.status = status,
};
daemon_reply_write(g_io_channel_unix_get_fd(*invocation->conn->channel), &reply);
daemon_free_conn(invocation->conn);
free(invocation->msg.payload);
free(invocation->msg.opts.prompt);
free(invocation->conn);
free(invocation);
}
static gboolean on_data(GIOChannel *source, GIOCondition condition, void *data) {
struct daemon_conn *conn = (struct daemon_conn *)data;
if (condition & G_IO_IN) {
struct daemon_invocation *invocation = malloc(sizeof(struct daemon_invocation));
invocation->conn = conn;
int fd = g_io_channel_unix_get_fd(source);
if (daemon_message_read(fd, &invocation->msg) < 0) {
daemon_free_conn(conn);
return FALSE;
}
mauncher_win_run(
conn->ctx->app, invocation->msg.payload, invocation->msg.opts,
&win_callback, invocation);
}
if (condition & G_IO_HUP) {
daemon_free_conn(conn);
return FALSE;
}
return TRUE;
}
static gboolean on_connect(GIOChannel *source, GIOCondition condition, gpointer data) {
struct daemon_ctx *ctx = (struct daemon_ctx *)data;
socklen_t socklen = sizeof(ctx->addr);
int fd = accept(ctx->sockfd, &ctx->addr, &socklen);
if (fd < 0) {
perror("accept");
return TRUE;
}
GIOChannel **channel = NULL;
for (size_t i = 0; i < sizeof(ctx->channels) / sizeof(*ctx->channels); ++i) {
if (ctx->channels[i] == NULL) {
channel = &ctx->channels[i];
break;
}
}
if (channel == NULL) {
printf("Client attempted connection, but we're full!\n");
close(fd);
return TRUE;
}
*channel = g_io_channel_unix_new(fd);
GSource *gsource = g_io_create_watch(*channel, G_IO_IN | G_IO_HUP);
struct daemon_conn *conn = malloc(sizeof(*conn));
conn->channel = channel;
conn->source = gsource;
conn->ctx = ctx;
g_source_set_callback(gsource, (GSourceFunc)&on_data, conn, NULL);
g_source_attach(conn->source, g_main_context_default());
return TRUE;
}
static void activate(GtkApplication *app, gpointer data) {
struct daemon_ctx *ctx = (struct daemon_ctx *)data;
ctx->app = app;
g_io_add_watch(g_io_channel_unix_new(ctx->sockfd), G_IO_IN, &on_connect, ctx);
gtk_main();
}
static int daemon_main(int closefd) {
chdir(g_get_home_dir());
struct daemon_ctx ctx = { 0 };
ctx.addr.sun_family = AF_UNIX;
char *sockpath = string_concat((char *[]) { xdg_runtime_dir(), "/mauncher-daemon.sock", NULL });
if (strlen(sockpath) >= sizeof(ctx.addr.sun_path)) {
fprintf(stderr, "Unix socket path too long: %s\n", sockpath);
free(sockpath);
return EXIT_FAILURE;
}
strcpy(ctx.addr.sun_path, sockpath);
free(sockpath);
ctx.sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (ctx.sockfd < 0) {
perror(ctx.addr.sun_path);
return EXIT_FAILURE;
}
if (bind(ctx.sockfd, (struct sockaddr *)&ctx.addr, sizeof(ctx.addr)) < 0) {
if (errno == EADDRINUSE) {
fprintf(stderr, "%s exists, removing it.\n", ctx.addr.sun_path);
if (unlink(ctx.addr.sun_path) < 0) {
perror(ctx.addr.sun_path);
close(ctx.sockfd);
return EXIT_FAILURE;
}
if (bind(ctx.sockfd, (struct sockaddr *)&ctx.addr, sizeof(ctx.addr)) < 0) {
perror(ctx.addr.sun_path);
close(ctx.sockfd);
}
} else {
perror(ctx.addr.sun_path);
close(ctx.sockfd);
return EXIT_FAILURE;
}
}
if (listen(ctx.sockfd, 2) < 0) {
perror(ctx.addr.sun_path);
close(ctx.sockfd);
return EXIT_FAILURE;
}
if (closefd >= 0)
close(closefd);
GtkApplication *app = gtk_application_new("coffee.mort.mauncher", G_APPLICATION_NON_UNIQUE);
g_signal_connect(app, "activate", G_CALLBACK(activate), &ctx);
int status = g_application_run(G_APPLICATION(app), 0, NULL);
g_object_unref(app);
return status;
}
static int daemon_fork() {
int fds[2];
if (pipe(fds) < 0) {
perror("pipe");
return -1;
}
pid_t child = fork();
if (child < 0) {
perror("fork");
close(fds[0]);
close(fds[1]);
return -1;
}
if (child == 0) {
close(fds[0]);
daemon(1, 0);
exit(daemon_main(fds[1]));
} else {
close(fds[1]);
char buf[1];
// Just wait for the child to close the pipe
if (read(fds[0], buf, 1) < 0) {
perror("read");
return -1;
}
}
return 0;
}
struct opts {
struct mauncher_win_opts winopts;
gboolean daemon;
};
int main(int argc, char **argv) {
struct opts opts = { 0 };
GOptionEntry optents[] = {
{
"prompt", 'p', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &opts.winopts.prompt,
"The prompt to be displayed left of the input field", NULL,
}, {
"insensitive", 'i', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opts.winopts.insensitive,
"Match case-insensitive", NULL,
}, {
"daemon", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opts.daemon,
"Spawn the mauncher daemon. This will be done automatically,\n"
" "
"but doing it explicitly on startup will make the first invocation faster.", NULL,
},
{ 0 },
};
GOptionContext *optctx = g_option_context_new(NULL);
g_option_context_add_main_entries(optctx, optents, NULL);
GError *err = NULL;
g_option_context_parse(optctx, &argc, &argv, &err);
g_option_context_free(optctx);
if (err != NULL) {
fprintf(stderr, "%s\n", err->message);
g_error_free(err);
return EXIT_FAILURE;
}
if (opts.daemon)
return daemon_main(-1);
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
char *sockpath = string_concat((char *[]) { xdg_runtime_dir(), "/mauncher-daemon.sock", NULL });
if (strlen(sockpath) >= sizeof(addr.sun_path)) {
fprintf(stderr, "Unix socket path too long: %s\n", sockpath);
free(sockpath);
return EXIT_FAILURE;
}
strcpy(addr.sun_path, sockpath);
free(sockpath);
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
perror(addr.sun_path);
printf("socket\n");
return EXIT_FAILURE;
}
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
if (errno == ENOENT || errno == ECONNREFUSED) {
if (daemon_fork() < 0) {
close(sockfd);
return EXIT_FAILURE;
}
if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror(addr.sun_path);
close(sockfd);
return EXIT_FAILURE;
}
} else {
perror(addr.sun_path);
close(sockfd);
return EXIT_FAILURE;
}
}
struct daemon_message msg = { 0 };
size_t payload_len;
msg.payload = read_all(STDIN_FILENO, &payload_len);
memcpy(&msg.opts, &opts.winopts, sizeof(msg.opts));
if (daemon_message_write(sockfd, &msg) < 0) {
free(msg.payload);
close(sockfd);
return EXIT_FAILURE;
}
free(msg.payload);
struct daemon_reply reply;
if (daemon_reply_read(sockfd, &reply) < 0) {
close(sockfd);
return EXIT_FAILURE;
}
close(sockfd);
if (reply.status == EXIT_SUCCESS)
puts(reply.reply);
free(reply.reply);
return reply.status;
}