chiark / gitweb /
prefork-interp: add copyright licences
[chiark-utils.git] / cprogs / prefork.h
1 /* common stuff for cgi-fcgi-interp and prefork-interp */
2 /*
3  * Copyright 2016-2022 Ian Jackson and contributors to chiark-utils
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  * There is NO WARRANTY.
6  */
7
8 #ifndef PREFORK_H
9 #define PREFORK_H
10
11 #include "common.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <stdbool.h>
18 #include <assert.h>
19 #include <limits.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/utsname.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <sys/file.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <pwd.h>
30 #include <time.h>
31 #include <signal.h>
32 #include <sys/wait.h>
33 #include <syslog.h>
34         
35 #include <nettle/sha.h>
36
37 #include "myopt.h"
38 #include "timespeccmp.h"
39
40 #define MINHEXHASH 33
41
42 extern const char *interp, *ident, *script, *socket_path, *lock_path;
43 extern bool logging;
44 extern struct sha256_ctx identsc;
45 extern const char *run_base;
46
47 extern const char our_name[];
48
49 bool find_run_base_var_run(void);
50 void find_socket_path(void);
51
52 int acquire_lock(void);
53 int flock_file(const char *lock_path);
54
55 extern const struct cmdinfo cmdinfos[];
56 #define PREFORK_CMDINFOS \
57   { "help",   0, .call=of_help                                         }, \
58   { 0, 'g',   1,                    .sassignto= &ident                 }, \
59   { 0, 'G',   1, .call= off_ident_addstring                            }, \
60   { 0, 'E',   1, .call= off_ident_addenv                               },
61
62 void process_opts(const char *const **argv_io);
63
64 void vmsgcore(int estatus, int errnoval, const char *fmt, va_list al);
65
66 #define DEF_MSG(func, attrs, estatus, errnoval, after)  \
67   static void func(const char *fmt, ...)                \
68     __attribute__((unused, format(printf,1,2))) attrs;  \
69   static void func(const char *fmt, ...) {              \
70     va_list al;                                         \
71     va_start(al,fmt);                                   \
72     vmsgcore(estatus,errnoval,fmt,al);                  \
73     after                                               \
74   }
75
76 DEF_MSG(warninge, /*empty*/, 0, errno, { });
77 DEF_MSG(warning , /*empty*/, 0, -1,    { });
78
79 #define DEF_DIE(func, errnoval) \
80   DEF_MSG(func, __attribute__((noreturn)), 127, errnoval, { abort(); })
81
82 DEF_DIE(diee, errno)
83 DEF_DIE(die,  -1)
84
85 #define MAX_OPTS 5
86
87 void fusagemessage(FILE *f);
88 void usagemessage(void);
89 void of_help(const struct cmdinfo *ci, const char *val);
90 void of_iassign(const struct cmdinfo *ci, const char *val);
91 void ident_addinit(void);
92 bool stabs_same_inode(struct stat *a, struct stat *b);
93 void ident_addstring(char key, const char *string);
94
95 void off_ident_addstring(const struct cmdinfo *ci, const char *name);
96 void off_ident_addenv(const struct cmdinfo *ci, const char *name);
97
98 void ident_add_key_byte(char key);
99
100 #define IDENT_ADD_OBJ(key, obj) do{                             \
101     ident_add_key_byte(key);                                    \
102     sha256_update(&identsc, sizeof((obj)), (void*)&obj);        \
103   }while(0)
104
105 #endif /*PREFORK_H*/