← Programming examples · libnex
03_run_uscita.c
The file alone · in the libnex-dev package: /usr/share/doc/libnex-dev/examples/
/*
-------------------------------------------------------------------------------------------------
_ ____ _ _____ ___ ___ _ _ ____ _____ ____ _ _ _____ ____ ___ _
/ \ | _ \ / \|_ _/ _ \ |_ _| \ | |/ ___| ____/ ___| \ | | ____| _ \|_ _| / \
/ _ \ | |_) | / _ \ | || | | | | || \| | | _| _|| | _| \| | _| | |_) || | / _ \
/ ___ \| _ < / ___ \| || |_| | | || |\ | |_| | |__| |_| | |\ | |___| _ < | | / ___ \
/_/ \_\_| \_\/_/ \_\_| \___/ |___|_| \_|\____|_____\____|_| \_|_____|_| \_\___/_/ \_\
-------------------------------------------------------------------------------------------------
Copyright (c) 2026 Arato Ingegneria. Tutti i diritti riservati.
Name : 03_run_uscita.c
Author : Ing. Roberto arato
Version : 1.0.0
Copyright : Ing. Roberto arato (c) 2026
Description : Esempio 3 - nex_run: comando su un altro nodo con l'uscita in diretta
-------------------------------------------------------------------------------------------------
*/
/*
Come system(), ma sul cluster: il comando gira sul nodo indicato e le sue uscite
arrivano qui mentre vengono scritte (eventi STDOUT e STDERR). Alla fine l'evento EXIT
porta il codice d'uscita; LOST dice che il nodo si e' perso (esito sconosciuto),
ERROR che la richiesta e' stata rifiutata (il comando non e' partito).
Uso: ./03_run_uscita NODO COMANDO [ARG...] es. ./03_run_uscita any uname -a
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nex.h>
// Valore della chiave key nel JSON (la prima che compare), testo o numero, senza
// virgolette: basta per l'esempio, in un programma vero si usa una libreria JSON.
// 0 se la chiave non c'e'.
static int valore(const char *json, const char *key, char *out, size_t len) {
char pat[64];
snprintf(pat, sizeof(pat), "\"%s\"", key);
const char *p = json ? strstr(json, pat) : NULL;
out[0] = 0;
if (p == NULL) return 0;
p += strlen(pat);
while (*p == ' ' || *p == ':') p++; // "key":"v" e "key": "v"
if (*p == '"') p++;
snprintf(out, len, "%.*s", (int)strcspn(p, "\",}] "), p);
return 1;
}
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "uso: %s NODO|any COMANDO [ARG...]\n", argv[0]);
return 2;
}
char cwd[4096];
if (getcwd(cwd, sizeof(cwd)) == NULL) return 1;
nex_conn *nd = nex_open(NULL);
if (nd == NULL) { fprintf(stderr, "agent non raggiungibile\n"); return 1; }
// La richiesta: nodo, comando, cartella, ambiente (NULL = nessuno), CPU e memoria
// massime (NULL = senza limiti). Dopo nex_run l'handle serve solo a questo job.
char *req = nex_run_json(argv[1], (const char *const *)argv + 2, cwd, NULL, NULL, "1G");
int err = 0;
nex_job *job = nex_run(nd, req, &err);
nex_free(req);
if (job == NULL) { fprintf(stderr, "run: %s\n", nex_strerror(err)); return 1; }
int code = 255, ev;
char *data;
size_t len;
while (nex_job_next(job, &ev, &data, &len) == NEX_OK) {
switch (ev) {
case NEX_EV_STARTED: fprintf(stderr, "[partito: %s]\n", data); break;
case NEX_EV_STDOUT: fwrite(data, 1, len, stdout); fflush(stdout); break;
case NEX_EV_STDERR: fwrite(data, 1, len, stderr); break;
case NEX_EV_EXIT: { // {"code":N,"unit":"..."}
char c[16];
if (valore(data, "code", c, sizeof(c))) code = atoi(c);
break;
}
case NEX_EV_LOST: fprintf(stderr, "[nodo perso: %s]\n", data); break;
case NEX_EV_ERROR: fprintf(stderr, "[rifiutato: %s]\n", data); break;
}
nex_free(data);
}
nex_job_free(job);
nex_close(nd);
return code;
}