xref: /titanic_52/usr/src/lib/libnsl/rpc/getdname.c (revision 02e56f3f1bfc8d9977bafb8cb5202f576dcded27)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * getdname.c
39  *	Gets and sets the domain name of the system
40  */
41 
42 #include "rpc_mt.h"
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/types.h>
46 #include <sys/utsname.h>
47 #include <sys/systeminfo.h>
48 #include <sys/time.h>
49 #include <syslog.h>
50 
51 #ifndef SI_SRPC_DOMAIN
52 #define	use_file
53 #endif
54 
55 #ifdef use_file
56 char DOMAIN[] = "/etc/domain";
57 #endif
58 
59 int setdomainname();
60 
61 #ifdef use_file
62 static char *domainname;
63 #endif
64 
65 extern mutex_t	dname_lock;
66 
67 int
68 getdomainname(name, namelen)
69 	char *name;
70 	int namelen;
71 {
72 #ifdef use_file
73 	FILE *domain_fd;
74 	char *line;
75 
76 	(void) mutex_lock(&dname_lock);
77 	if (domainname) {
78 		(void) strncpy(name, domainname, namelen);
79 		(void) mutex_unlock(&dname_lock);
80 		return (0);
81 	}
82 
83 	domainname = calloc(1, 256);
84 	if (domainname == NULL) {
85 		syslog(LOG_ERR, "getdomainname : out of memory.");
86 		(void) mutex_unlock(&dname_lock);
87 		return (-1);
88 	}
89 
90 	if ((domain_fd = fopen(DOMAIN, "r")) == NULL) {
91 
92 		(void) mutex_unlock(&dname_lock);
93 		return (-1);
94 	}
95 	if (fscanf(domain_fd, "%s", domainname) == NULL) {
96 		(void) fclose(domain_fd);
97 		(void) mutex_unlock(&dname_lock);
98 		return (-1);
99 	}
100 	(void) fclose(domain_fd);
101 	(void) strncpy(name, domainname, namelen);
102 	(void) mutex_unlock(&dname_lock);
103 	return (0);
104 #else
105 	int sysinfostatus;
106 
107 	sysinfostatus = sysinfo(SI_SRPC_DOMAIN, name, namelen);
108 
109 	return ((sysinfostatus < 0) ? -1 : 0);
110 #endif
111 }
112 
113 int
114 setdomainname(domain, len)
115 	char *domain;
116 	int len;
117 {
118 #ifdef use_file
119 
120 	FILE *domain_fd;
121 
122 	(void) mutex_lock(&dname_lock);
123 	if (domainname)
124 		free(domainname);
125 
126 	if ((domain_fd = fopen(DOMAIN, "w")) == NULL) {
127 		(void) mutex_unlock(&dname_lock);
128 		return (-1);
129 	}
130 	if (fputs(domain, domain_fd) == NULL) {
131 		(void) mutex_unlock(&dname_lock);
132 		return (-1);
133 	}
134 	(void) fclose(domain_fd);
135 	domainname = calloc(1, 256);
136 	if (domainname == NULL) {
137 		syslog(LOG_ERR, "setdomainname : out of memory.");
138 		(void) mutex_unlock(&dname_lock);
139 		return (-1);
140 	}
141 	(void) strncpy(domainname, domain, len);
142 	(void) mutex_unlock(&dname_lock);
143 	return (0);
144 #else
145 	int sysinfostatus;
146 
147 	sysinfostatus = sysinfo(SI_SET_SRPC_DOMAIN,
148 				domain, len + 1); /* add null */
149 	return ((sysinfostatus < 0) ? -1 : 0);
150 #endif
151 }
152