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
|
#include "ctrl.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/un.h>
#include <sys/types.h>
#include <tllist.h>
#define LOG_MODULE "ctrl"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "ctrl-protocol.h"
#include "dbus.h"
extern volatile sig_atomic_t aborted;
struct ctrl;
struct client {
struct ctrl *ctrl;
int fd;
struct {
union {
struct ctrl_request cmd;
uint8_t raw[sizeof(struct ctrl_request)];
};
size_t idx;
} recv;
};
struct ctrl {
struct fdm *fdm;
struct notif_mgr *notif_mgr;
struct dbus *bus;
int server_fd;
char *socket_path;
tll(struct client) clients;
};
static bool
send_reply(int fd, const struct ctrl_reply *reply)
{
LOG_DBG("client: FD=%d, reply: result=%s",
fd, reply->result == CTRL_OK ? "ok" : "fail");
if (write(fd, reply, sizeof(*reply)) != sizeof(*reply)) {
LOG_ERRNO("client: FD=%d: failed to send reply", fd);
return false;
}
return true;
}
static void
client_disconnected(struct ctrl *ctrl, int fd)
{
LOG_DBG("client: FD=%d disconnected", fd);
fdm_del(ctrl->fdm, fd);
tll_foreach(ctrl->clients, it) {
if (it->item.fd == fd) {
tll_remove(ctrl->clients, it);
break;
}
}
}
struct actions_cb_data {
struct ctrl *ctrl;
int client_fd;
};
static void
actions_complete(struct notif *notif, const char *action_id, void *data)
{
struct actions_cb_data *info = data;
struct ctrl *ctrl = info->ctrl;
int fd = info->client_fd;
struct ctrl_reply reply = {.result = CTRL_INVALID_ID};
LOG_DBG("actions callback: notif=%u, ID=%s", notif_id, action_id);
if (action_id == NULL)
goto out;
reply.result = notif_signal_action(notif, action_id) ? CTRL_OK : CTRL_ERROR;
out:
send_reply(fd, &reply);
client_disconnected(ctrl, fd);
free(info);
}
static enum ctrl_result
actions_by_id(struct ctrl *ctrl, int fd, uint32_t id)
{
struct notif *notif = notif_mgr_get_notif(ctrl->notif_mgr, id);
if (notif == NULL)
return CTRL_INVALID_ID;
if (notif_action_count(notif) == 0)
return CTRL_NO_ACTIONS;
struct actions_cb_data *info = malloc(sizeof(*info));
*info = (struct actions_cb_data){.ctrl = ctrl, .client_fd = fd};
notif_select_action(notif, &actions_complete, info);
return CTRL_OK;
}
static bool
fdm_client(struct fdm *fdm, int fd, int events, void *data)
{
struct client *client = data;
struct ctrl *ctrl = client->ctrl;
assert(client->fd == fd);
bool ret = false;
uint32_t *ids = NULL;
int64_t id_count = -1;
size_t left = sizeof(client->recv.cmd) - client->recv.idx;
ssize_t count = recv(fd, &client->recv.raw[client->recv.idx], left, 0);
if (count < 0) {
LOG_ERRNO("client: FD=%d: failed to receive command", fd);
return false;
}
client->recv.idx += count;
if (client->recv.idx < sizeof(client->recv.cmd)) {
/* Haven’t received a full command yet */
goto no_err;
}
assert(client->recv.idx == sizeof(client->recv.cmd));
/* TODO: endianness */
struct ctrl_reply reply;
switch (client->recv.cmd.cmd) {
case CTRL_QUIT:
LOG_DBG("client: FD=%d, quit", fd);
aborted = 1;
reply.result = CTRL_OK;
break;
case CTRL_LIST:
id_count = notif_mgr_get_ids(ctrl->notif_mgr, NULL, 0);
LOG_INFO("got %"PRIi64" IDs", id_count);
reply.result = id_count >= 0 ? CTRL_OK : CTRL_ERROR;
if (id_count >= 0) {
if (id_count > 0)
ids = calloc(id_count, sizeof(ids[0]));
notif_mgr_get_ids(ctrl->notif_mgr, ids, id_count);
}
break;
case CTRL_PAUSE:
LOG_DBG("client: FD=%d, pause", fd);
notif_mgr_pause(ctrl->notif_mgr);
break;
case CTRL_UNPAUSE:
LOG_DBG("client: FD=%d, unpause", fd);
notif_mgr_unpause(ctrl->notif_mgr);
break;
case CTRL_DISMISS_BY_ID:
LOG_DBG("client: FD=%d, dismiss by-id: %u", fd, client->recv.cmd.id);
reply.result = notif_mgr_dismiss_id(ctrl->notif_mgr, client->recv.cmd.id)
? CTRL_OK : CTRL_INVALID_ID;
break;
case CTRL_DISMISS_ALL:
LOG_DBG("client: FD=%d, dismiss all", fd);
reply.result = notif_mgr_dismiss_all(ctrl->notif_mgr)
? CTRL_OK : CTRL_ERROR;
break;
case CTRL_ACTIONS_BY_ID:
LOG_DBG("client: FD=%d, actions by-id: %u", fd, client->recv.cmd.id);
if ((reply.result = actions_by_id(ctrl, fd, client->recv.cmd.id)) == CTRL_OK) {
/* Action selection helper successfully started, wait for
* response before sending reply to fnottctl */
goto no_reply;
}
break;
case CTRL_DISMISS_WITH_DEFAULT_ACTION_BY_ID: {
LOG_DBG("client: FD=%d, dismiss-with-default-action: %u", fd, client->recv.cmd.id);
struct notif *notif =
notif_mgr_get_notif(ctrl->notif_mgr, client->recv.cmd.id);
if (notif != NULL) {
notif_signal_action(notif, "default");
reply.result =
notif_mgr_dismiss_id(ctrl->notif_mgr, client->recv.cmd.id)
? CTRL_OK : CTRL_INVALID_ID;
} else
reply.result = CTRL_INVALID_ID;
break;
}
}
if (!send_reply(fd, &reply))
goto err;
if (reply.result == CTRL_OK && id_count >= 0) {
if (write(fd, &id_count, sizeof(id_count)) != sizeof(id_count)) {
LOG_ERRNO("failed to write 'list' response");
goto err;
}
for (size_t i = 0; i < id_count; i++) {
char *summary = NULL;
uint32_t len = 0;
const struct notif *notif = notif_mgr_get_notif(ctrl->notif_mgr, ids[i]);
if (notif != NULL) {
summary = notif_get_summary(notif);
len = strlen(summary);
}
if (write(fd, &ids[i], sizeof(ids[i])) != sizeof(ids[i]) ||
write(fd, &len, sizeof(len)) != sizeof(len) ||
write(fd, summary != NULL ? summary : "", len) != len)
{
LOG_ERRNO("failed to write 'list' response");
free(summary);
goto err;
}
free(summary);
}
}
no_err:
ret = true;
err:
free(ids);
if ((events & EPOLLHUP) || client->recv.idx >= sizeof(client->recv.cmd)) {
/* Client disconnected */
client_disconnected(ctrl, fd);
}
return ret;
no_reply:
free(ids);
return true;
}
static bool
fdm_server(struct fdm *fdm, int fd, int events, void *data)
{
if (events & EPOLLHUP) {
LOG_ERR("disconnected from controller UNIX socket");
return false;
}
struct ctrl *ctrl = data;
struct sockaddr_un addr;
socklen_t addr_size = sizeof(addr);
int client_fd = accept4(ctrl->server_fd, (struct sockaddr *)&addr, &addr_size, SOCK_CLOEXEC);
if (client_fd == -1) {
LOG_ERRNO("failed to accept client connection");
return false;
}
LOG_DBG("client FD=%d connected", client_fd);
tll_push_back(ctrl->clients, ((struct client){.ctrl = ctrl, .fd = client_fd}));
struct client *client = &tll_back(ctrl->clients)
;
if (!fdm_add(ctrl->fdm, client_fd, EPOLLIN, &fdm_client, client)) {
LOG_ERR("failed to register client FD with FDM");
tll_pop_back(ctrl->clients);
close(client_fd);
return false;
}
return true;
}
static char *
get_socket_path(void)
{
const char *xdg_runtime = getenv("XDG_RUNTIME_DIR");
if (xdg_runtime == NULL)
return strdup("/tmp/fnott.sock");
const char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display == NULL) {
char *path = malloc(strlen(xdg_runtime) + 1 + strlen("fnott.sock") + 1);
sprintf(path, "%s/fnott.sock", xdg_runtime);
return path;
}
char *path = malloc(strlen(xdg_runtime) + 1 + strlen("fnott-.sock") + strlen(wayland_display) + 1);
sprintf(path, "%s/fnott-%s.sock", xdg_runtime, wayland_display);
return path;
}
struct ctrl *
ctrl_init(struct fdm *fdm, struct notif_mgr *notif_mgr, struct dbus *bus)
{
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd == -1) {
LOG_ERRNO("failed to create UNIX socket");
return NULL;
}
char *sock_path = get_socket_path();
if (sock_path == NULL)
goto err;
unlink(sock_path);
struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
LOG_ERRNO("%s: failed to bind", addr.sun_path);
goto err;
}
if (listen(fd, 5) < 0) {
LOG_ERRNO("%s: failed to listen", addr.sun_path);
goto err;
}
struct ctrl *ctrl = malloc(sizeof(*ctrl));
*ctrl = (struct ctrl){
.fdm = fdm,
.bus = bus,
.notif_mgr = notif_mgr,
.server_fd = fd,
.socket_path = sock_path,
.clients = tll_init(),
};
if (!fdm_add(fdm, fd, EPOLLIN, &fdm_server, ctrl)) {
LOG_ERR("failed to register with FDM");
goto err;
}
return ctrl;
err:
if (sock_path)
free(sock_path);
if (fd != -1)
close(fd);
return NULL;
}
void
ctrl_destroy(struct ctrl *ctrl)
{
if (ctrl == NULL)
return;
fdm_del(ctrl->fdm, ctrl->server_fd);
if (ctrl->socket_path != NULL)
unlink(ctrl->socket_path);
free(ctrl->socket_path);
free(ctrl);
}
int
ctrl_poll_fd(const struct ctrl *ctrl)
{
return ctrl->server_fd;
}
|