summaryrefslogtreecommitdiff
path: root/minecctl/misc.c
blob: 90189dc102b47f47a55a114452a3c540b6aacabb (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* SPDX-License-Identifier: GPL-2.0 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>

#include "shared/utils.h"
#include "misc.h"
#include "minecctl.h"

static const char *get_homedir()
{
	const char *e;
        struct passwd *passwd;
        uid_t uid;

	e = getenv("HOME");
	if (e && e[0] == '/')
		return e;

	uid = getuid();
	if (uid == 0)
		return "/root";

        passwd = getpwuid(uid);
	if (passwd && passwd->pw_dir[0] == '/')
		return passwd->pw_dir;

	return NULL;
}

int open_subdir(int dfd, const char *subdir, bool nofail)
{
	int sfd;

	if (nofail && mkdirat(dfd, subdir, 0777) < 0 && errno != EEXIST) {
		error("Unable to create subdirectory %s: %m", subdir);
		return -1;
	}

	sfd = openat(dfd, subdir, O_PATH | O_CLOEXEC | O_DIRECTORY);
	if (sfd < 0 && nofail)
		error("Unable to open subdirectory %s: %m", subdir);

	return sfd;
}

int open_xdg_dir(const char *envname, const char *altpath, bool nofail)
{
        const char *e;
	const char *h;
	int dfd, hfd;

	e = getenv(envname);
	if (e && e[0] == '/') {
		dfd = open(e, O_PATH | O_CLOEXEC | O_DIRECTORY);
		if (dfd < 0)
			error("Unable to open $%s(%s): %m", envname, e);
		return dfd;
	}

	h = get_homedir();
	if (!h) {
		error("Unable to determine home directory");
		return -1;
	}

	hfd = open(h, O_PATH | O_CLOEXEC | O_DIRECTORY);
	if (hfd < 0) {
		error("Unable to open $HOME(%s): %m", h);
		return -1;
	}

	dfd = open_subdir(hfd, altpath, nofail);
	close(hfd);
	return dfd;
}

/* FIXME: Can be shared */
void set_use_colors()
{
	int fd;
	const char *e;

	if (getenv("NO_COLOR"))
		return;

	fd = fileno(stderr);
	if (fd < 0)
		return;

	if (!isatty(fd))
		return;

	e = getenv("TERM");
	if (!e)
		return;

	if (streq(e, "dumb"))
		return;

	enable_colors();
}

char **strv_copy(char *const *strv)
{
	size_t len = 1;
	char **rv;
	char **to;

	for (char *const *str = strv; *str; str++)
		len++;

	rv = zmalloc(len * sizeof(char *));
	to = rv;

	for (char *const *str = strv; *str; str++)
		*(to++) = xstrdup(*str);

	return rv;
}

char **strv_from_strs(const char *first, ...)
{
	size_t len = 1;
	va_list ap;
	char **rv, **to;

	if (first) {
		va_start(ap, first);
		while (va_arg(ap, const char *))
			len++;
		va_end(ap);
	}

	rv = zmalloc(len * sizeof(char *));
	to = rv;
	*(to++) = first ? xstrdup(first) : NULL;

	if (first) {
		const char *str;

		va_start(ap, first);
		while ((str = va_arg(ap, const char *)))
			*(to++) = xstrdup(str);
		va_end(ap);
		*to = NULL;
	}

	return rv;
}

char *strv_join(char *const *strv)
{
	size_t len = 0;
	char *rv, *to;

	for (char *const *str = strv; *str; str++)
		len += strlen(*str) + 1;

	if (len == 0)
		return NULL;

	rv = zmalloc(len);
	to = rv;

	for (char *const *str = strv; *str; str++) {
		if (str != strv)
			*(to++) = ' ';
		to = stpcpy(to, *str);
	}

	return rv;
}

void strv_free(char **strv)
{
	if (!strv)
		return;

	for (char **str = strv; *str; str++)
		xfree(*str);

	xfree(strv);
}

int connect_any(struct list_head *addrs, struct saddr **rsaddr,
		const char **error)
{
	struct saddr *saddr;
	int sfd;

	if (list_empty(addrs)) {
		*error = "no address to connect to";
		return -1;
	}

	list_for_each_entry(saddr, addrs, list) {
		verbose("Attempting connection to %s", saddr->addrstr);
		sfd = socket(saddr->st.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0);
		if (sfd < 0) {
			*error = "failed to create socket";
			return -1;
		}

		socket_set_low_latency(sfd, true, true, true);

		if (connect(sfd, (struct sockaddr *)&saddr->st,
			    saddr->addrlen) < 0) {
			close(sfd);
			sfd = -1;
			continue;
		}

		if (rsaddr)
			*rsaddr = saddr;
		break;
	}

	if (sfd < 0) {
		*error = "failed to connect to remote host";
		return -1;
	}

	return sfd;
}

char *ask_password()
{
	struct termios old, new;
	char *password = NULL;
	size_t len = 0;
	ssize_t r;

	if (!isatty(STDIN_FILENO))
		return NULL;

	if (tcgetattr(STDIN_FILENO, &old) < 0)
		return NULL;

	new = old;
	new.c_lflag &= ~ECHO;
	new.c_lflag |= ICANON;
	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) < 0)
		return NULL;

	fprintf(stderr, "Password: ");
	r = getline(&password, &len, stdin);

	tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);

	if (r < 0) {
		info("Error in getline: %m");
		clearerr(stdin);
		free(password);
		return NULL;
	}

	while (r > 0 && password[r - 1] == '\n')
		password[--r] = '\0';

	return password;
}

void __debug(_unused_ enum debug_lvl lvl, const char *fmt, ...)
{
	va_list ap;
	const char *color = NULL;


	if (lvl & DBG_ERROR)
		color = ansi_red;
	else if (!(lvl & (DBG_INFO | DBG_VERBOSE)))
		color = ansi_grey;

	if (color)
		fprintf(stderr, "%s", color);

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (color)
		fprintf(stderr, "%s", ansi_normal);
}

_noreturn_ void __die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	exit(EXIT_FAILURE);
}

void *__zmalloc(const char *fn, int line, size_t size)
{
	void *ptr;

	assert_die(!empty_str(fn) && line > 0 && size > 0, "invalid arguments");

	ptr = calloc(1, size);
	if (!ptr)
		die("malloc: %m");
	return ptr;
}

char *__xstrdup(const char *fn, int line, const char *s)
{
	char *ptr;

	assert_die(!empty_str(fn) && line > 0 && !empty_str(s),
		   "invalid arguments");

	ptr = strdup(s);
	if (!ptr)
		die("strdup: %m");
	return ptr;
}

char *__xstrndup(const char *fn, int line, const char *s, size_t n)
{
	char *ptr;

	assert_die(!empty_str(fn) && line > 0 && !empty_str(s) && n > 0,
		   "invalid arguments");

	ptr = strndup(s, n);
	if (!ptr)
		die("strdup: %m");
	return ptr;
}

void __xfree(const char *fn, int line, void *ptr)
{
	assert_die(!empty_str(fn) && line > 0, "invalid arguments");

	free(ptr);
}