aboutsummaryrefslogtreecommitdiffstats
path: root/glougloud/util.c
blob: 83d55a0b7695b2febe9a1039e55414f4dd682009 (plain) (blame)
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
#include <sys/types.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>
#include <string.h>

#include "glougloud.h"

FILE *logfile;
int loglevel;
int logpinvalid;

#define LOGFILE "/var/log/glougloud"
#define LOG_FORCED -2
#define LOG_FATAL -1
#define LOG_WARN 0
#define LOG_INFO 1
#define LOG_DEBUG 2

static void logit(int, const char *, const char *, va_list);

void
log_init(int level, int pinvalid)
{
	logfile = fopen(LOGFILE, "a+");
	if (!logfile) {
		printf("cannot open log file %s!\n", LOGFILE);
		exit(1);
	}
	loglevel = level;
	logpinvalid = pinvalid;
}

void
log_tmp(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    logit(LOG_FORCED, "XXX ", msg, ap);
    va_end(ap);
}
void
log_pinvalid(const char *msg, ...)
{
    va_list ap;

    if (!logpinvalid)
	return;

    va_start(ap, msg);
    logit(LOG_FORCED, "pinvalid: ", msg, ap);
    va_end(ap);
}
void
log_debug(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    logit(LOG_DEBUG, "", msg, ap);
    va_end(ap);
}
void
log_info(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    logit(LOG_INFO, "", msg, ap);
    va_end(ap);
}
void
log_warn(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    logit(LOG_WARN, "", msg, ap);
    va_end(ap);
}
void __dead
fatal(const char *msg, ...)
{
    va_list ap;

    va_start(ap, msg);
    logit(LOG_FATAL, "", msg, ap);
    va_end(ap);
    
    exit(1);
}

/* XXX mpsafe */
static void
logit(int level, const char *prefix, const char *msg, va_list ap)
{
    time_t clock;

    if (level <= loglevel) {
	time(&clock);
	fprintf(logfile, "%d ", (int)clock);
	vfprintf(logfile, prefix, ap);
	vfprintf(logfile, msg, ap);
	fprintf(logfile, "\n");
	fflush(logfile);
    }
}

void *
xmalloc(size_t size)
{
	void *ptr;

	if (size == 0)
		fatal("xmalloc: zero size");
	ptr = malloc(size);
	if (ptr == NULL)
		fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
	return ptr;
}

void *
xcalloc(size_t nmemb, size_t size)
{
	void *ptr;

	if (size == 0)
		fatal("xcalloc: zero size");
	ptr = calloc(nmemb, size);
	if (ptr == NULL)
		fatal("xcalloc: out of memory (allocating %lu bytes)", (u_long) size);
	return ptr;
}

void
fd_nonblock(int fd)
{
	int flags = fcntl(fd, F_GETFL, 0);
	int rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
	if (rc == -1)
		log_warn("failed to set fd %i non-blocking", fd);
}

void
droppriv()
{
	struct passwd	*pw;

	pw = getpwnam(GLOUGLOUD_USER);
	if (!pw)
		fatal("unknown user %s", GLOUGLOUD_USER);
	if (chroot(pw->pw_dir) != 0)
		fatal("unable to chroot");
	if (chdir("/") != 0)
		fatal("unable to chdir");
	if (setgroups(1, &pw->pw_gid) == -1)
		fatal("setgroups() failed");
	if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
		fatal("setresgid failed");
	if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
		fatal("setresuid() failed");
	endpwent();
}

void
addrcpy(struct sockaddr_in *dst, struct sockaddr_in *src)
{
		dst->sin_addr.s_addr = src->sin_addr.s_addr;
		dst->sin_port = src->sin_port;
		dst->sin_family = src->sin_family;
}

void
socketpair_prepare(int fd[2])
{
	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fd) == -1)
		fatal("socketpair_prepare");
	fd_nonblock(fd[0]);
	fd_nonblock(fd[1]);
}