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