1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/winnt/winnt-curproc.c
38 * Impletion of winnt curproc routines.
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <libcfs/libcfs.h>
46 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
50 cfs_task_t this_task =
51 { /* umask */ 0,/* blocked*/0, /* pid */ 0, /* pgrp */ 0,
52 /* uid,euid,suid,fsuid */ 0, 0, 0, 0,
53 /* gid_t gid,egid,sgid,fsgid */ 0, 0, 0, 0,
54 /* ngroups*/ 1, /*cgroups*/ 0, /*groups*/ 0,
55 /* group_info */ NULL,
56 /* cap_effective, cap_inheritable, cap_permitted */ 0, 0, 0,
58 /* journal_info */ NULL
62 uid_t cfs_curproc_uid(void)
67 gid_t cfs_curproc_gid(void)
72 uid_t cfs_curproc_fsuid(void)
74 return this_task.fsuid;
77 gid_t cfs_curproc_fsgid(void)
79 return this_task.fsgid;
82 pid_t cfs_curproc_pid(void)
84 return cfs_current()->pid;
87 int cfs_curproc_groups_nr(void)
89 return this_task.ngroups;
92 void cfs_curproc_groups_dump(gid_t *array, int size)
94 LASSERT(size <= NGROUPS);
95 size = min_t(int, size, this_task.ngroups);
96 memcpy(array, this_task.groups, size * sizeof(__u32));
99 int cfs_curproc_is_in_groups(gid_t gid)
101 return in_group_p(gid);
104 mode_t cfs_curproc_umask(void)
106 return this_task.umask;
109 char *cfs_curproc_comm(void)
111 return this_task.comm;
114 void cfs_cap_raise(cfs_cap_t cap)
116 this_task.cap_effective |= (1 << cap);
119 void cfs_cap_lower(cfs_cap_t cap)
121 this_task.cap_effective &= ~(1 << cap);
124 int cfs_cap_raised(cfs_cap_t cap)
126 return this_task.cap_effective & (1 << cap);
129 cfs_cap_t cfs_curproc_cap_pack(void) {
130 return this_task.cap_effective;
133 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
134 this_task.cap_effective = cap;
137 int cfs_capable(cfs_cap_t cap)
143 * Implementation of linux task management routines
147 /* global of the task manager structure */
149 TASK_MAN cfs_win_task_manger;
151 /* global idr context */
152 struct idr_context * cfs_win_task_slot_idp = NULL;
161 PTASK_SLOT task = NULL;
163 if (cfs_win_task_manger.slab) {
164 task = cfs_mem_cache_alloc(cfs_win_task_manger.slab, 0);
166 task = cfs_alloc(sizeof(TASK_SLOT), 0);
173 init_task_slot(PTASK_SLOT task)
175 memset(task, 0, sizeof(TASK_SLOT));
176 task->Magic = TASKSLT_MAGIC;
177 task->task = this_task;
178 cfs_init_event(&task->Event, TRUE, FALSE);
182 cleanup_task_slot(PTASK_SLOT task)
184 if (task->task.pid) {
185 cfs_idr_remove(cfs_win_task_slot_idp, task->task.pid);
188 if (cfs_win_task_manger.slab) {
189 cfs_mem_cache_free(cfs_win_task_manger.slab, task);
196 * task manager related routines
206 PLIST_ENTRY ListEntry = NULL;
207 PTASK_SLOT TaskSlot = NULL;
209 cfs_spin_lock(&(cfs_win_task_manger.Lock));
211 ListEntry = cfs_win_task_manger.TaskList.Flink;
212 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
214 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
216 if (TaskSlot->Pid == ProcessId && TaskSlot->Tid == ThreadId) {
219 /* remove the taskslot */
220 RemoveEntryList(&(TaskSlot->Link));
221 cfs_win_task_manger.NumOfTasks--;
223 /* now free the task slot */
224 cleanup_task_slot(TaskSlot);
228 ListEntry = ListEntry->Flink;
231 cfs_spin_unlock(&(cfs_win_task_manger.Lock));
239 /* initialize the content and magic */
240 memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
241 cfs_win_task_manger.Magic = TASKMAN_MAGIC;
243 /* initialize the spinlock protection */
244 cfs_spin_lock_init(&cfs_win_task_manger.Lock);
246 /* create slab memory cache */
247 cfs_win_task_manger.slab = cfs_mem_cache_create(
248 "TSLT", sizeof(TASK_SLOT), 0, 0);
250 /* intialize the list header */
251 InitializeListHead(&(cfs_win_task_manger.TaskList));
253 cfs_win_task_slot_idp = cfs_idr_init();
254 if (!cfs_win_task_slot_idp) {
258 /* set the thread creation/destruction notify routine */
259 status = PsSetCreateThreadNotifyRoutine(task_manager_notify);
261 if (!NT_SUCCESS(status)) {
262 cfs_enter_debugger();
263 /* remove idr context */
264 if (cfs_win_task_slot_idp) {
265 cfs_idr_exit(cfs_win_task_slot_idp);
266 cfs_win_task_slot_idp = NULL;
268 return cfs_error_code(status);
275 cleanup_task_manager()
277 PLIST_ENTRY ListEntry = NULL;
278 PTASK_SLOT TaskSlot = NULL;
280 /* remove ThreadNotifyRoutine: task_manager_notify */
281 PsRemoveCreateThreadNotifyRoutine(task_manager_notify);
283 /* remove idr context */
284 if (cfs_win_task_slot_idp) {
285 cfs_idr_exit(cfs_win_task_slot_idp);
286 cfs_win_task_slot_idp = NULL;
289 /* cleanup all the taskslots attached to the list */
290 cfs_spin_lock(&(cfs_win_task_manger.Lock));
292 while (!IsListEmpty(&(cfs_win_task_manger.TaskList))) {
294 ListEntry = cfs_win_task_manger.TaskList.Flink;
295 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
297 RemoveEntryList(ListEntry);
298 cleanup_task_slot(TaskSlot);
301 cfs_spin_unlock(&cfs_win_task_manger.Lock);
303 /* destroy the taskslot cache slab */
304 cfs_mem_cache_destroy(cfs_win_task_manger.slab);
305 memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
310 * schedule routines (task slot list)
317 HANDLE Pid = PsGetCurrentProcessId();
318 HANDLE Tid = PsGetCurrentThreadId();
319 PETHREAD Tet = PsGetCurrentThread();
321 PLIST_ENTRY ListEntry = NULL;
322 PTASK_SLOT TaskSlot = NULL;
324 cfs_spin_lock(&(cfs_win_task_manger.Lock));
326 ListEntry = cfs_win_task_manger.TaskList.Flink;
327 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
329 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
330 if (TaskSlot->Pid == Pid && TaskSlot->Tid == Tid) {
331 if (TaskSlot->Tet != Tet) {
334 // The old thread was already exit. This must be a
335 // new thread which get the same Tid to the previous.
344 if (TaskSlot->Pid > Pid) {
347 } else if (TaskSlot->Pid == Pid) {
348 if (TaskSlot->Tid > Tid) {
356 ListEntry = ListEntry->Flink;
361 /* allocate new task slot */
362 TaskSlot = alloc_task_slot();
364 cfs_enter_debugger();
368 /* set task slot IDs */
369 init_task_slot(TaskSlot);
373 TaskSlot->task.pid = (pid_t)cfs_idr_get_new(cfs_win_task_slot_idp, Tet);
375 if (ListEntry == (&(cfs_win_task_manger.TaskList))) {
377 // Empty case or the biggest case, put it to the tail.
379 InsertTailList(&(cfs_win_task_manger.TaskList), &(TaskSlot->Link));
382 // Get a slot and smaller than it's tid, put it just before.
384 InsertHeadList(ListEntry->Blink, &(TaskSlot->Link));
387 cfs_win_task_manger.NumOfTasks++;
391 // To Check whether he task structures are arranged in the expected order ?
395 PTASK_SLOT Prev = NULL, Curr = NULL;
397 ListEntry = cfs_win_task_manger.TaskList.Flink;
399 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
401 Curr = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
402 ListEntry = ListEntry->Flink;
405 if (Prev->Pid > Curr->Pid) {
406 cfs_enter_debugger();
407 } else if (Prev->Pid == Curr->Pid) {
408 if (Prev->Tid > Curr->Tid) {
409 cfs_enter_debugger();
420 cfs_spin_unlock(&(cfs_win_task_manger.Lock));
423 cfs_enter_debugger();
427 return (&(TaskSlot->task));
430 /* deschedule for a bit... */
432 cfs_pause(cfs_duration_t ticks)
434 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, ticks);
438 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t time)
440 cfs_task_t * task = cfs_current();
441 PTASK_SLOT slot = NULL;
444 cfs_enter_debugger();
448 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
449 cfs_assert(slot->Magic == TASKSLT_MAGIC);
451 if (time == CFS_MAX_SCHEDULE_TIMEOUT) {
455 cfs_wait_event_internal(&(slot->Event), time);
461 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, 0);
469 PTASK_SLOT slot = NULL;
472 cfs_enter_debugger();
476 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
477 cfs_assert(slot->Magic == TASKSLT_MAGIC);
479 cfs_wake_event(&(slot->Event));
485 sleep_on(cfs_waitq_t *waitq)
489 cfs_waitlink_init(&link);
490 cfs_waitq_add(waitq, &link);
491 cfs_waitq_wait(&link, CFS_TASK_INTERRUPTIBLE);
492 cfs_waitq_del(waitq, &link);
495 EXPORT_SYMBOL(cfs_curproc_uid);
496 EXPORT_SYMBOL(cfs_curproc_pid);
497 EXPORT_SYMBOL(cfs_curproc_gid);
498 EXPORT_SYMBOL(cfs_curproc_fsuid);
499 EXPORT_SYMBOL(cfs_curproc_fsgid);
500 EXPORT_SYMBOL(cfs_curproc_umask);
501 EXPORT_SYMBOL(cfs_curproc_comm);
502 EXPORT_SYMBOL(cfs_curproc_groups_nr);
503 EXPORT_SYMBOL(cfs_curproc_groups_dump);
504 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
505 EXPORT_SYMBOL(cfs_cap_raise);
506 EXPORT_SYMBOL(cfs_cap_lower);
507 EXPORT_SYMBOL(cfs_cap_raised);
508 EXPORT_SYMBOL(cfs_curproc_cap_pack);
509 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
510 EXPORT_SYMBOL(cfs_capable);