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