/* The processes pic starts, for `pic ps` (procs.c; host only: a build
* without processes would list its runner jobs instead). Every launch site
* records its process here at start and again when it ends, in picu_proc of
* ~/.pic/pic.sqlite. Recording is best effort and never fails the work it
* describes. Processes a hook script starts on its own are not tracked. */
#ifndef PIC_PROCS_H
#define PIC_PROCS_H
#include "pic.h"
#include "userdb.h"
/* The sink for the host executor (pic_proc_sink_set). */
extern const pic_proc_sink pic_procs_sink;
/* Once at startup, by the process that owns what it starts: prunes rows
* finished over 7 days ago and, when NOTICE, prints one line to stderr if
* processes of earlier runs are still running (at most once an hour). */
void pic_procs_init(int notice);
/* The workspace and conversation (either may be NULL) recorded with what is
* started from now on; copied. */
void pic_proc_scope(const char *workspace, const char *conversation);
/* Records a process started now (its own process group when PGID is its pid):
* its id for pic_proc_end, or -1 when it could not be recorded. */
long long pic_proc_start(const char *kind, const char *command, long pid, long pgid);
/* Marks it ended: EXIT_CODE (-1 when it was a signal or is unknown), SIG. */
void pic_proc_end(long long id, int exit_code, int sig);
/* The one OS-specific part: the process's start time as an opaque number
* that differs between two processes with the same pid; 0 when unknown
* (the process is gone, or this OS has no way to say). */
long long pic_proc_start_token(long pid);
/* Whether PID is alive and, when both are known, its start token is TOKEN
* (so a pid reused by another process does not count). */
int pic_proc_alive(long pid, long long token);
/* Whether the recorded process is running now. */
int pic_proc_running(const pic_proc *p);
/* SIGTERM to the recorded process group (the process itself when it leads
* none), SIGKILL after GRACE_MS if it is still there. 0, or -1 when it was
* not running (nothing is signaled). */
int pic_proc_kill(const pic_proc *p, int grace_ms);
/* `pic ps ARGS` (ARGS after "ps"): 0, 1, or 2 for a usage error. */
int pic_ps_command(int argc, char **argv);
#endif