Files
ssw-platform/scripts/ipc-reap.c
T
sukyboor 9771fa844d init: 尚数网平台 JNPF 7.0 兼容后端(多应用 + 面 A/面 B 双契约)
- axum 0.8 + pglite:12 张表,多应用按 App-Code 头解析 f_system_id 隔离
- 面 A(ssw/react 形状)+ 面 B(官方 JNPF 形状,Vue-Version: 3)双契约并存
- 业务模块 asset/DataResource;面 B 用户 CRUD 9 条已落地
- 六道闸门 + 两把运行时尺子(perm-smoke 43 / perm-smoke-jnpf 23)
- scripts/:前端接口清单、页面依赖反查、运行时探针
2026-10-30 22:08:31 +08:00

124 lines
4.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* ipc-reap —— macOS(SysV) 信号量泄漏回收器
*
* 🔴 为什么需要它:
* macOS(XNU) 的 SysV 信号量集**在创建进程退出后不回收**(与 Linux 不同,
* 且 macOS 不带 ipcrm/ipcs)⇒ 每次 `cargo test` 都会新建若干 PGlite 实例
* (本仓 7 个测试文件各 1 个 `Db::open_temp()`),
* 每个实例在 initdb 阶段 semget 一个 **nsems=20** 的 set,进程退出后这 20 个
* 信号量就永久留在系统表里。
* 累积到 kern.sysv.semmns(87381)上限后,**任何新的 PostgreSQL 实例都起不来**
* (initdb 报 `semget(...): No space left on device`)。
*
* 回收口径(刻意保守,宁可漏回收也不误伤活进程):
* ① 只回收 nsems == 20(= PGlite/postgres-pglite 的指纹)的 set;
* ② 只回收创建者 uid == 当前用户的 set(不碰 root / 其他用户的);
* ③ 只回收「最近一次操作时间」距今超过 --min-idle 秒的 set
* (活着的 PostgreSQL 会持续 semop ⇒ otime 是新的 ⇒ 不会被误删)。
*
* 用法:
* cc -O2 -o ipc-reap ipc-reap.c && ./ipc-reap [--dry-run] [--nsems 20]
* [--min-idle 3600] [--max-id 2000000]
* 退出码:0 = 正常(--dry-run 时不落任何修改) / 2 = 参数错
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/sem.h>
static int g_dry_run = 0;
static int g_nsems = 20; /* PGlite 指纹 */
static long g_min_idle = 3600; /* 秒:最近操作距今超过它才算「死的」 */
static long g_max_id = 2000000;
static void usage(void)
{
fprintf(stderr,
"用法: ipc-reap [--dry-run] [--nsems N] [--min-idle SECONDS] [--max-id N]\n");
exit(2);
}
/** 当前还能新建多大的 set(每个建完立刻释放 ⇒ 净中性)⇒ 余量。 */
static long free_capacity(void)
{
long lo = 0, hi = 4096;
while (lo < hi) {
long mid = lo + (hi - lo + 1) / 2;
int id = semget(IPC_PRIVATE, (int)mid, 0600 | IPC_CREAT);
if (id == -1) {
hi = mid - 1;
} else {
semctl(id, 0, IPC_RMID);
lo = mid;
}
}
return lo;
}
int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--dry-run") == 0) {
g_dry_run = 1;
} else if (strcmp(argv[i], "--nsems") == 0 && i + 1 < argc) {
g_nsems = atoi(argv[++i]);
} else if (strcmp(argv[i], "--min-idle") == 0 && i + 1 < argc) {
g_min_idle = atol(argv[++i]);
} else if (strcmp(argv[i], "--max-id") == 0 && i + 1 < argc) {
g_max_id = atol(argv[++i]);
} else {
usage();
}
}
uid_t me = getuid();
time_t now = time(NULL);
long before = free_capacity();
long scanned_sets = 0, matched = 0, reaped = 0, freed_sems = 0, skipped_busy = 0;
for (long id = 0; id < g_max_id; id++) {
struct semid_ds ds;
if (semctl((int)id, 0, IPC_STAT, &ds) == -1)
continue;
scanned_sets++;
if ((int)ds.sem_nsems != g_nsems)
continue;
if (ds.sem_perm.cuid != me)
continue;
matched++;
if (now - ds.sem_otime < g_min_idle) {
skipped_busy++; /* 最近还被用过 ⇒ 大概率是活着的实例 */
continue;
}
if (!g_dry_run) {
if (semctl((int)id, 0, IPC_RMID) == -1) {
fprintf(stderr, " ⚠️ id=%ld IPC_RMID 失败:%s\n", id, strerror(errno));
continue;
}
}
reaped++;
freed_sems += ds.sem_nsems;
}
long after = g_dry_run ? before : free_capacity();
printf("回收前余量:最多可建 %ld 个信号量的 set\n", before);
printf("扫描 id 空间 [0, %ld):现存 set %ld 个\n", g_max_id, scanned_sets);
printf("命中口径(nsems=%d 且 uid=%d):%ld 个\n", g_nsems, (int)me, matched);
printf(" 其中最近被使用过(疑似活实例,保留):%ld 个\n", skipped_busy);
printf("%s:回收 %ld 个 set / %ld 个信号量\n",
g_dry_run ? "[dry-run] 本应回收" : "已回收", reaped, freed_sems);
printf("回收后余量:最多可建 %ld 个信号量的 set\n", after);
if (!g_dry_run && after < g_nsems) {
printf("⚠️ 余量仍不足 %d 个信号量 ⇒ 还有别处的占用(PGlite 需要 %d 个)\n",
g_nsems, g_nsems);
return 1;
}
return 0;
}