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 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * Portions of this source code were derived from Berkeley 4.3 BSD 32 * under license from the Regents of the University of California. 33 */ 34 35 #pragma ident "%Z%%M% %I% %E% SMI" 36 37 #include <sys/param.h> 38 #include <sys/isa_defs.h> 39 #include <sys/types.h> 40 #include <sys/sysmacros.h> 41 #include <sys/systm.h> 42 #include <sys/errno.h> 43 #include <sys/pathname.h> 44 #include <sys/vfs.h> 45 #include <sys/vnode.h> 46 #include <sys/proc.h> 47 #include <sys/uio.h> 48 #include <sys/debug.h> 49 #include <c2/audit.h> 50 51 /* 52 * Create a symbolic link. Similar to link or rename except target 53 * name is passed as string argument, not converted to vnode reference. 54 */ 55 int 56 symlink(char *target, char *linkname) 57 { 58 vnode_t *dvp; 59 struct vattr vattr; 60 struct pathname lpn; 61 char *tbuf; 62 size_t tlen; 63 int error; 64 65 top: 66 if (error = pn_get(linkname, UIO_USERSPACE, &lpn)) 67 return (set_errno(error)); 68 if (error = lookuppn(&lpn, NULL, NO_FOLLOW, &dvp, NULLVPP)) { 69 pn_free(&lpn); 70 if (error == ESTALE) 71 goto top; 72 return (set_errno(error)); 73 } 74 if (vn_is_readonly(dvp)) 75 error = EROFS; 76 else if (pn_fixslash(&lpn)) 77 error = ENOTDIR; 78 else { 79 tbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 80 if ((error = copyinstr(target, tbuf, MAXPATHLEN, &tlen)) == 0) { 81 vattr.va_type = VLNK; 82 vattr.va_mode = 0777; 83 vattr.va_mask = AT_TYPE|AT_MODE; 84 error = VOP_SYMLINK(dvp, lpn.pn_path, &vattr, 85 tbuf, CRED()); 86 #ifdef C2_AUDIT 87 if (audit_active) 88 audit_symlink_create(dvp, lpn.pn_path, 89 tbuf, error); 90 #endif /* C2_AUDIT */ 91 } 92 kmem_free(tbuf, MAXPATHLEN); 93 } 94 pn_free(&lpn); 95 VN_RELE(dvp); 96 if (error) { 97 if (error == ESTALE) 98 goto top; 99 return (set_errno(error)); 100 } 101 return (0); 102 } 103