← Esempi di programmazione · libnex
08_processi_log.c
Il file da solo · nel pacchetto libnex-dev: /usr/share/doc/libnex-dev/examples/
/*
-------------------------------------------------------------------------------------------------
_ ____ _ _____ ___ ___ _ _ ____ _____ ____ _ _ _____ ____ ___ _
/ \ | _ \ / \|_ _/ _ \ |_ _| \ | |/ ___| ____/ ___| \ | | ____| _ \|_ _| / \
/ _ \ | |_) | / _ \ | || | | | | || \| | | _| _|| | _| \| | _| | |_) || | / _ \
/ ___ \| _ < / ___ \| || |_| | | || |\ | |_| | |__| |_| | |\ | |___| _ < | | / ___ \
/_/ \_\_| \_\/_/ \_\_| \___/ |___|_| \_|\____|_____\____|_| \_|_____|_| \_\___/_/ \_\
-------------------------------------------------------------------------------------------------
Copyright (c) 2026 Arato Ingegneria. Tutti i diritti riservati.
Name : 08_processi_log.c
Author : Ing. Roberto arato
Version : 1.0.0
Copyright : Ing. Roberto arato (c) 2026
Description : Esempio 8 - processi, segnali per PID, messaggi ai terminali e log del cluster
-------------------------------------------------------------------------------------------------
*/
/*
- nex_spawn di un "sleep" e ricerca del suo PID in nex_ps (processi di tutto
il cluster: ognuno vede i propri, root tutti);
- nex_kill: segnale a un PID di qualunque nodo (come "nex kill node-b:PID");
- nex_msg: messaggio ai terminali di un utente su tutti i nodi (come "nex msg");
- nex_logs: ultime righe del journal dei propri programmi, da tutti i nodi.
Uso: cd /nex/...; ./08_processi_log
*/
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nex.h>
int main(void) {
nex_pid p;
const char *dorme[] = { "sleep", "300", NULL };
if (nex_spawn("any", dorme, &p) != NEX_OK) { fprintf(stderr, "%s\n", nex_errmsg()); return 1; }
nex_conn *nd = nex_open(NULL);
if (nd == NULL) return 1;
// Nel JSON ogni processo e' {"pid":N,...,"cmd":"sleep 300","cgroup":".../nex-job-JOB.service"}:
// si cerca il cgroup del proprio job (cosi' un altro "sleep 300" non si confonde con il nostro),
// si torna indietro al "pid" dello stesso oggetto e si controlla il comando (in un programma
// vero: una libreria JSON). Il processo compare dopo un attimo: si riprova per 10 s.
char job[48];
snprintf(job, sizeof(job), "nex-job-%s.service\"", p.job);
int pid = 0;
for (int prova = 0; prova < 20 && pid == 0; prova++) {
char *ps = NULL;
usleep(500000);
if (nex_ps(nd, 1, &ps) != NEX_OK) continue;
for (const char *c = strstr(ps, job); c && pid == 0; c = strstr(c + 1, job)) {
const char *q = c;
while (q > ps && strncmp(q, "{\"pid\":", 7) != 0) q--;
const char *cmd = strstr(q, "\"cmd\":\"sleep 300\"");
if (q > ps && cmd && cmd < c) pid = atoi(q + 7);
}
nex_free(ps);
}
printf("sleep 300 su %s, PID %d\n", p.node, pid);
char *resp = NULL;
if (pid > 0 && nex_kill(nd, p.node, pid, SIGTERM, &resp) == NEX_OK) printf("kill: %s\n", resp);
nex_free(resp);
int st = -1;
nex_wait(&p, &st, 10000);
printf("codice dello sleep dopo SIGTERM: %d\n", st);
struct passwd *pw = getpwuid(getuid());
if (pw && nex_msg(nd, pw->pw_name, "prova da 08_processi_log", &resp) == NEX_OK) printf("msg: %s\n", resp);
nex_free(resp);
// Tutti i nodi (NULL), nessuna unita' ne' testo da cercare, ultime 5 righe, priorita' fino a 6.
if (nex_logs(nd, NULL, NULL, NULL, 5, 0, 6, &resp) == NEX_OK) printf("log: %.300s...\n", resp);
nex_free(resp);
nex_close(nd);
return 0;
}