xref: /onnv-gate/usr/src/lib/libdhcpsvc/modules/files/general.c (revision 0:68f95e015346)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file contains public API functions for module-level tasks.  For the
31  * semantics of these functions, please see the Enterprise DHCP
32  * Architecture Document.
33  *
34  * WARNING: This file is also compiled into the files0 module; if you make
35  *	    changes to this file which are not appropriate for files0, you
36  *	    will need to provide files0 with its own implementation.
37  */
38 
39 #include <assert.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <libgen.h>
46 #include <errno.h>
47 #include <dhcp_svc_public.h>
48 #include <dhcp_svc_private.h>		/* DSVC_SYNCH_CROSSHOST */
49 
50 #include "util.h"
51 
52 /*
53  * This symbol and its value tell the private layer that it must provide
54  * synchronization guarantees via dsvclockd(1M) before calling our *_dn()
55  * and *_dt() methods.  Please see $SRC/lib/libdhcpsvc/private/README.synch
56  */
57 int dsvc_synchtype = DSVC_SYNCH_DSVCD | DSVC_SYNCH_CROSSHOST;
58 
59 int
status(const char * location)60 status(const char *location)
61 {
62 	if (location != NULL) {
63 		if (access(location, F_OK|R_OK) == -1) {
64 			if (errno == ENOENT)
65 				return (DSVC_NO_LOCATION);
66 			return (syserr_to_dsvcerr(errno));
67 		}
68 	}
69 	return (DSVC_SUCCESS);
70 }
71 
72 int
version(int * vp)73 version(int *vp)
74 {
75 	*vp = DSVC_PUBLIC_VERSION;
76 	return (DSVC_SUCCESS);
77 }
78 
79 int
mklocation(const char * location)80 mklocation(const char *location)
81 {
82 	if (mkdirp(location, 0755) == -1) {
83 		switch (errno) {
84 		case ENAMETOOLONG:
85 		case ENOTDIR:
86 			return (DSVC_INVAL);
87 
88 		case EEXIST:
89 			return (DSVC_EXISTS);
90 
91 		case EROFS:
92 		case EPERM:
93 		case EACCES:
94 			return (DSVC_ACCESS);
95 
96 		default:
97 			return (DSVC_INTERNAL);
98 		}
99 	}
100 
101 	return (DSVC_SUCCESS);
102 }
103 
104 int
mkloctoken(const char * location,char * token,size_t tokensize)105 mkloctoken(const char *location, char *token, size_t tokensize)
106 {
107 	assert(tokensize >= MAXPATHLEN);
108 	if (realpath(location, token) == NULL)
109 		return (syserr_to_dsvcerr(errno));
110 
111 	return (DSVC_SUCCESS);
112 }
113