1 /* This file is part of GNU Pies.
2    Copyright (C) 2008-2020 Sergey Poznyakoff
3 
4    GNU Pies is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Pies is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 enum prog_type
18   {
19     TYPE_COMPONENT,
20     TYPE_COMMAND
21   };
22 
23 enum prog_status
24   {
25     status_stopped,    /* Component is stopped. */
26     status_running,    /* Component is running */
27     status_listener,   /* Component is an inetd listener */
28     status_sleeping,   /* Component is sleeping. An attempt to start it will
29 			  be made at prog->v.p.timestamp + SLEEPTIME */
30     status_stopping,   /* Component is being stopped */
31     status_finished,   /* A "once" or "startup" component has finished */
32   };
33 
34 struct conn_class
35 {
36   const char *tag;
37   union pies_sockaddr_storage sa_storage;
38   size_t sa_len;
39   size_t count;
40 };
41 
42 struct prog
43 {
44   struct prog *next, *prev;
45   enum prog_type type;
46   pid_t pid;             /* PID */
47   int active :1;         /* The prog is active */
48   int wait   :1;         /* Wait for this prog to terminate */
49   int stop   :1;         /* Stop this prog */
50   union
51   {
52     struct
53     {
54       struct component *comp;
55       size_t argc;
56       char **argv;             /* Actual command line (NULL-terminated) */
57       int socket;
58       int redir[2];            /* FDs of redirectors */
59       time_t timestamp;        /* Time of last startup */
60       size_t failcount;        /* Number of failed starts since timestamp */
61       enum prog_status status; /* Current component status */
62       environ_t *env;          /* Execution environment (only in child) */
63       /* If status == status_listener: */
64       size_t num_instances;    /* Number of running instances */
65       /* If comp->type == pies_comp_inetd && status == status_running */
66       struct prog *listener;
67       union pies_sockaddr_storage sa_storage;
68       size_t sa_len;
69       struct conn_class *cclass;
70     } p;
71 
72     struct
73     {
74       char *tag;
75       char *command;
76     } c;
77   } v;
78 };
79 
80 #define IS_COMPONENT(p) ((p)->type == TYPE_COMPONENT)
81 #define IS_ACTIVE_COMPONENT(prog) (IS_COMPONENT(prog) && (prog)->active)
82 
83 struct prog *progman_locate (const char *name);
84 int progman_foreach (int (*filter) (struct prog *, void *data), void *data);
85 void prog_stop (struct prog *prog, int sig);
86 void progman_stop_component (struct prog **prog);
87 
88 char const *prog_tag (struct prog const *prog);
89 
90 int prog_activate_listener (struct prog *prog);
91 void prog_deactivate_listener (struct prog *prog);
92