]>
Commit | Line | Data |
---|---|---|
328c390e DM |
1 | From c713acbd14c32071057a65d4a02a18500fed43f3 Mon Sep 17 00:00:00 2001 |
2 | From: Dietmar Maurer <dietmar@proxmox.com> | |
3 | Date: Tue, 20 Sep 2011 06:26:18 +0200 | |
4 | Subject: [PATCH 2/4] add the init-logger binary | |
5 | ||
6 | We use diet to create small binaries. | |
7 | ||
8 | Signed-off-by: Dietmar Maurer <dietmar@proxmox.com> | |
9 | --- | |
10 | scripts/Makefile.am | 4 + | |
11 | scripts/init-logger.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++ | |
12 | 2 files changed, 167 insertions(+), 0 deletions(-) | |
13 | create mode 100644 scripts/init-logger.c | |
14 | ||
211c83e2 DM |
15 | Index: new/scripts/Makefile.am |
16 | =================================================================== | |
17 | --- new.orig/scripts/Makefile.am 2012-02-10 06:33:24.000000000 +0100 | |
18 | +++ new/scripts/Makefile.am 2012-02-10 06:39:40.000000000 +0100 | |
328c390e DM |
19 | @@ -17,7 +17,11 @@ |
20 | ||
21 | include $(top_srcdir)/pathsubst.am | |
22 | ||
23 | +init-logger: init-logger.c | |
24 | + diet -Os gcc -static -s -o init-logger init-logger.c | |
25 | + | |
26 | vzlib_SCRIPTS = \ | |
27 | + init-logger \ | |
28 | vps-create \ | |
211c83e2 | 29 | vps-download \ |
328c390e | 30 | vps-functions \ |
211c83e2 DM |
31 | Index: new/scripts/init-logger.c |
32 | =================================================================== | |
33 | --- /dev/null 1970-01-01 00:00:00.000000000 +0000 | |
34 | +++ new/scripts/init-logger.c 2012-02-10 06:39:40.000000000 +0100 | |
328c390e DM |
35 | @@ -0,0 +1,163 @@ |
36 | +/* | |
37 | + | |
38 | + Copyright (C) 2008 Proxmox Server Solutions GmbH | |
39 | + | |
40 | + This program is free software; you can redistribute it and/or modify | |
41 | + it under the terms of the GNU General Public License as published by | |
42 | + the Free Software Foundation; version 2 dated June, 1991. | |
43 | + | |
44 | + This program is distributed in the hope that it will be useful, | |
45 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
46 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
47 | + GNU General Public License for more details. | |
48 | + | |
49 | + You should have received a copy of the GNU General Public License | |
50 | + along with this program; if not, write to the Free Software Foundation, | |
51 | + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
52 | + You should have received a copy of the GNU General Public License | |
53 | + | |
54 | + Author: Dietmar Maurer <dietmar@proxmox.com> | |
55 | + | |
56 | + Compile statically using dietlibc: | |
57 | + diet -Os gcc -static -s -o init-logger.ARCH init-logger.c | |
58 | +*/ | |
59 | + | |
60 | +#include <sys/types.h> | |
61 | +#include <sys/wait.h> | |
62 | +#include <stdlib.h> | |
63 | +#include <unistd.h> | |
64 | +#include <errno.h> | |
65 | +#include <stdio.h> | |
66 | +#include <signal.h> | |
67 | +#include <sys/select.h> | |
68 | + | |
69 | +/* Set a signal handler */ | |
70 | +static void | |
71 | +setsig(struct sigaction *sa, int sig, | |
72 | + void (*fun)(int), int flags) | |
73 | +{ | |
74 | + sa->sa_handler = fun; | |
75 | + sa->sa_flags = flags; | |
76 | + sigemptyset(&sa->sa_mask); | |
77 | + sigaction(sig, sa, NULL); | |
78 | +} | |
79 | + | |
80 | +static int terminate = 0; | |
81 | + | |
82 | +void | |
83 | +term_handler() | |
84 | +{ | |
85 | + terminate = 1; | |
86 | +} | |
87 | + | |
88 | +ssize_t | |
89 | +safe_read (int fd, char *buf, size_t count) | |
90 | +{ | |
91 | + ssize_t n; | |
92 | + | |
93 | + do { | |
94 | + n = read (fd, buf, count); | |
95 | + } while (n < 0 && errno == EINTR); | |
96 | + | |
97 | + return n; | |
98 | +} | |
99 | + | |
100 | +ssize_t | |
101 | +safe_write (int fd, char *buf, size_t count) | |
102 | +{ | |
103 | + ssize_t n; | |
104 | + | |
105 | + do { | |
106 | + n = write (fd, buf, count); | |
107 | + } while (n < 0 && errno == EINTR); | |
108 | + | |
109 | + return n; | |
110 | +} | |
111 | + | |
112 | +int | |
113 | +full_write(int fd, char *buf, size_t len) | |
114 | +{ | |
115 | + size_t n; | |
116 | + size_t total; | |
117 | + | |
118 | + total = 0; | |
119 | + | |
120 | + while (len > 0) { | |
121 | + n = safe_write(fd, buf, len); | |
122 | + | |
123 | + if (n < 0) | |
124 | + break; | |
125 | + | |
126 | + buf += n; | |
127 | + total += n; | |
128 | + len -= n; | |
129 | + } | |
130 | + | |
131 | + return total; | |
132 | +} | |
133 | + | |
134 | +static void | |
135 | +simple_cat (void) | |
136 | +{ | |
137 | + int bufsize = 256; | |
138 | + char buf[bufsize]; | |
139 | + size_t n_read; | |
140 | + int noop_count = 0; | |
141 | + | |
142 | + fd_set rfds; | |
143 | + struct timeval tv; | |
144 | + int retval; | |
145 | + | |
146 | + FD_ZERO(&rfds); | |
147 | + FD_SET(STDIN_FILENO, &rfds); | |
148 | + | |
149 | + tv.tv_sec = 1; | |
150 | + tv.tv_usec = 0; | |
151 | + | |
152 | + while ((retval = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv)) >= 0 || | |
153 | + (errno == EINTR)) { | |
154 | + | |
155 | + tv.tv_sec = 1; | |
156 | + tv.tv_usec = 0; | |
157 | + | |
158 | + FD_ZERO(&rfds); | |
159 | + FD_SET(STDIN_FILENO, &rfds); | |
160 | + | |
161 | + if (retval == -1 && errno == EINTR) | |
162 | + continue; | |
163 | + | |
164 | + if (retval) { | |
165 | + n_read = safe_read (STDIN_FILENO, buf, bufsize); | |
166 | + if (n_read == ((size_t) -1)) | |
167 | + return; | |
168 | + | |
169 | + noop_count = 0; | |
170 | + | |
171 | + if (full_write (STDOUT_FILENO, buf, n_read) != n_read) | |
172 | + return; | |
173 | + } else { | |
174 | + if (terminate) | |
175 | + noop_count++; | |
176 | + } | |
177 | + | |
178 | + if (noop_count >= 2) | |
179 | + return; | |
180 | + } | |
181 | +} | |
182 | + | |
183 | +int | |
184 | +main(int argc, char * argv[]) | |
185 | +{ | |
186 | + struct sigaction sa; | |
187 | + | |
188 | + setsig(&sa, SIGTERM, term_handler, SA_RESTART); | |
189 | + setsig(&sa, SIGINT, term_handler, SA_RESTART); | |
190 | + | |
191 | + printf ("starting init logger\n"); | |
192 | + | |
193 | + simple_cat(); | |
194 | + | |
195 | + printf ("\ninit logger finished\n"); | |
196 | + | |
197 | + exit (0); | |
198 | +} |