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