kernel - Move mplock to machine-independent C
[dragonfly.git] / sys / emulation / linux / linux_getcwd.c
1 /* $FreeBSD: src/sys/compat/linux/linux_getcwd.c,v 1.2.2.3 2001/11/05 19:08:22 marcel Exp $ */
2 /* $DragonFly: src/sys/emulation/linux/linux_getcwd.c,v 1.23 2006/12/23 00:27:02 swildner Exp $ */
3 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
4 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
5
6 /*-
7  * Copyright (c) 1999 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Bill Sommerfeld.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 #include "opt_compat.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/namei.h>
54 #include <sys/uio.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <sys/kern_syscall.h>
58 #include <vfs/ufs/dir.h>        /* XXX only for DIRBLKSIZ */
59
60 #include <sys/mplock2.h>
61
62 #include <arch_linux/linux.h>
63 #include <arch_linux/linux_proto.h>
64 #include "linux_util.h"
65
66 /*
67  * Find pathname of process's current directory.
68  *
69  * Use vfs vnode-to-name reverse cache; if that fails, fall back
70  * to reading directory contents.
71  *
72  * MPALMOSTSAFE
73  */
74 int
75 sys_linux_getcwd(struct linux_getcwd_args *args)
76 {
77         int buflen;
78         int error;
79         char *buf;
80         char *bp;
81
82 #ifdef DEBUG
83         kprintf("Linux-emul(%d): getcwd(%p, %d)\n", 
84             (curthread->td_proc ? (int)curthread->td_proc->p_pid : -1),
85             args->buf, args->bufsize);
86 #endif
87         buflen = args->bufsize;
88         if (buflen < 2)
89                 return (EINVAL);
90         if (buflen > MAXPATHLEN)
91                 buflen = MAXPATHLEN;
92
93         buf = kmalloc(buflen, M_TEMP, M_WAITOK);
94         get_mplock();
95         bp = kern_getcwd(buf, buflen, &error);
96         rel_mplock();
97         if (error == 0) {
98                 buflen = strlen(bp) + 1;
99                 error = copyout(bp, args->buf, buflen);
100                 args->sysmsg_result = buflen;
101         }
102         kfree(buf, M_TEMP);
103         return (error);
104 }
105