Whamcloud - gitweb
LU-2800 autoconf: remove obsolete autoconf options
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-curproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-curproc.c
37  *
38  * Lustre curproc API implementation for Linux kernel
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #include <linux/sched.h>
44 #include <linux/fs_struct.h>
45
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
48
49 #define DEBUG_SUBSYSTEM S_LNET
50
51 #include <libcfs/libcfs.h>
52
53 /*
54  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
55  * for Linux kernel.
56  */
57
58 uid_t  cfs_curproc_uid(void)
59 {
60         return current_uid();
61 }
62
63 gid_t  cfs_curproc_gid(void)
64 {
65         return current_gid();
66 }
67
68 uid_t  cfs_curproc_fsuid(void)
69 {
70         return current_fsuid();
71 }
72
73 uid_t  cfs_curproc_euid(void)
74 {
75         return current_euid();
76 }
77
78 uid_t  cfs_curproc_egid(void)
79 {
80         return current_egid();
81 }
82
83 gid_t  cfs_curproc_fsgid(void)
84 {
85         return current_fsgid();
86 }
87
88 pid_t  cfs_curproc_pid(void)
89 {
90         return current->pid;
91 }
92
93 int    cfs_curproc_groups_nr(void)
94 {
95         int nr;
96
97         task_lock(current);
98         nr = current_cred()->group_info->ngroups;
99         task_unlock(current);
100         return nr;
101 }
102
103 void   cfs_curproc_groups_dump(gid_t *array, int size)
104 {
105         task_lock(current);
106         size = min_t(int, size, current_cred()->group_info->ngroups);
107         memcpy(array, current_cred()->group_info->blocks[0], size * sizeof(__u32));
108         task_unlock(current);
109 }
110
111
112 int    cfs_curproc_is_in_groups(gid_t gid)
113 {
114         return in_group_p(gid);
115 }
116
117 mode_t cfs_curproc_umask(void)
118 {
119         return current->fs->umask;
120 }
121
122 char  *cfs_curproc_comm(void)
123 {
124         return current->comm;
125 }
126
127 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
128 #define cfs_cap_pack(cap) (cap)
129 #define cfs_cap_unpack(cap) (cap)
130
131 void cfs_cap_raise(cfs_cap_t cap)
132 {
133         struct cred *cred;
134         if ((cred = prepare_creds())) {
135                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
136                 commit_creds(cred);
137         }
138 }
139
140 void cfs_cap_lower(cfs_cap_t cap)
141 {
142         struct cred *cred;
143         if ((cred = prepare_creds())) {
144                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
145                 commit_creds(cred);
146         }
147 }
148
149 int cfs_cap_raised(cfs_cap_t cap)
150 {
151         return cap_raised(current_cap(), cfs_cap_unpack(cap));
152 }
153
154 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
155 {
156 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
157         *cap = cfs_cap_pack(kcap);
158 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
159         *cap = cfs_cap_pack(kcap[0]);
160 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
161         /* XXX lost high byte */
162         *cap = cfs_cap_pack(kcap.cap[0]);
163 #else
164         #error "need correct _KERNEL_CAPABILITY_VERSION "
165 #endif
166 }
167
168 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
169 {
170 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
171         *kcap = cfs_cap_unpack(cap);
172 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
173         (*kcap)[0] = cfs_cap_unpack(cap);
174 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
175         kcap->cap[0] = cfs_cap_unpack(cap);
176 #else
177         #error "need correct _KERNEL_CAPABILITY_VERSION "
178 #endif
179 }
180
181 cfs_cap_t cfs_curproc_cap_pack(void)
182 {
183         cfs_cap_t cap;
184         cfs_kernel_cap_pack(current_cap(), &cap);
185         return cap;
186 }
187
188 void cfs_curproc_cap_unpack(cfs_cap_t cap)
189 {
190         struct cred *cred;
191         if ((cred = prepare_creds())) {
192                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
193                 commit_creds(cred);
194         }
195 }
196
197 int cfs_capable(cfs_cap_t cap)
198 {
199         return capable(cfs_cap_unpack(cap));
200 }
201
202 /* Check if task is running in 32-bit API mode, for the purpose of
203  * userspace binary interfaces.  On 32-bit Linux this is (unfortunately)
204  * always true, even if the application is using LARGEFILE64 and 64-bit
205  * APIs, because Linux provides no way for the filesystem to know if it
206  * is called via 32-bit or 64-bit APIs.  Other clients may vary.  On
207  * 64-bit systems, this will only be true if the binary is calling a
208  * 32-bit system call. */
209 int cfs_curproc_is_32bit(void)
210 {
211 #ifdef HAVE_IS_COMPAT_TASK
212         return is_compat_task();
213 #else
214         return (BITS_PER_LONG == 32);
215 #endif
216 }
217
218 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
219                                  void *buf, int len, int write)
220 {
221         /* Just copied from kernel for the kernels which doesn't
222          * have access_process_vm() exported */
223         struct mm_struct *mm;
224         struct vm_area_struct *vma;
225         struct page *page;
226         void *old_buf = buf;
227
228         mm = get_task_mm(tsk);
229         if (!mm)
230                 return 0;
231
232         down_read(&mm->mmap_sem);
233         /* ignore errors, just check how much was sucessfully transfered */
234         while (len) {
235                 int bytes, rc, offset;
236                 void *maddr;
237
238                 rc = get_user_pages(tsk, mm, addr, 1,
239                                      write, 1, &page, &vma);
240                 if (rc <= 0)
241                         break;
242
243                 bytes = len;
244                 offset = addr & (PAGE_SIZE-1);
245                 if (bytes > PAGE_SIZE-offset)
246                         bytes = PAGE_SIZE-offset;
247
248                 maddr = kmap(page);
249                 if (write) {
250                         copy_to_user_page(vma, page, addr,
251                                           maddr + offset, buf, bytes);
252                         set_page_dirty_lock(page);
253                 } else {
254                         copy_from_user_page(vma, page, addr,
255                                             buf, maddr + offset, bytes);
256                 }
257                 kunmap(page);
258                 page_cache_release(page);
259                 len -= bytes;
260                 buf += bytes;
261                 addr += bytes;
262         }
263         up_read(&mm->mmap_sem);
264         mmput(mm);
265
266         return buf - old_buf;
267 }
268
269 /* Read the environment variable of current process specified by @key. */
270 int cfs_get_environ(const char *key, char *value, int *val_len)
271 {
272         struct mm_struct *mm;
273         char *buffer, *tmp_buf = NULL;
274         int buf_len = PAGE_CACHE_SIZE;
275         int key_len = strlen(key);
276         unsigned long addr;
277         int rc;
278         ENTRY;
279
280         buffer = kmalloc(buf_len, GFP_USER);
281         if (!buffer)
282                 RETURN(-ENOMEM);
283
284         mm = get_task_mm(current);
285         if (!mm) {
286                 kfree(buffer);
287                 RETURN(-EINVAL);
288         }
289
290         /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
291          * which is already holding mmap_sem for writes.  If some other
292          * thread gets the write lock in the meantime, this thread will
293          * block, but at least it won't deadlock on itself.  LU-1735 */
294         if (down_read_trylock(&mm->mmap_sem) == 0)
295                 return -EDEADLK;
296         up_read(&mm->mmap_sem);
297
298         addr = mm->env_start;
299         while (addr < mm->env_end) {
300                 int this_len, retval, scan_len;
301                 char *env_start, *env_end;
302
303                 memset(buffer, 0, buf_len);
304
305                 this_len = min_t(int, mm->env_end - addr, buf_len);
306                 retval = cfs_access_process_vm(current, addr, buffer,
307                                                this_len, 0);
308                 if (retval != this_len)
309                         break;
310
311                 addr += retval;
312
313                 /* Parse the buffer to find out the specified key/value pair.
314                  * The "key=value" entries are separated by '\0'. */
315                 env_start = buffer;
316                 scan_len = this_len;
317                 while (scan_len) {
318                         char *entry;
319                         int entry_len;
320
321                         env_end = memscan(env_start, '\0', scan_len);
322                         LASSERT(env_end >= env_start &&
323                                 env_end <= env_start + scan_len);
324
325                         /* The last entry of this buffer cross the buffer
326                          * boundary, reread it in next cycle. */
327                         if (unlikely(env_end - env_start == scan_len)) {
328                                 /* This entry is too large to fit in buffer */
329                                 if (unlikely(scan_len == this_len)) {
330                                         CERROR("Too long env variable.\n");
331                                         GOTO(out, rc = -EINVAL);
332                                 }
333                                 addr -= scan_len;
334                                 break;
335                         }
336
337                         entry = env_start;
338                         entry_len = env_end - env_start;
339
340                         /* Key length + length of '=' */
341                         if (entry_len > key_len + 1 &&
342                             !memcmp(entry, key, key_len)) {
343                                 entry += key_len + 1;
344                                 entry_len -= key_len + 1;
345                                 /* The 'value' buffer passed in is too small.*/
346                                 if (entry_len >= *val_len)
347                                         GOTO(out, rc = -EOVERFLOW);
348
349                                 memcpy(value, entry, entry_len);
350                                 *val_len = entry_len;
351                                 GOTO(out, rc = 0);
352                         }
353
354                         scan_len -= (env_end - env_start + 1);
355                         env_start = env_end + 1;
356                 }
357         }
358         GOTO(out, rc = -ENOENT);
359
360 out:
361         mmput(mm);
362         kfree((void *)buffer);
363         if (tmp_buf)
364                 kfree((void *)tmp_buf);
365         return rc;
366 }
367 EXPORT_SYMBOL(cfs_get_environ);
368
369 EXPORT_SYMBOL(cfs_curproc_uid);
370 EXPORT_SYMBOL(cfs_curproc_pid);
371 EXPORT_SYMBOL(cfs_curproc_euid);
372 EXPORT_SYMBOL(cfs_curproc_gid);
373 EXPORT_SYMBOL(cfs_curproc_egid);
374 EXPORT_SYMBOL(cfs_curproc_fsuid);
375 EXPORT_SYMBOL(cfs_curproc_fsgid);
376 EXPORT_SYMBOL(cfs_curproc_umask);
377 EXPORT_SYMBOL(cfs_curproc_comm);
378 EXPORT_SYMBOL(cfs_curproc_groups_nr);
379 EXPORT_SYMBOL(cfs_curproc_groups_dump);
380 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
381 EXPORT_SYMBOL(cfs_cap_raise);
382 EXPORT_SYMBOL(cfs_cap_lower);
383 EXPORT_SYMBOL(cfs_cap_raised);
384 EXPORT_SYMBOL(cfs_curproc_cap_pack);
385 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
386 EXPORT_SYMBOL(cfs_capable);
387 EXPORT_SYMBOL(cfs_curproc_is_32bit);
388
389 /*
390  * Local variables:
391  * c-indentation-style: "K&R"
392  * c-basic-offset: 8
393  * tab-width: 8
394  * fill-column: 80
395  * scroll-step: 1
396  * End:
397  */