aboutsummaryrefslogtreecommitdiff
path: root/pies/pies.h
blob: bc8105e9df932c94af1532e52b6977cc0aed3c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* This file is part of Mailfromd.
   Copyright (C) 2008 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <sysexits.h>

#include <mailutils/mailutils.h>
#include <mailutils/daemon.h>
#include <mailutils/libargp.h>
#include <mailutils/syslog.h>

#include "inttostr.h"
#include "xalloc.h"
#include "c-ctype.h"
#include "libmf.h"

#define RETR_OUT 0
#define RETR_ERR 1

#define TESTTIME  2*60
#define SLEEPTIME 5*60
#define MAXSPAWN 10

typedef struct limits_rec *limits_record_t;

struct pies_privs_data
{
  char *user;
  mu_list_t groups;
};

#define MAX_RETURN_CODE 127

enum return_action
{
  action_restart,
  action_disable,
};

struct action
{
  enum return_action act;  /* Action to take when the component terminates */
  mu_address_t addr;       /* Addresses to notify about it. */
  char *message;           /* Notification mail. */
};

enum pies_comp_mode
  {
    /* Execute the component, no sockets are opened. This is the default
       Pies mode. */
    pies_comp_exec,
    /* Open a socket and start a component with stdin/stdout bound to that
       socket. Corresponds to MeTA1 notion of `start_action = accept'.
    */
    pies_comp_accept,
    /* Inetd mode: like above, but start the component only when an
       incoming connection is requested. Corresponds to
       `start_action = nostartaccept' in MeTA1.
    */
    pies_comp_inetd,
    /* Open a socket, start a component, and pass the socket fd to the
       component via the UNIX domain socket. Corresponds to
       `start_action = pass' in MeTA1. */
    pies_comp_pass_fd
  };

struct component
{
  enum pies_comp_mode mode;  
  char *tag;             /* Entry tag (for diagnostics purposes) */
  char **argv;           /* Program command line */
  char **env;            /* Program environment */
  char *dir;             /* Working directory */
  char **depend;         /* Dependencies */
  /* FIXME: disabled and precios can be encoded as bits in mode */
  int disabled;          /* The componenet is disabled */
  int precious;          /* The component is precious (cannot be disabled) */
  char *rmfile;          /* Try to remove this file before starting */
  struct pies_privs_data privs; /* UID/GIDS+groups to run under */
  mode_t umask;          /* Umask to install before starting */
  limits_record_t limits;/* System limits */
  mu_url_t socket_url;   /* Socket to listen on (if mode != pies_comp_exec) */
  char *pass_fd_socket;  /* Socket to pass fd on
			    (if mode == pies_comp_pass_fd) */
  mu_acl_t acl;
  /* Retranslators: */
  int facility;	         /* Syslog facility. */
  int retr[2];           /* Priorities for stdout and stderr */
  /* Actions to execute on various exit codes: */
  struct action *act[MAX_RETURN_CODE+1];
};

extern char *syslog_tag;
extern unsigned long shutdown_timeout;
extern struct component default_component;
extern mu_acl_t pies_acl;
extern mu_debug_t pies_debug;

void register_prog (struct component *comp);
size_t progman_running_count (void);
void progman_start (void);
void progman_wake_sleeping (void);
void progman_stop (void);
void progman_cleanup (int expect_term);
void progman_stop_component (const char *name);
void progman_dump_stats (const char *filename);
int progman_accept (int socket);

void log_setup (int want_stderr);
void signal_setup (RETSIGTYPE (*sf)(int));
void priv_setup (struct pies_privs_data *pr);

typedef struct pies_depmap *pies_depmap_t;
typedef struct pies_depmap_pos *pies_depmap_pos_t;
enum pies_depmap_direction
  {
    depmap_row = 0,
    depmap_col = !depmap_row
  };

pies_depmap_t depmap_alloc (unsigned count);
pies_depmap_t depmap_copy (pies_depmap_t dpm);
void depmap_set (pies_depmap_t dmap, unsigned row, unsigned col);
int depmap_isset (pies_depmap_t dmap, unsigned row, unsigned col);
void depmap_tc (pies_depmap_t dmap);
unsigned depmap_first (pies_depmap_t dmap, enum pies_depmap_direction dir,
		       unsigned coord, pies_depmap_pos_t *ppos);
unsigned depmap_next (pies_depmap_t dmap, pies_depmap_pos_t pos);


void pies_pause (void);
int register_listener (int fd);
int pass_fd (const char *socket, int fd);
int open_socket (mu_url_t url);


int parse_limits (limits_record_t *plrec, char *str, char **endp);
int set_limits (const char *name, limits_record_t lrec);

Return to:

Send suggestions and report system problems to the System administrator.