File: utils.c

package info (click to toggle)
resmgr 1.0-2sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 308 kB
  • ctags: 313
  • sloc: ansic: 3,165; sh: 556; makefile: 109
file content (245 lines) | stat: -rw-r--r-- 4,404 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Utility log functions.
 *
 * These are linked in if and only if the application doesn't
 * implement its own log functions
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include "utils.h"

static int	use_syslog = 0;

static void	def_fatal(const char *, ...);
static void	def_log(const char *, ...);
static void	def_debug(const char *, ...);
static void	def_clog(int, const char *, ...);
static void	vlog(int, const char *, va_list);

void		(*fatal)(const char *, ...) = def_fatal;
void		(*log)(const char *, ...) = def_log;
void		(*debug)(const char *, ...) = def_debug;
void		(*clog)(int, const char *, ...) = def_clog;

void
log_open_syslog(const char *name, int log_perror)
{
	if (use_syslog)
		closelog();
	openlog(name, LOG_PID | (log_perror? LOG_PERROR : 0), LOG_DAEMON);
	use_syslog = 1;

	/* Reinit logging */
	fatal = def_fatal;
	log   = def_log;
	clog  = def_clog;
}

static void
vlog(int level, const char *fmt, va_list ap)
{
	if (use_syslog) {
		vsyslog(level, fmt, ap);
	} else {
		vfprintf(stderr, fmt, ap);
		if (!strchr(fmt, '\n'))
			fputs("\n", stderr);
	}
}

static void
def_fatal(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	vlog(LOG_NOTICE, fmt, ap);
	exit(1);
}

static void
def_log(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	vlog(LOG_INFO, fmt, ap);
	va_end(ap);
}

static void
def_debug(const char *fmt, ...)
{
	va_list		ap;

	va_start(ap, fmt);
	vlog(LOG_DEBUG, fmt, ap);
	va_end(ap);
}

static void
def_clog(int code, const char *fmt, ...)
{
	char		buffer[1024];
	va_list		ap;
	int		n;

	va_start(ap, fmt);
	sprintf(buffer, "%03d ", code);
	n = strlen(buffer);
	vsnprintf(buffer+n, sizeof(buffer)-n, fmt, ap);
	vlog(LOG_INFO, "%s", buffer);
	va_end(ap);
}

int
make_pidfile(const char *pathname, int force)
{
	char	pids[32], tempname[1024], *sp;
	int	fd, okay = 0;
	pid_t	pid;

	if (!force
	 && (pid = read_pidfile(pathname)) > 0
	 && kill(pid, 0) < 0 && errno == ESRCH) {
		log("Removed stale lock %s", pathname);
		unlink(pathname);
	}

	assert(strlen(pathname) + sizeof("fenceXXXXXX") < sizeof(tempname));
	strcpy(tempname, pathname);
	if ((sp = strrchr(tempname, '/')) != 0) {
		sp += 1;
	} else {
		sp = tempname;
	}
	strcpy(sp, "fenceXXXXXX");

	if ((fd = mkstemp(tempname)) < 0)
		return 0;

	fchmod(fd, 0644);

	sprintf(pids, "%u\n", getpid());
	write(fd, pids, strlen(pids));
	close(fd);

	if (force)
		okay = rename(tempname, pathname) >= 0;
	else
		okay = link(tempname, pathname) >= 0;

	if (!okay)
		syslog(LOG_NOTICE, "Failed to lock %s: %m", pathname);
	unlink(tempname);
	return okay;
}

int
read_pidfile(const char *pathname)
{
	char	buffer[32];
	int	n, fd, pid;

	if ((fd = open(pathname, O_RDONLY)) < 0)
		return -1;

	n = read(fd, buffer, sizeof(buffer)-1);
	close(fd);

	if (n > 0) {
		buffer[n] = '\0';
		pid = atoi(buffer);
		if (pid > 0)
			return pid;
	}

	return -1;
}

/*
 * Split line into white space separated words
 */
int
line_split(char *inbuf, char **argv, unsigned int max)
{
	unsigned int	n = 0;
	char		*s;

	inbuf[strcspn(inbuf, "#\r\n")] = '\0';
	s = strtok(inbuf, " \t");
	while (s) {
		if (n < max - 1)
			argv[n++] = s;
		s = strtok(NULL, " \t");
	}
	argv[n] = NULL;
	return n;
}

/*
 * Merge a list
 */
char *
merge_list(int argc, char **argv, const char *sepa)
{
	char	*result = NULL, *s;
	int	total = 0, slen, len;

	slen = strlen(sepa);
	while (argc--) {
		s = *argv++;
		len = strlen(s);
		result = (char *) realloc(result, total + slen + len + 1);
		if (*result)
			strcat(result, sepa);
		strcat(result, s);
		total = strlen(result);
	}

	if (!result)
		result = strdup("");

	return result;
}

/*
 * Get the list of groups for a given user
 */
void
get_groups(const char *username, char **groups, unsigned int max)
{
	struct passwd	*pwd;
	struct group	*grp;
	unsigned int	n = 0, m;

	if ((pwd = getpwnam(username)) != NULL
	 && (grp = getgrgid(pwd->pw_gid)) != NULL) {
	 	groups[n++] = strdup(grp->gr_name);
	}

	setgrent();
	while (n < max && (grp = getgrent()) != NULL) {
		for (m = 0; grp->gr_mem[m]; m++) {
			if (!strcasecmp(grp->gr_mem[m], username)) {
				groups[n++] = strdup(grp->gr_name);
				break;
			}
		}
	}
	groups[n] = NULL;
	endgrent();
}