// Relais im Spielprozess: nimmt Rahmen an /tmp/-mitte an und schreibt sie auf die // Tischleitung, die das Spiel SELBST haelt. Kein zweiter Verbinder, kein Wettlauf. #define _DARWIN_C_SOURCE #include #include #include #include #include #include #include #include #include #include #include static volatile int tabelle = -1; static char pfad[108]; static char wache[120]; static char ziel[108]; static FILE *protokoll; static void merke(const char *was, int fd) { // Debug-Log nur, wenn RELAIS_LOG gesetzt ist - KEIN hart kodierter /tmp/cp_analyse-Pfad mehr. // Ohne die Variable laeuft das Relais still weiter; der Betrieb haengt NICHT am Log. static int aus = 0; if (aus) return; if (!protokoll) { const char *p = getenv("RELAIS_LOG"); if (!p) { aus = 1; return; } protokoll = fopen(p, "a"); if (!protokoll) { aus = 1; return; } } fprintf(protokoll, "%d %s fd=%d ziel=%s\n", (int)time(NULL), was, fd, ziel); fflush(protokoll); } #include static void ziel_aus_argv(void) { int argc = *_NSGetArgc(); char **argv = *_NSGetArgv(); for (int i = 0; i < argc; i++) { const char *a = argv[i]; if (a && strncmp(a, "roomName=", 9) == 0 && strlen(a) < 100) { snprintf(ziel, sizeof ziel, "/tmp/%s", a + 9); return; } } } static int tabelle_finden(void) { for (int fd = 3; fd < 1024; fd++) { struct sockaddr_un a; socklen_t l = sizeof a; if (getpeername(fd, (struct sockaddr *)&a, &l) != 0) continue; if (a.sun_family != AF_UNIX) continue; if (strncmp(a.sun_path, "/tmp/", 5) != 0) continue; if (strstr(a.sun_path, "lobby-pipe")) continue; if (strstr(a.sun_path, "-mitte")) continue; if (ziel[0] && strcmp(a.sun_path, ziel) != 0) continue; if (strlen(a.sun_path) + 7 >= sizeof pfad) continue; return fd; } return -1; } static int wache_auf(void) { int s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) return -1; struct sockaddr_un a; memset(&a, 0, sizeof a); a.sun_family = AF_UNIX; snprintf(a.sun_path, sizeof a.sun_path, "%s-mitte", pfad); unlink(a.sun_path); if (bind(s, (struct sockaddr *)&a, sizeof a) != 0 || listen(s, 8) != 0) { close(s); return -1; } snprintf(wache, sizeof wache, "%s-mitte", pfad); return s; } static void *einwerfer(void *arg) { int c = (int)(long)arg; merke("annahme", c); unsigned char puffer[65536]; for (;;) { ssize_t n = read(c, puffer, sizeof puffer); merke("gelesen", (int)n); if (n <= 0) break; int fd = tabelle; if (fd > 0) { ssize_t off = 0; while (off < n) { ssize_t w = write(fd, puffer + off, (size_t)(n - off)); merke("geschrieben", (int)w); if (w <= 0) { merke("fehler", errno); break; } off += w; } } } close(c); return NULL; } static void *lauf(void *arg) { (void)arg; int s = -1; for (;;) { int fd = tabelle_finden(); if (fd != tabelle) { tabelle = fd; merke("wechsel", fd); snprintf(pfad, sizeof pfad, "%s", ""); if (fd > 0) { struct sockaddr_un a; socklen_t l = sizeof a; if (getpeername(fd, (struct sockaddr *)&a, &l) == 0) snprintf(pfad, sizeof pfad, "%s", a.sun_path); } if (s >= 0) { close(s); s = -1; unlink(wache); } if (pfad[0]) s = wache_auf(); } if (s >= 0) { struct timeval tv = {0, 200000}; fd_set r; FD_ZERO(&r); FD_SET(s, &r); if (select(s + 1, &r, NULL, NULL, &tv) > 0) { int c = accept(s, NULL, NULL); if (c >= 0) { pthread_t t; pthread_create(&t, NULL, einwerfer, (void *)(long)c); pthread_detach(t); } } } else { usleep(200000); } } return NULL; } __attribute__((constructor)) static void start(void) { ziel_aus_argv(); pthread_t t; if (pthread_create(&t, NULL, lauf, NULL) == 0) pthread_detach(t); }