1 /*
2  * Various trivial helper wrappers around standard functions
3  */
4 #include "cache.h"
5 #include "config.h"
6 
memory_limit_check(size_t size,int gentle)7 static int memory_limit_check(size_t size, int gentle)
8 {
9 	static size_t limit = 0;
10 	if (!limit) {
11 		limit = git_env_ulong("GIT_ALLOC_LIMIT", 0);
12 		if (!limit)
13 			limit = SIZE_MAX;
14 	}
15 	if (size > limit) {
16 		if (gentle) {
17 			error("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
18 			      (uintmax_t)size, (uintmax_t)limit);
19 			return -1;
20 		} else
21 			die("attempting to allocate %"PRIuMAX" over limit %"PRIuMAX,
22 			    (uintmax_t)size, (uintmax_t)limit);
23 	}
24 	return 0;
25 }
26 
xstrdup(const char * str)27 char *xstrdup(const char *str)
28 {
29 	char *ret = strdup(str);
30 	if (!ret)
31 		die("Out of memory, strdup failed");
32 	return ret;
33 }
34 
do_xmalloc(size_t size,int gentle)35 static void *do_xmalloc(size_t size, int gentle)
36 {
37 	void *ret;
38 
39 	if (memory_limit_check(size, gentle))
40 		return NULL;
41 	ret = malloc(size);
42 	if (!ret && !size)
43 		ret = malloc(1);
44 	if (!ret) {
45 		if (!gentle)
46 			die("Out of memory, malloc failed (tried to allocate %lu bytes)",
47 			    (unsigned long)size);
48 		else {
49 			error("Out of memory, malloc failed (tried to allocate %lu bytes)",
50 			      (unsigned long)size);
51 			return NULL;
52 		}
53 	}
54 #ifdef XMALLOC_POISON
55 	memset(ret, 0xA5, size);
56 #endif
57 	return ret;
58 }
59 
xmalloc(size_t size)60 void *xmalloc(size_t size)
61 {
62 	return do_xmalloc(size, 0);
63 }
64 
do_xmallocz(size_t size,int gentle)65 static void *do_xmallocz(size_t size, int gentle)
66 {
67 	void *ret;
68 	if (unsigned_add_overflows(size, 1)) {
69 		if (gentle) {
70 			error("Data too large to fit into virtual memory space.");
71 			return NULL;
72 		} else
73 			die("Data too large to fit into virtual memory space.");
74 	}
75 	ret = do_xmalloc(size + 1, gentle);
76 	if (ret)
77 		((char*)ret)[size] = 0;
78 	return ret;
79 }
80 
xmallocz(size_t size)81 void *xmallocz(size_t size)
82 {
83 	return do_xmallocz(size, 0);
84 }
85 
xmallocz_gently(size_t size)86 void *xmallocz_gently(size_t size)
87 {
88 	return do_xmallocz(size, 1);
89 }
90 
91 /*
92  * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
93  * "data" to the allocated memory, zero terminates the allocated memory,
94  * and returns a pointer to the allocated memory. If the allocation fails,
95  * the program dies.
96  */
xmemdupz(const void * data,size_t len)97 void *xmemdupz(const void *data, size_t len)
98 {
99 	return memcpy(xmallocz(len), data, len);
100 }
101 
xstrndup(const char * str,size_t len)102 char *xstrndup(const char *str, size_t len)
103 {
104 	char *p = memchr(str, '\0', len);
105 	return xmemdupz(str, p ? p - str : len);
106 }
107 
xstrncmpz(const char * s,const char * t,size_t len)108 int xstrncmpz(const char *s, const char *t, size_t len)
109 {
110 	int res = strncmp(s, t, len);
111 	if (res)
112 		return res;
113 	return s[len] == '\0' ? 0 : 1;
114 }
115 
xrealloc(void * ptr,size_t size)116 void *xrealloc(void *ptr, size_t size)
117 {
118 	void *ret;
119 
120 	if (!size) {
121 		free(ptr);
122 		return xmalloc(0);
123 	}
124 
125 	memory_limit_check(size, 0);
126 	ret = realloc(ptr, size);
127 	if (!ret)
128 		die("Out of memory, realloc failed");
129 	return ret;
130 }
131 
xcalloc(size_t nmemb,size_t size)132 void *xcalloc(size_t nmemb, size_t size)
133 {
134 	void *ret;
135 
136 	if (unsigned_mult_overflows(nmemb, size))
137 		die("data too large to fit into virtual memory space");
138 
139 	memory_limit_check(size * nmemb, 0);
140 	ret = calloc(nmemb, size);
141 	if (!ret && (!nmemb || !size))
142 		ret = calloc(1, 1);
143 	if (!ret)
144 		die("Out of memory, calloc failed");
145 	return ret;
146 }
147 
xsetenv(const char * name,const char * value,int overwrite)148 void xsetenv(const char *name, const char *value, int overwrite)
149 {
150 	if (setenv(name, value, overwrite))
151 		die_errno(_("could not setenv '%s'"), name ? name : "(null)");
152 }
153 
154 /*
155  * Limit size of IO chunks, because huge chunks only cause pain.  OS X
156  * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
157  * the absence of bugs, large chunks can result in bad latencies when
158  * you decide to kill the process.
159  *
160  * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
161  * that is smaller than that, clip it to SSIZE_MAX, as a call to
162  * read(2) or write(2) larger than that is allowed to fail.  As the last
163  * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
164  * to override this, if the definition of SSIZE_MAX given by the platform
165  * is broken.
166  */
167 #ifndef MAX_IO_SIZE
168 # define MAX_IO_SIZE_DEFAULT (8*1024*1024)
169 # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
170 #  define MAX_IO_SIZE SSIZE_MAX
171 # else
172 #  define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
173 # endif
174 #endif
175 
176 /**
177  * xopen() is the same as open(), but it die()s if the open() fails.
178  */
xopen(const char * path,int oflag,...)179 int xopen(const char *path, int oflag, ...)
180 {
181 	mode_t mode = 0;
182 	va_list ap;
183 
184 	/*
185 	 * va_arg() will have undefined behavior if the specified type is not
186 	 * compatible with the argument type. Since integers are promoted to
187 	 * ints, we fetch the next argument as an int, and then cast it to a
188 	 * mode_t to avoid undefined behavior.
189 	 */
190 	va_start(ap, oflag);
191 	if (oflag & O_CREAT)
192 		mode = va_arg(ap, int);
193 	va_end(ap);
194 
195 	for (;;) {
196 		int fd = open(path, oflag, mode);
197 		if (fd >= 0)
198 			return fd;
199 		if (errno == EINTR)
200 			continue;
201 
202 		if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
203 			die_errno(_("unable to create '%s'"), path);
204 		else if ((oflag & O_RDWR) == O_RDWR)
205 			die_errno(_("could not open '%s' for reading and writing"), path);
206 		else if ((oflag & O_WRONLY) == O_WRONLY)
207 			die_errno(_("could not open '%s' for writing"), path);
208 		else
209 			die_errno(_("could not open '%s' for reading"), path);
210 	}
211 }
212 
handle_nonblock(int fd,short poll_events,int err)213 static int handle_nonblock(int fd, short poll_events, int err)
214 {
215 	struct pollfd pfd;
216 
217 	if (err != EAGAIN && err != EWOULDBLOCK)
218 		return 0;
219 
220 	pfd.fd = fd;
221 	pfd.events = poll_events;
222 
223 	/*
224 	 * no need to check for errors, here;
225 	 * a subsequent read/write will detect unrecoverable errors
226 	 */
227 	poll(&pfd, 1, -1);
228 	return 1;
229 }
230 
231 /*
232  * xread() is the same a read(), but it automatically restarts read()
233  * operations with a recoverable error (EAGAIN and EINTR). xread()
234  * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
235  */
xread(int fd,void * buf,size_t len)236 ssize_t xread(int fd, void *buf, size_t len)
237 {
238 	ssize_t nr;
239 	if (len > MAX_IO_SIZE)
240 		len = MAX_IO_SIZE;
241 	while (1) {
242 		nr = read(fd, buf, len);
243 		if (nr < 0) {
244 			if (errno == EINTR)
245 				continue;
246 			if (handle_nonblock(fd, POLLIN, errno))
247 				continue;
248 		}
249 		return nr;
250 	}
251 }
252 
253 /*
254  * xwrite() is the same a write(), but it automatically restarts write()
255  * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
256  * GUARANTEE that "len" bytes is written even if the operation is successful.
257  */
xwrite(int fd,const void * buf,size_t len)258 ssize_t xwrite(int fd, const void *buf, size_t len)
259 {
260 	ssize_t nr;
261 	if (len > MAX_IO_SIZE)
262 		len = MAX_IO_SIZE;
263 	while (1) {
264 		nr = write(fd, buf, len);
265 		if (nr < 0) {
266 			if (errno == EINTR)
267 				continue;
268 			if (handle_nonblock(fd, POLLOUT, errno))
269 				continue;
270 		}
271 
272 		return nr;
273 	}
274 }
275 
276 /*
277  * xpread() is the same as pread(), but it automatically restarts pread()
278  * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES
279  * NOT GUARANTEE that "len" bytes is read even if the data is available.
280  */
xpread(int fd,void * buf,size_t len,off_t offset)281 ssize_t xpread(int fd, void *buf, size_t len, off_t offset)
282 {
283 	ssize_t nr;
284 	if (len > MAX_IO_SIZE)
285 		len = MAX_IO_SIZE;
286 	while (1) {
287 		nr = pread(fd, buf, len, offset);
288 		if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
289 			continue;
290 		return nr;
291 	}
292 }
293 
read_in_full(int fd,void * buf,size_t count)294 ssize_t read_in_full(int fd, void *buf, size_t count)
295 {
296 	char *p = buf;
297 	ssize_t total = 0;
298 
299 	while (count > 0) {
300 		ssize_t loaded = xread(fd, p, count);
301 		if (loaded < 0)
302 			return -1;
303 		if (loaded == 0)
304 			return total;
305 		count -= loaded;
306 		p += loaded;
307 		total += loaded;
308 	}
309 
310 	return total;
311 }
312 
write_in_full(int fd,const void * buf,size_t count)313 ssize_t write_in_full(int fd, const void *buf, size_t count)
314 {
315 	const char *p = buf;
316 	ssize_t total = 0;
317 
318 	while (count > 0) {
319 		ssize_t written = xwrite(fd, p, count);
320 		if (written < 0)
321 			return -1;
322 		if (!written) {
323 			errno = ENOSPC;
324 			return -1;
325 		}
326 		count -= written;
327 		p += written;
328 		total += written;
329 	}
330 
331 	return total;
332 }
333 
pread_in_full(int fd,void * buf,size_t count,off_t offset)334 ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset)
335 {
336 	char *p = buf;
337 	ssize_t total = 0;
338 
339 	while (count > 0) {
340 		ssize_t loaded = xpread(fd, p, count, offset);
341 		if (loaded < 0)
342 			return -1;
343 		if (loaded == 0)
344 			return total;
345 		count -= loaded;
346 		p += loaded;
347 		total += loaded;
348 		offset += loaded;
349 	}
350 
351 	return total;
352 }
353 
xdup(int fd)354 int xdup(int fd)
355 {
356 	int ret = dup(fd);
357 	if (ret < 0)
358 		die_errno("dup failed");
359 	return ret;
360 }
361 
362 /**
363  * xfopen() is the same as fopen(), but it die()s if the fopen() fails.
364  */
xfopen(const char * path,const char * mode)365 FILE *xfopen(const char *path, const char *mode)
366 {
367 	for (;;) {
368 		FILE *fp = fopen(path, mode);
369 		if (fp)
370 			return fp;
371 		if (errno == EINTR)
372 			continue;
373 
374 		if (*mode && mode[1] == '+')
375 			die_errno(_("could not open '%s' for reading and writing"), path);
376 		else if (*mode == 'w' || *mode == 'a')
377 			die_errno(_("could not open '%s' for writing"), path);
378 		else
379 			die_errno(_("could not open '%s' for reading"), path);
380 	}
381 }
382 
xfdopen(int fd,const char * mode)383 FILE *xfdopen(int fd, const char *mode)
384 {
385 	FILE *stream = fdopen(fd, mode);
386 	if (stream == NULL)
387 		die_errno("Out of memory? fdopen failed");
388 	return stream;
389 }
390 
fopen_for_writing(const char * path)391 FILE *fopen_for_writing(const char *path)
392 {
393 	FILE *ret = fopen(path, "w");
394 
395 	if (!ret && errno == EPERM) {
396 		if (!unlink(path))
397 			ret = fopen(path, "w");
398 		else
399 			errno = EPERM;
400 	}
401 	return ret;
402 }
403 
warn_on_inaccessible(const char * path)404 static void warn_on_inaccessible(const char *path)
405 {
406 	warning_errno(_("unable to access '%s'"), path);
407 }
408 
warn_on_fopen_errors(const char * path)409 int warn_on_fopen_errors(const char *path)
410 {
411 	if (errno != ENOENT && errno != ENOTDIR) {
412 		warn_on_inaccessible(path);
413 		return -1;
414 	}
415 
416 	return 0;
417 }
418 
fopen_or_warn(const char * path,const char * mode)419 FILE *fopen_or_warn(const char *path, const char *mode)
420 {
421 	FILE *fp = fopen(path, mode);
422 
423 	if (fp)
424 		return fp;
425 
426 	warn_on_fopen_errors(path);
427 	return NULL;
428 }
429 
xmkstemp(char * filename_template)430 int xmkstemp(char *filename_template)
431 {
432 	int fd;
433 	char origtemplate[PATH_MAX];
434 	strlcpy(origtemplate, filename_template, sizeof(origtemplate));
435 
436 	fd = mkstemp(filename_template);
437 	if (fd < 0) {
438 		int saved_errno = errno;
439 		const char *nonrelative_template;
440 
441 		if (strlen(filename_template) != strlen(origtemplate))
442 			filename_template = origtemplate;
443 
444 		nonrelative_template = absolute_path(filename_template);
445 		errno = saved_errno;
446 		die_errno("Unable to create temporary file '%s'",
447 			nonrelative_template);
448 	}
449 	return fd;
450 }
451 
452 /* Adapted from libiberty's mkstemp.c. */
453 
454 #undef TMP_MAX
455 #define TMP_MAX 16384
456 
git_mkstemps_mode(char * pattern,int suffix_len,int mode)457 int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
458 {
459 	static const char letters[] =
460 		"abcdefghijklmnopqrstuvwxyz"
461 		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
462 		"0123456789";
463 	static const int num_letters = ARRAY_SIZE(letters) - 1;
464 	static const char x_pattern[] = "XXXXXX";
465 	static const int num_x = ARRAY_SIZE(x_pattern) - 1;
466 	uint64_t value;
467 	struct timeval tv;
468 	char *filename_template;
469 	size_t len;
470 	int fd, count;
471 
472 	len = strlen(pattern);
473 
474 	if (len < num_x + suffix_len) {
475 		errno = EINVAL;
476 		return -1;
477 	}
478 
479 	if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
480 		errno = EINVAL;
481 		return -1;
482 	}
483 
484 	/*
485 	 * Replace pattern's XXXXXX characters with randomness.
486 	 * Try TMP_MAX different filenames.
487 	 */
488 	gettimeofday(&tv, NULL);
489 	value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
490 	filename_template = &pattern[len - num_x - suffix_len];
491 	for (count = 0; count < TMP_MAX; ++count) {
492 		uint64_t v = value;
493 		int i;
494 		/* Fill in the random bits. */
495 		for (i = 0; i < num_x; i++) {
496 			filename_template[i] = letters[v % num_letters];
497 			v /= num_letters;
498 		}
499 
500 		fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
501 		if (fd >= 0)
502 			return fd;
503 		/*
504 		 * Fatal error (EPERM, ENOSPC etc).
505 		 * It doesn't make sense to loop.
506 		 */
507 		if (errno != EEXIST)
508 			break;
509 		/*
510 		 * This is a random value.  It is only necessary that
511 		 * the next TMP_MAX values generated by adding 7777 to
512 		 * VALUE are different with (module 2^32).
513 		 */
514 		value += 7777;
515 	}
516 	/* We return the null string if we can't find a unique file name.  */
517 	pattern[0] = '\0';
518 	return -1;
519 }
520 
git_mkstemp_mode(char * pattern,int mode)521 int git_mkstemp_mode(char *pattern, int mode)
522 {
523 	/* mkstemp is just mkstemps with no suffix */
524 	return git_mkstemps_mode(pattern, 0, mode);
525 }
526 
xmkstemp_mode(char * filename_template,int mode)527 int xmkstemp_mode(char *filename_template, int mode)
528 {
529 	int fd;
530 	char origtemplate[PATH_MAX];
531 	strlcpy(origtemplate, filename_template, sizeof(origtemplate));
532 
533 	fd = git_mkstemp_mode(filename_template, mode);
534 	if (fd < 0) {
535 		int saved_errno = errno;
536 		const char *nonrelative_template;
537 
538 		if (!filename_template[0])
539 			filename_template = origtemplate;
540 
541 		nonrelative_template = absolute_path(filename_template);
542 		errno = saved_errno;
543 		die_errno("Unable to create temporary file '%s'",
544 			nonrelative_template);
545 	}
546 	return fd;
547 }
548 
warn_if_unremovable(const char * op,const char * file,int rc)549 static int warn_if_unremovable(const char *op, const char *file, int rc)
550 {
551 	int err;
552 	if (!rc || errno == ENOENT)
553 		return 0;
554 	err = errno;
555 	warning_errno("unable to %s '%s'", op, file);
556 	errno = err;
557 	return rc;
558 }
559 
unlink_or_msg(const char * file,struct strbuf * err)560 int unlink_or_msg(const char *file, struct strbuf *err)
561 {
562 	int rc = unlink(file);
563 
564 	assert(err);
565 
566 	if (!rc || errno == ENOENT)
567 		return 0;
568 
569 	strbuf_addf(err, "unable to unlink '%s': %s",
570 		    file, strerror(errno));
571 	return -1;
572 }
573 
unlink_or_warn(const char * file)574 int unlink_or_warn(const char *file)
575 {
576 	return warn_if_unremovable("unlink", file, unlink(file));
577 }
578 
rmdir_or_warn(const char * file)579 int rmdir_or_warn(const char *file)
580 {
581 	return warn_if_unremovable("rmdir", file, rmdir(file));
582 }
583 
remove_or_warn(unsigned int mode,const char * file)584 int remove_or_warn(unsigned int mode, const char *file)
585 {
586 	return S_ISGITLINK(mode) ? rmdir_or_warn(file) : unlink_or_warn(file);
587 }
588 
access_error_is_ok(int err,unsigned flag)589 static int access_error_is_ok(int err, unsigned flag)
590 {
591 	return (is_missing_file_error(err) ||
592 		((flag & ACCESS_EACCES_OK) && err == EACCES));
593 }
594 
access_or_warn(const char * path,int mode,unsigned flag)595 int access_or_warn(const char *path, int mode, unsigned flag)
596 {
597 	int ret = access(path, mode);
598 	if (ret && !access_error_is_ok(errno, flag))
599 		warn_on_inaccessible(path);
600 	return ret;
601 }
602 
access_or_die(const char * path,int mode,unsigned flag)603 int access_or_die(const char *path, int mode, unsigned flag)
604 {
605 	int ret = access(path, mode);
606 	if (ret && !access_error_is_ok(errno, flag))
607 		die_errno(_("unable to access '%s'"), path);
608 	return ret;
609 }
610 
xgetcwd(void)611 char *xgetcwd(void)
612 {
613 	struct strbuf sb = STRBUF_INIT;
614 	if (strbuf_getcwd(&sb))
615 		die_errno(_("unable to get current working directory"));
616 	return strbuf_detach(&sb, NULL);
617 }
618 
xsnprintf(char * dst,size_t max,const char * fmt,...)619 int xsnprintf(char *dst, size_t max, const char *fmt, ...)
620 {
621 	va_list ap;
622 	int len;
623 
624 	va_start(ap, fmt);
625 	len = vsnprintf(dst, max, fmt, ap);
626 	va_end(ap);
627 
628 	if (len < 0)
629 		BUG("your snprintf is broken");
630 	if (len >= max)
631 		BUG("attempt to snprintf into too-small buffer");
632 	return len;
633 }
634 
write_file_buf(const char * path,const char * buf,size_t len)635 void write_file_buf(const char *path, const char *buf, size_t len)
636 {
637 	int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
638 	if (write_in_full(fd, buf, len) < 0)
639 		die_errno(_("could not write to '%s'"), path);
640 	if (close(fd))
641 		die_errno(_("could not close '%s'"), path);
642 }
643 
write_file(const char * path,const char * fmt,...)644 void write_file(const char *path, const char *fmt, ...)
645 {
646 	va_list params;
647 	struct strbuf sb = STRBUF_INIT;
648 
649 	va_start(params, fmt);
650 	strbuf_vaddf(&sb, fmt, params);
651 	va_end(params);
652 
653 	strbuf_complete_line(&sb);
654 
655 	write_file_buf(path, sb.buf, sb.len);
656 	strbuf_release(&sb);
657 }
658 
sleep_millisec(int millisec)659 void sleep_millisec(int millisec)
660 {
661 	poll(NULL, 0, millisec);
662 }
663 
xgethostname(char * buf,size_t len)664 int xgethostname(char *buf, size_t len)
665 {
666 	/*
667 	 * If the full hostname doesn't fit in buf, POSIX does not
668 	 * specify whether the buffer will be null-terminated, so to
669 	 * be safe, do it ourselves.
670 	 */
671 	int ret = gethostname(buf, len);
672 	if (!ret)
673 		buf[len - 1] = 0;
674 	return ret;
675 }
676 
is_empty_or_missing_file(const char * filename)677 int is_empty_or_missing_file(const char *filename)
678 {
679 	struct stat st;
680 
681 	if (stat(filename, &st) < 0) {
682 		if (errno == ENOENT)
683 			return 1;
684 		die_errno(_("could not stat %s"), filename);
685 	}
686 
687 	return !st.st_size;
688 }
689 
open_nofollow(const char * path,int flags)690 int open_nofollow(const char *path, int flags)
691 {
692 #ifdef O_NOFOLLOW
693 	return open(path, flags | O_NOFOLLOW);
694 #else
695 	struct stat st;
696 	if (lstat(path, &st) < 0)
697 		return -1;
698 	if (S_ISLNK(st.st_mode)) {
699 		errno = ELOOP;
700 		return -1;
701 	}
702 	return open(path, flags);
703 #endif
704 }
705