xref: /illumos-gate/usr/src/test/libmlrpc-tests/tests/common/util_common.c (revision ce8560eeb961d528e27685fcdd2ffb03e9478dbf)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Tintri by DDN, Inc. All rights reserved.
14  */
15 
16 /*
17  * Common utilities for libmlrpc tests.
18  */
19 
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 
27 uchar_t *
read_buf_from_file(char * file,uint32_t * size)28 read_buf_from_file(char *file, uint32_t *size)
29 {
30 	struct stat stats;
31 	uchar_t *buf;
32 	FILE *fp;
33 	size_t nread;
34 	int rc;
35 
36 	errno = 0;
37 	rc = stat(file, &stats);
38 
39 	if (rc < 0) {
40 		fprintf(stderr, "stat failed with rc %d:\n", rc);
41 		perror(file);
42 		return (NULL);
43 	}
44 
45 	buf = malloc(stats.st_size);
46 
47 	if (buf == NULL) {
48 		fprintf(stderr, "couldn't allocate buffer\n");
49 		return (NULL);
50 	}
51 	errno = 0;
52 	fp = fopen(file, "r");
53 	if (fp == NULL) {
54 		fprintf(stderr, "fopen failed to open file:\n");
55 		perror(file);
56 		free(buf);
57 		return (NULL);
58 	}
59 
60 	errno = 0;
61 	nread = fread(buf, 1, stats.st_size, fp);
62 	if (nread == EOF && errno != 0) {
63 		fprintf(stderr, "fread failed:\n");
64 		perror(file);
65 		free(buf);
66 		return (NULL);
67 	}
68 
69 	(void) fclose(fp);
70 	if (nread == EOF) {
71 		free(buf);
72 		buf = NULL;
73 	}
74 	*size = nread;
75 	return (buf);
76 }
77 
78 /*
79  * smb_token_log() outputs to syslog. The library defines syslog to be
80  * smb_syslog, which it defines as NODIRECT to allow fksmbd to provide
81  * its own version. We use that to redirect syslog to stderr, so that
82  * we can print the token output to a useful location.
83  */
84 void
smb_syslog(int pri,const char * fmt,...)85 smb_syslog(int pri, const char *fmt, ...)
86 {
87 	va_list ap;
88 
89 	va_start(ap, fmt);
90 	(void) vfprintf(stderr, fmt, ap);
91 	va_end(ap);
92 	fprintf(stderr, "\n");
93 }
94