byhandhook.c 6.19 KB
Newer Older
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
/*  This file is part of "reprepro"
 *  Copyright (C) 2010 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "error.h"
#include "filecntl.h"
#include "names.h"
#include "configparser.h"
#include "globmatch.h"
33
#include "hooks.h"
34 35 36 37 38 39 40 41 42 43 44
#include "byhandhook.h"

struct byhandhook {
	/*@null@*/struct byhandhook *next;
	char *sectionglob;
	char *priorityglob;
	char *filenameglob;
	char *script;
};

void byhandhooks_free(struct byhandhook *l) {
45
	while (l != NULL) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
		/*@null@*/struct byhandhook *n = l->next;

		free(l->sectionglob);
		free(l->priorityglob);
		free(l->filenameglob);
		free(l->script);
		free(l);
		l = n;
	}
}

retvalue byhandhooks_parse(struct configiterator *iter, struct byhandhook **hooks_p) {
	retvalue r;
	char *v;
	struct byhandhook *h, *hooks = NULL, **nexthook_p = &hooks;

	r = config_getwordinline(iter, &v);
63 64 65 66
	if (RET_IS_OK(r)) {
		fprintf(stderr,
"Error parsing %s, line %u, column %u: unexpected input '%s'"
" (each hook must be in its own line)!\n",
67 68 69 70 71 72 73
				config_filename(iter),
				config_markerline(iter),
				config_markercolumn(iter),
				v);
		free(v);
		r = RET_ERROR;
	}
74
	if (RET_WAS_ERROR(r))
75
		return r;
76
	while (config_nextline(iter)) {
77
		r = config_getwordinline(iter, &v);
78
		if (r == RET_NOTHING)
79
			continue;
80
		if (RET_WAS_ERROR(r))
81
			break;
82 83
		h = zNEW(struct byhandhook);
		if (FAILEDTOALLOC(h)) {
84 85
			r = RET_ERROR_OOM;
			break;
86
		}
87 88
		*nexthook_p = h;
		nexthook_p = &h->next;
89 90
		h->sectionglob = v;
		r = config_getwordinline(iter, &v);
91 92 93
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Error parsing %s, line %u, column %u: each byhand hooks needs 4 arguments, found only 1!\n",
94 95 96 97 98
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter));
			r = RET_ERROR;
		}
99
		if (RET_WAS_ERROR(r))
100
			break;
101 102
		h->priorityglob = v;
		r = config_getwordinline(iter, &v);
103 104 105
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Error parsing %s, line %u, column %u: each byhand hooks needs 4 arguments, found only 2!\n",
106 107 108 109 110
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter));
			r = RET_ERROR;
		}
111
		if (RET_WAS_ERROR(r))
112
			break;
113 114
		h->filenameglob = v;
		r = config_getwordinline(iter, &v);
115 116 117
		if (r == RET_NOTHING) {
			fprintf(stderr,
"Error parsing %s, line %u, column %u: each byhand hooks needs 4 arguments, found only 2!\n",
118 119 120 121 122
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter));
			r = RET_ERROR;
		}
123
		if (RET_WAS_ERROR(r))
124
			break;
125
		assert (v != NULL && v[0] != '\0'); \
126 127 128 129 130
		h->script = configfile_expandname(v, v);
		if (FAILEDTOALLOC(h->script)) {
			r = RET_ERROR_OOM;
			break;
		}
131
		r = config_getwordinline(iter, &v);
132 133 134
		if (RET_IS_OK(r)) {
			fprintf(stderr,
"Error parsing %s, line %u, column %u: each byhand hooks needs exactly 4 arguments, but there are more (first unexpected: '%s'!\n",
135 136 137 138 139 140
					config_filename(iter),
					config_markerline(iter),
					config_markercolumn(iter), v);
			free(v);
			r = RET_ERROR;
		}
141
		if (RET_WAS_ERROR(r))
142 143
			break;
	}
144
	if (RET_WAS_ERROR(r)) {
145 146
		byhandhooks_free(hooks);
		return r;
147 148 149 150 151 152 153 154 155 156
	}
	*hooks_p = hooks;
	return RET_OK;
}

bool byhandhooks_matched(const struct byhandhook *list, const struct byhandhook **touse, const char *section, const char *priority, const char *filename) {
	const struct byhandhook *h;

	/* for each file the first matching hook is called
	 * it might later be extended to allow multiple with some keywords */
157 158
	if (*touse != NULL)
		/* if ((*touse)->nonexclusive) list = (*touse)->next ; else */
159
		return false;
160 161
	for (h = list ; h != NULL ; h = h->next) {
		if (!globmatch(section, h->sectionglob))
162
			continue;
163
		if (!globmatch(priority, h->priorityglob))
164
			continue;
165
		if (!globmatch(filename, h->filenameglob))
166 167 168 169 170 171 172 173 174 175 176
			continue;
		*touse = h;
		return true;
	}
	return false;
}

retvalue byhandhook_call(const struct byhandhook *h, const char *codename, const char *section, const char *priority, const char *name, const char *fullfilename) {
	pid_t child;

	child = fork();
177
	if (child == 0) {
178 179
		/* Try to close all open fd but 0,1,2 */
		closefrom(3);
180
		sethookenvironment(causingfile, NULL, NULL, NULL);
181 182 183 184 185 186 187 188 189 190 191
		(void)execl(h->script, h->script, codename,
				section, priority, name,
				fullfilename, (char*)NULL);
		{
			int e = errno;
			fprintf(stderr, "Error %d executing '%s': %s\n",
					e, h->script,
				strerror(e));
		}
		_exit(255);
	}
192
	if (child < 0) {
193 194 195 196
		int e = errno;
		fprintf(stderr, "Error %d forking: %s!\n", e, strerror(e));
		return RET_ERRNO(e);
	}
197
	while (true) {
198 199 200 201
		int status;
		pid_t pid;

		pid = waitpid(child, &status, 0);
202 203 204
		if (pid == child) {
			if (WIFEXITED(status)) {
				if (WEXITSTATUS(status) == 0) {
205 206 207 208 209 210 211 212
					return RET_OK;
				}
				fprintf(stderr,
"Byhandhook '%s' '%s' '%s' '%s' '%s' '%s' failed with exit code %d!\n",
					h->script, codename,
					section, priority, name,
					fullfilename,
					(int)(WEXITSTATUS(status)));
213
			} else if (WIFSIGNALED(status)) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227
				fprintf(stderr,
"Byhandhook '%s' '%s' '%s' '%s' '%s' '%s' killed by signal %d!\n",
					h->script, codename,
					section, priority, name,
					fullfilename,
					(int)(WTERMSIG(status)));
			} else {
				fprintf(stderr,
"Byhandhook '%s' '%s' '%s' '%s' '%s' '%s' failed!\n",
					h->script, codename,
					section, priority, name,
					fullfilename);
			}
			return RET_ERROR;
228
		} else if (pid == (pid_t)-1) {
229 230
			int e = errno;

231
			if (e == EINTR)
232 233 234 235 236 237 238 239 240 241
				continue;
			fprintf(stderr,
"Error %d calling waitpid on byhandhook child: %s\n",
				e, strerror(e));
			return RET_ERRNO(e);
		}
	}
	/* NOT REACHED */
}