1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 
9 #include "k5-int.h"
10 #include <sys/file.h>
11 #include <fcntl.h>
12 
13 #ifndef O_BINARY
14 #define	O_BINARY	0
15 #endif
16 
17 krb5_error_code
18 krb5_create_secure_file(krb5_context context, const char *pathname)
19 
20 {
21 	int 	fd;
22 	int 	open_flag;
23 
24 	open_flag = O_CREAT|O_EXCL|O_TRUNC|O_RDWR;
25 
26 	/*
27 	 * Make sure file name is reserved.
28 	 * The O_BINARY flag is not a supported flag in the Solaris
29 	 * open(2) system call, but it is included here to be consistent
30 	 * with other open calls in the Kerberos library code.
31 	 */
32 
33 	fd = open(pathname, open_flag | O_BINARY, 0600);
34 	if (fd == -1) {
35 		return (errno);
36 	} else {
37 		close(fd);
38 		return (0);
39 	}
40 }
41 
42 krb5_error_code
43 krb5_sync_disk_file(krb5_context context, FILE *fp)
44 {
45 	if (fp == NULL) {
46 		(void) fclose(fp);
47 		return (errno);
48 	}
49 	if ((fflush(fp) == EOF) || ferror(fp) || (fsync(fileno(fp)) == -1)) {
50 		return (errno);
51 	}
52 	return (0);
53 }
54