summaryrefslogtreecommitdiffabout
path: root/src/genrc.h
blob: c6ee57b82813dfde34309e93a92a2997b88ff9ca (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
/* This file is part of genrc
Copyryght (C) 2018 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <regex.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/wait.h>
#include "grecs.h"
#include "wordsplit.h"

void setprogname(char const *s);
void genrc_error(char const *fmt, ...);
void usage_error(char const *fmt, ...);
void system_error(int ec, char const *fmt, ...);

#define xmalloc grecs_malloc
#define xzalloc grecs_zalloc
#define xcalloc grecs_calloc
#define xrealloc grecs_realloc
#define xstrdup grecs_strdup
void *x2nrealloc(void *p, size_t *pn, size_t s);

#define SHELL "/bin/sh"

pid_t file_read_pid(char const *filename);

typedef struct transform *TRANSFORM;
char *transform_string(TRANSFORM tf, const char *input);
char *transform_string_if_match(TRANSFORM tf, const char *input);
TRANSFORM compile_transform_expr(const char *expr, int cflags);

struct pidlist {
	pid_t *pidv;
	size_t pidc;
	size_t pidn;
};
typedef struct pidlist PIDLIST;

void pidlist_init(PIDLIST *);
void pidlist_free(PIDLIST *);
void pidlist_clear(PIDLIST *plist);
void pidlist_add(PIDLIST *, pid_t);
ssize_t pidlist_index(PIDLIST *plist, pid_t p);
int pidlist_member(PIDLIST *plist, pid_t p);
int pidlist_remove(PIDLIST *plist, size_t i);
void pidlist_kill(PIDLIST *plist, int sig);

pid_t strtopid(char const *str);

int pid_is_running(pid_t pid);

void runas(void);
int str_to_sig(char const *);
int str_to_int(char const *);


enum {
	MATCH_REGEX,  /* extended POSIX regexp match (default) */
	MATCH_PCRE,   /* PCRE match (not implemented) */
	MATCH_GLOB,   /* glob pattern match */
	MATCH_EXACT,  /* exact match */
};
#define MATCH_DEFAULT MATCH_REGEX

enum {
	PROCF_ICASE   = 0x01, /* ignore case */
	PROCF_ALL     = 0x02, /* use all pids */
	PROCF_CMDLINE = 0x04, /* match against entire command line */
	PROCF_EXE     = 0x08  /* match against executable name */	
};
#define PROCF_DEFAULT 0

struct procscanbuf {
	int match;   /* match type */
	int flags;   /* match flags */
	void *pattern;
};

typedef struct procscanbuf *PROCSCANBUF;

PROCSCANBUF procscan_init(char const *pattern, char const *flagstr);
void procscan_free(PROCSCANBUF buf);
int procscan_match(PROCSCANBUF buf, char const *arg);

void match_exact_init(PROCSCANBUF buf, char const *pattern);
void match_exact_free(PROCSCANBUF buf);
int match_exact(PROCSCANBUF buf, char const *arg);

void match_glob_init(PROCSCANBUF buf, char const *pattern);
void match_glob_free(PROCSCANBUF buf);
int match_glob(PROCSCANBUF buf, char const *arg);

void match_regex_init(PROCSCANBUF buf, char const *pattern);
void match_regex_free(PROCSCANBUF buf);
int match_regex(PROCSCANBUF buf, char const *arg);

void match_pcre_init(PROCSCANBUF buf, char const *pattern);
void match_pcre_free(PROCSCANBUF buf);
int match_pcre(PROCSCANBUF buf, char const *arg);


enum {
	RESTART_ON_EXIT,
	RESTART_ON_SIGNAL
};

void add_restart_condition(int type, char const *arg);


struct genrc_pid_closure {
	char const *name;
        int (*pid)(struct genrc_pid_closure *, PIDLIST *);
};

typedef struct genrc_pid_closure GENRC_PID_CLOSURE;
GENRC_PID_CLOSURE *get_pid_closure(char const *str);
int get_pid_list(GENRC_PID_CLOSURE *, PIDLIST *);

GENRC_PID_CLOSURE *genrc_pid_file_init(int argc, char **argv);
GENRC_PID_CLOSURE *genrc_pid_config_init(int argc, char **argv);
GENRC_PID_CLOSURE *genrc_pid_grep_init(int argc, char **argv);
GENRC_PID_CLOSURE *genrc_pid_proc_init(int argc, char **argv);
GENRC_PID_CLOSURE *genrc_pid_ps_init(int argc, char **argv);

extern char *genrc_command;
extern char *genrc_program;
extern char *genrc_pid_from;
extern unsigned genrc_timeout;
extern int genrc_no_reload;
extern int genrc_signal_reload;
extern int genrc_signal_stop;
extern GENRC_PID_CLOSURE *genrc_pid_closure;
extern char *genrc_create_pidfile;
extern int genrc_verbose;

int sentinel(void);

int com_start(void);
int com_status(void);
int com_stop(void);
int com_restart(void);
int com_reload(void);

Return to:

Send suggestions and report system problems to the System administrator.