opt: more reasonable method to set scoket flag
Some checks failed
Sync website / sync-website (push) Has been cancelled
Sync wiki / sync-wiki (push) Has been cancelled

This commit is contained in:
DreamMaoMao 2026-05-30 11:51:39 +08:00
parent 8cb3a40ac7
commit 6849e5b4db
2 changed files with 43 additions and 21 deletions

View file

@ -217,7 +217,7 @@ static cJSON *build_monitor_tags_response(Monitor *m) {
static void send_static_json(int fd, const char *json_str) { static void send_static_json(int fd, const char *json_str) {
size_t len = strlen(json_str); size_t len = strlen(json_str);
send(fd, json_str, len, MSG_NOSIGNAL); send(fd, json_str, len, 0);
} }
/* ---------- 一次性命令处理 ---------- */ /* ---------- 一次性命令处理 ---------- */
@ -413,7 +413,7 @@ static void handle_command(int client_fd, const char *cmd_raw) {
char *msg = malloc(len + 2); char *msg = malloc(len + 2);
if (msg) { if (msg) {
snprintf(msg, len + 2, "%s\n", json_str); snprintf(msg, len + 2, "%s\n", json_str);
send(client_fd, msg, len + 1, MSG_NOSIGNAL); send(client_fd, msg, len + 1, 0);
free(msg); free(msg);
} }
free(json_str); free(json_str);
@ -434,7 +434,7 @@ static void ipc_notify_json_to_fd(int fd, cJSON *json) {
return; return;
} }
snprintf(msg, len + 2, "%s\n", str); snprintf(msg, len + 2, "%s\n", str);
if (send(fd, msg, len + 1, MSG_NOSIGNAL) < 0) { if (send(fd, msg, len + 1, 0) < 0) {
struct ipc_watch_client *wc, *tmp; struct ipc_watch_client *wc, *tmp;
wl_list_for_each_safe(wc, tmp, &watch_clients, link) { wl_list_for_each_safe(wc, tmp, &watch_clients, link) {
if (wc->fd == fd) { if (wc->fd == fd) {
@ -462,7 +462,7 @@ static int ipc_watch_data_handler(int fd, uint32_t mask, void *data) {
} }
if (mask & WL_EVENT_READABLE) { if (mask & WL_EVENT_READABLE) {
char buf[64]; char buf[64];
ssize_t n = recv(fd, buf, sizeof(buf), MSG_DONTWAIT); ssize_t n = recv(fd, buf, sizeof(buf), 0);
if (n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) { if (n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)) {
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
@ -643,8 +643,7 @@ static int ipc_handle_client_data(int fd, uint32_t mask, void *data) {
available = client->buf_cap - client->buf_len; available = client->buf_cap - client->buf_len;
} }
ssize_t n = recv(fd, client->buf + client->buf_len, available - 1, ssize_t n = recv(fd, client->buf + client->buf_len, available - 1, 0);
MSG_DONTWAIT);
if (n <= 0) if (n <= 0)
goto cleanup; goto cleanup;
@ -683,8 +682,12 @@ static int ipc_handle_connection(int fd, uint32_t mask, void *data) {
if (client_fd < 0) if (client_fd < 0)
return 0; return 0;
// 设置 O_NONBLOCK
int flags = fcntl(client_fd, F_GETFL, 0); int flags = fcntl(client_fd, F_GETFL, 0);
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK); fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
// 设置 FD_CLOEXEC
flags = fcntl(client_fd, F_GETFD, 0);
fcntl(client_fd, F_SETFD, flags | FD_CLOEXEC);
struct ipc_client_state *client = calloc(1, sizeof(*client)); struct ipc_client_state *client = calloc(1, sizeof(*client));
client->fd = client_fd; client->fd = client_fd;
@ -715,7 +718,7 @@ void ipc_notify_monitor(Monitor *m) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -731,10 +734,8 @@ void ipc_notify_last_surface_ws_name(Monitor *m) {
if (wc->type != IPC_WATCH_LAST_OPEN_SURFACE) if (wc->type != IPC_WATCH_LAST_OPEN_SURFACE)
continue; continue;
/* 匹配具体 monitor 名称,或空名称表示默认 selmon */
bool match = false; bool match = false;
if (wc->target.monitor.name[0] == '\0') { if (wc->target.monitor.name[0] == '\0') {
/* 订阅的是 selmon */
if (m == selmon) if (m == selmon)
match = true; match = true;
} else { } else {
@ -759,7 +760,7 @@ void ipc_notify_last_surface_ws_name(Monitor *m) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
free(json_str); free(json_str);
@ -790,7 +791,7 @@ void ipc_notify_focusing_client(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -814,7 +815,7 @@ void ipc_notify_client(Client *c) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -840,7 +841,7 @@ void ipc_notify_tags(Monitor *m) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -870,7 +871,7 @@ void ipc_notify_all_monitors(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -900,7 +901,7 @@ void ipc_notify_all_clients(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -925,7 +926,7 @@ void ipc_notify_all_tags(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -951,7 +952,7 @@ void ipc_notify_keymode(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -977,7 +978,7 @@ void ipc_notify_kb_layout(void) {
snprintf(json_str, len + 2, "%s\n", raw); snprintf(json_str, len + 2, "%s\n", raw);
free(raw); free(raw);
} }
if (send(wc->fd, json_str, len + 1, MSG_NOSIGNAL) < 0) if (send(wc->fd, json_str, len + 1, 0) < 0)
ipc_remove_watch_client(wc); ipc_remove_watch_client(wc);
} }
} }
@ -1000,11 +1001,25 @@ void ipc_init(struct wl_event_loop *event_loop) {
snprintf(ipc_socket_path, sizeof(ipc_socket_path), "%s/mango-%d.sock", snprintf(ipc_socket_path, sizeof(ipc_socket_path), "%s/mango-%d.sock",
xdg_runtime, getpid()); xdg_runtime, getpid());
ipc_sock_fd = ipc_sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (ipc_sock_fd < 0) if (ipc_sock_fd < 0)
return; return;
// 设置 FD_CLOEXEC
int flags = fcntl(ipc_sock_fd, F_GETFD, 0);
if (flags == -1 || fcntl(ipc_sock_fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
wlr_log(WLR_ERROR, "failed to set FD_CLOEXEC on IPC socket");
close(ipc_sock_fd);
return;
}
// 设置 O_NONBLOCK
flags = fcntl(ipc_sock_fd, F_GETFL, 0);
if (flags == -1 || fcntl(ipc_sock_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
wlr_log(WLR_ERROR, "failed to set O_NONBLOCK on IPC socket");
close(ipc_sock_fd);
return;
}
struct sockaddr_un addr = {.sun_family = AF_UNIX}; struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, ipc_socket_path, sizeof(addr.sun_path) - 1); strncpy(addr.sun_path, ipc_socket_path, sizeof(addr.sun_path) - 1);

View file

@ -5805,13 +5805,20 @@ void setup(void) {
} }
init_baked_points(); init_baked_points();
int32_t drm_fd, i, sig[] = {SIGCHLD, SIGINT, SIGTERM, SIGPIPE}; int32_t drm_fd, i;
int32_t sig[] = {SIGCHLD, SIGINT,
SIGTERM}; // 不设置SIGPIPE,因为ipc发送失败不应该影响主程序
struct sigaction sa = {.sa_flags = SA_RESTART, .sa_handler = handlesig}; struct sigaction sa = {.sa_flags = SA_RESTART, .sa_handler = handlesig};
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
for (i = 0; i < LENGTH(sig); i++) for (i = 0; i < LENGTH(sig); i++)
sigaction(sig[i], &sa, NULL); sigaction(sig[i], &sa, NULL);
// 单独为 SIGPIPE 设置忽略
struct sigaction sa_pipe = {.sa_flags = 0, .sa_handler = SIG_IGN};
sigemptyset(&sa_pipe.sa_mask);
sigaction(SIGPIPE, &sa_pipe, NULL);
wlr_log_init(config.log_level, NULL); wlr_log_init(config.log_level, NULL);
/* The Wayland display is managed by libwayland. It handles accepting /* The Wayland display is managed by libwayland. It handles accepting