1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_xxx.c 8.2 (Berkeley) 11/14/93 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_compat.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/sysproto.h> 44 #include <sys/kernel.h> 45 #include <sys/proc.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/sysctl.h> 49 #include <sys/utsname.h> 50 51 52 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 53 54 #ifndef _SYS_SYSPROTO_H_ 55 struct gethostname_args { 56 char *hostname; 57 u_int len; 58 }; 59 #endif 60 /* 61 * MPSAFE 62 */ 63 /* ARGSUSED */ 64 int 65 ogethostname(td, uap) 66 struct thread *td; 67 struct gethostname_args *uap; 68 { 69 int name[2]; 70 int error; 71 size_t len = uap->len; 72 73 name[0] = CTL_KERN; 74 name[1] = KERN_HOSTNAME; 75 mtx_lock(&Giant); 76 error = userland_sysctl(td, name, 2, uap->hostname, &len, 1, 0, 0, 0); 77 mtx_unlock(&Giant); 78 return(error); 79 } 80 81 #ifndef _SYS_SYSPROTO_H_ 82 struct sethostname_args { 83 char *hostname; 84 u_int len; 85 }; 86 #endif 87 /* 88 * MPSAFE 89 */ 90 /* ARGSUSED */ 91 int 92 osethostname(td, uap) 93 struct thread *td; 94 register struct sethostname_args *uap; 95 { 96 int name[2]; 97 int error; 98 99 name[0] = CTL_KERN; 100 name[1] = KERN_HOSTNAME; 101 mtx_lock(&Giant); 102 if ((error = suser_cred(td->td_ucred, PRISON_ROOT)) == 0) { 103 error = userland_sysctl(td, name, 2, 0, 0, 0, 104 uap->hostname, uap->len, 0); 105 } 106 mtx_unlock(&Giant); 107 return (error); 108 } 109 110 #ifndef _SYS_SYSPROTO_H_ 111 struct ogethostid_args { 112 int dummy; 113 }; 114 #endif 115 /* 116 * MPSAFE 117 */ 118 /* ARGSUSED */ 119 int 120 ogethostid(td, uap) 121 struct thread *td; 122 struct ogethostid_args *uap; 123 { 124 125 *(long *)(td->td_retval) = hostid; 126 return (0); 127 } 128 #endif /* COMPAT_43 || COMPAT_SUNOS */ 129 130 #ifdef COMPAT_43 131 #ifndef _SYS_SYSPROTO_H_ 132 struct osethostid_args { 133 long hostid; 134 }; 135 #endif 136 /* 137 * MPSAFE 138 */ 139 /* ARGSUSED */ 140 int 141 osethostid(td, uap) 142 struct thread *td; 143 struct osethostid_args *uap; 144 { 145 int error; 146 147 if ((error = suser(td))) 148 return (error); 149 mtx_lock(&Giant); 150 hostid = uap->hostid; 151 mtx_unlock(&Giant); 152 return (0); 153 } 154 155 /* 156 * MPSAFE 157 */ 158 int 159 oquota(td, uap) 160 struct thread *td; 161 struct oquota_args *uap; 162 { 163 return (ENOSYS); 164 } 165 #endif /* COMPAT_43 */ 166 167 /* 168 * This is the FreeBSD-1.1 compatable uname(2) interface. These 169 * days it is done in libc as a wrapper around a bunch of sysctl's. 170 * This must maintain the old 1.1 binary ABI. 171 */ 172 #if SYS_NMLN != 32 173 #error "FreeBSD-1.1 uname syscall has been broken" 174 #endif 175 #ifndef _SYS_SYSPROTO_H_ 176 struct uname_args { 177 struct utsname *name; 178 }; 179 #endif 180 181 /* 182 * MPSAFE 183 */ 184 /* ARGSUSED */ 185 int 186 uname(td, uap) 187 struct thread *td; 188 struct uname_args *uap; 189 { 190 int name[2], error; 191 size_t len; 192 char *s, *us; 193 194 name[0] = CTL_KERN; 195 name[1] = KERN_OSTYPE; 196 len = sizeof (uap->name->sysname); 197 mtx_lock(&Giant); 198 error = userland_sysctl(td, name, 2, uap->name->sysname, &len, 199 1, 0, 0, 0); 200 if (error) 201 goto done2; 202 subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0); 203 204 name[1] = KERN_HOSTNAME; 205 len = sizeof uap->name->nodename; 206 error = userland_sysctl(td, name, 2, uap->name->nodename, &len, 207 1, 0, 0, 0); 208 if (error) 209 goto done2; 210 subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0); 211 212 name[1] = KERN_OSRELEASE; 213 len = sizeof uap->name->release; 214 error = userland_sysctl(td, name, 2, uap->name->release, &len, 215 1, 0, 0, 0); 216 if (error) 217 goto done2; 218 subyte( uap->name->release + sizeof(uap->name->release) - 1, 0); 219 220 /* 221 name = KERN_VERSION; 222 len = sizeof uap->name->version; 223 error = userland_sysctl(td, name, 2, uap->name->version, &len, 224 1, 0, 0, 0); 225 if (error) 226 goto done2; 227 subyte( uap->name->version + sizeof(uap->name->version) - 1, 0); 228 */ 229 230 /* 231 * this stupid hackery to make the version field look like FreeBSD 1.1 232 */ 233 for(s = version; *s && *s != '#'; s++); 234 235 for(us = uap->name->version; *s && *s != ':'; s++) { 236 error = subyte( us++, *s); 237 if (error) 238 goto done2; 239 } 240 error = subyte( us++, 0); 241 if (error) 242 goto done2; 243 244 name[0] = CTL_HW; 245 name[1] = HW_MACHINE; 246 len = sizeof uap->name->machine; 247 error = userland_sysctl(td, name, 2, uap->name->machine, &len, 248 1, 0, 0, 0); 249 if (error) 250 goto done2; 251 subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0); 252 done2: 253 mtx_unlock(&Giant); 254 return (error); 255 } 256 257 #ifndef _SYS_SYSPROTO_H_ 258 struct getdomainname_args { 259 char *domainname; 260 int len; 261 }; 262 #endif 263 264 /* 265 * MPSAFE 266 */ 267 /* ARGSUSED */ 268 int 269 getdomainname(td, uap) 270 struct thread *td; 271 struct getdomainname_args *uap; 272 { 273 int domainnamelen; 274 int error; 275 276 mtx_lock(&Giant); 277 domainnamelen = strlen(domainname) + 1; 278 if ((u_int)uap->len > domainnamelen) 279 uap->len = domainnamelen; 280 error = copyout(domainname, uap->domainname, uap->len); 281 mtx_unlock(&Giant); 282 return (error); 283 } 284 285 #ifndef _SYS_SYSPROTO_H_ 286 struct setdomainname_args { 287 char *domainname; 288 int len; 289 }; 290 #endif 291 292 /* 293 * MPSAFE 294 */ 295 /* ARGSUSED */ 296 int 297 setdomainname(td, uap) 298 struct thread *td; 299 struct setdomainname_args *uap; 300 { 301 int error, domainnamelen; 302 303 mtx_lock(&Giant); 304 if ((error = suser(td))) 305 goto done2; 306 if ((u_int)uap->len > sizeof (domainname) - 1) { 307 error = EINVAL; 308 goto done2; 309 } 310 domainnamelen = uap->len; 311 error = copyin(uap->domainname, domainname, uap->len); 312 domainname[domainnamelen] = 0; 313 done2: 314 mtx_unlock(&Giant); 315 return (error); 316 } 317 318