4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/winnt/winnt-curproc.c
36 * Impletion of winnt curproc routines.
39 #define DEBUG_SUBSYSTEM S_LNET
41 #include <libcfs/libcfs.h>
44 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
48 cfs_task_t this_task =
49 { /* umask */ 0,/* blocked*/0, /* pid */ 0, /* pgrp */ 0,
50 /* uid,euid,suid,fsuid */ 0, 0, 0, 0,
51 /* gid_t gid,egid,sgid,fsgid */ 0, 0, 0, 0,
52 /* ngroups*/ 1, /*cgroups*/ 0, /*groups*/ 0,
53 /* group_info */ NULL,
54 /* cap_effective, cap_inheritable, cap_permitted */ 0, 0, 0,
56 /* journal_info */ NULL
60 uid_t cfs_curproc_uid(void)
65 gid_t cfs_curproc_gid(void)
70 uid_t cfs_curproc_fsuid(void)
72 return this_task.fsuid;
75 gid_t cfs_curproc_fsgid(void)
77 return this_task.fsgid;
80 pid_t cfs_curproc_pid(void)
82 return cfs_current()->pid;
85 int cfs_curproc_groups_nr(void)
87 return this_task.ngroups;
90 void cfs_curproc_groups_dump(gid_t *array, int size)
92 LASSERT(size <= NGROUPS);
93 size = min_t(int, size, this_task.ngroups);
94 memcpy(array, this_task.groups, size * sizeof(__u32));
97 int cfs_curproc_is_in_groups(gid_t gid)
99 return in_group_p(gid);
102 mode_t cfs_curproc_umask(void)
104 return this_task.umask;
107 char *cfs_curproc_comm(void)
109 return this_task.comm;
112 void cfs_cap_raise(cfs_cap_t cap)
114 this_task.cap_effective |= (1 << cap);
117 void cfs_cap_lower(cfs_cap_t cap)
119 this_task.cap_effective &= ~(1 << cap);
122 int cfs_cap_raised(cfs_cap_t cap)
124 return this_task.cap_effective & (1 << cap);
127 cfs_cap_t cfs_curproc_cap_pack(void) {
128 return this_task.cap_effective;
131 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
132 this_task.cap_effective = cap;
135 int cfs_capable(cfs_cap_t cap)
141 * Implementation of linux task management routines
145 /* global of the task manager structure */
147 TASK_MAN cfs_win_task_manger;
149 /* global idr context */
150 struct idr_context * cfs_win_task_slot_idp = NULL;
159 PTASK_SLOT task = NULL;
161 if (cfs_win_task_manger.slab) {
162 task = cfs_mem_cache_alloc(cfs_win_task_manger.slab, 0);
164 task = cfs_alloc(sizeof(TASK_SLOT), 0);
171 init_task_slot(PTASK_SLOT task)
173 memset(task, 0, sizeof(TASK_SLOT));
174 task->Magic = TASKSLT_MAGIC;
175 task->task = this_task;
176 cfs_init_event(&task->Event, TRUE, FALSE);
180 cleanup_task_slot(PTASK_SLOT task)
182 if (task->task.pid) {
183 cfs_idr_remove(cfs_win_task_slot_idp, task->task.pid);
186 if (cfs_win_task_manger.slab) {
187 cfs_mem_cache_free(cfs_win_task_manger.slab, task);
194 * task manager related routines
204 PLIST_ENTRY ListEntry = NULL;
205 PTASK_SLOT TaskSlot = NULL;
207 cfs_spin_lock(&(cfs_win_task_manger.Lock));
209 ListEntry = cfs_win_task_manger.TaskList.Flink;
210 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
212 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
214 if (TaskSlot->Pid == ProcessId && TaskSlot->Tid == ThreadId) {
217 /* remove the taskslot */
218 RemoveEntryList(&(TaskSlot->Link));
219 cfs_win_task_manger.NumOfTasks--;
221 /* now free the task slot */
222 cleanup_task_slot(TaskSlot);
226 ListEntry = ListEntry->Flink;
229 cfs_spin_unlock(&(cfs_win_task_manger.Lock));
237 /* initialize the content and magic */
238 memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
239 cfs_win_task_manger.Magic = TASKMAN_MAGIC;
241 /* initialize the spinlock protection */
242 cfs_spin_lock_init(&cfs_win_task_manger.Lock);
244 /* create slab memory cache */
245 cfs_win_task_manger.slab = cfs_mem_cache_create(
246 "TSLT", sizeof(TASK_SLOT), 0, 0);
248 /* intialize the list header */
249 InitializeListHead(&(cfs_win_task_manger.TaskList));
251 cfs_win_task_slot_idp = cfs_idr_init();
252 if (!cfs_win_task_slot_idp) {
256 /* set the thread creation/destruction notify routine */
257 status = PsSetCreateThreadNotifyRoutine(task_manager_notify);
259 if (!NT_SUCCESS(status)) {
260 cfs_enter_debugger();
261 /* remove idr context */
262 if (cfs_win_task_slot_idp) {
263 cfs_idr_exit(cfs_win_task_slot_idp);
264 cfs_win_task_slot_idp = NULL;
266 return cfs_error_code(status);
273 cleanup_task_manager()
275 PLIST_ENTRY ListEntry = NULL;
276 PTASK_SLOT TaskSlot = NULL;
278 /* remove ThreadNotifyRoutine: task_manager_notify */
279 PsRemoveCreateThreadNotifyRoutine(task_manager_notify);
281 /* remove idr context */
282 if (cfs_win_task_slot_idp) {
283 cfs_idr_exit(cfs_win_task_slot_idp);
284 cfs_win_task_slot_idp = NULL;
287 /* cleanup all the taskslots attached to the list */
288 cfs_spin_lock(&(cfs_win_task_manger.Lock));
290 while (!IsListEmpty(&(cfs_win_task_manger.TaskList))) {
292 ListEntry = cfs_win_task_manger.TaskList.Flink;
293 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
295 RemoveEntryList(ListEntry);
296 cleanup_task_slot(TaskSlot);
299 cfs_spin_unlock(&cfs_win_task_manger.Lock);
301 /* destroy the taskslot cache slab */
302 cfs_mem_cache_destroy(cfs_win_task_manger.slab);
303 memset(&cfs_win_task_manger, 0, sizeof(TASK_MAN));
308 * schedule routines (task slot list)
315 HANDLE Pid = PsGetCurrentProcessId();
316 HANDLE Tid = PsGetCurrentThreadId();
317 PETHREAD Tet = PsGetCurrentThread();
319 PLIST_ENTRY ListEntry = NULL;
320 PTASK_SLOT TaskSlot = NULL;
322 cfs_spin_lock(&(cfs_win_task_manger.Lock));
324 ListEntry = cfs_win_task_manger.TaskList.Flink;
325 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
327 TaskSlot = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
328 if (TaskSlot->Pid == Pid && TaskSlot->Tid == Tid) {
329 if (TaskSlot->Tet != Tet) {
332 // The old thread was already exit. This must be a
333 // new thread which get the same Tid to the previous.
342 if (TaskSlot->Pid > Pid) {
345 } else if (TaskSlot->Pid == Pid) {
346 if (TaskSlot->Tid > Tid) {
354 ListEntry = ListEntry->Flink;
359 /* allocate new task slot */
360 TaskSlot = alloc_task_slot();
362 cfs_enter_debugger();
366 /* set task slot IDs */
367 init_task_slot(TaskSlot);
371 TaskSlot->task.pid = (pid_t)cfs_idr_get_new(cfs_win_task_slot_idp, Tet);
373 if (ListEntry == (&(cfs_win_task_manger.TaskList))) {
375 // Empty case or the biggest case, put it to the tail.
377 InsertTailList(&(cfs_win_task_manger.TaskList), &(TaskSlot->Link));
380 // Get a slot and smaller than it's tid, put it just before.
382 InsertHeadList(ListEntry->Blink, &(TaskSlot->Link));
385 cfs_win_task_manger.NumOfTasks++;
389 // To Check whether he task structures are arranged in the expected order ?
393 PTASK_SLOT Prev = NULL, Curr = NULL;
395 ListEntry = cfs_win_task_manger.TaskList.Flink;
397 while (ListEntry != (&(cfs_win_task_manger.TaskList))) {
399 Curr = CONTAINING_RECORD(ListEntry, TASK_SLOT, Link);
400 ListEntry = ListEntry->Flink;
403 if (Prev->Pid > Curr->Pid) {
404 cfs_enter_debugger();
405 } else if (Prev->Pid == Curr->Pid) {
406 if (Prev->Tid > Curr->Tid) {
407 cfs_enter_debugger();
418 cfs_spin_unlock(&(cfs_win_task_manger.Lock));
421 cfs_enter_debugger();
425 return (&(TaskSlot->task));
428 /* deschedule for a bit... */
430 cfs_pause(cfs_duration_t ticks)
432 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, ticks);
436 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t time)
438 cfs_task_t * task = cfs_current();
439 PTASK_SLOT slot = NULL;
442 cfs_enter_debugger();
446 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
447 cfs_assert(slot->Magic == TASKSLT_MAGIC);
449 if (time == CFS_MAX_SCHEDULE_TIMEOUT) {
453 cfs_wait_event_internal(&(slot->Event), time);
459 cfs_schedule_timeout_and_set_state(CFS_TASK_UNINTERRUPTIBLE, 0);
467 PTASK_SLOT slot = NULL;
470 cfs_enter_debugger();
474 slot = CONTAINING_RECORD(task, TASK_SLOT, task);
475 cfs_assert(slot->Magic == TASKSLT_MAGIC);
477 cfs_wake_event(&(slot->Event));
483 sleep_on(cfs_waitq_t *waitq)
487 cfs_waitlink_init(&link);
488 cfs_waitq_add(waitq, &link);
489 cfs_waitq_wait(&link, CFS_TASK_INTERRUPTIBLE);
490 cfs_waitq_del(waitq, &link);
493 EXPORT_SYMBOL(cfs_curproc_uid);
494 EXPORT_SYMBOL(cfs_curproc_pid);
495 EXPORT_SYMBOL(cfs_curproc_gid);
496 EXPORT_SYMBOL(cfs_curproc_fsuid);
497 EXPORT_SYMBOL(cfs_curproc_fsgid);
498 EXPORT_SYMBOL(cfs_curproc_umask);
499 EXPORT_SYMBOL(cfs_curproc_comm);
500 EXPORT_SYMBOL(cfs_curproc_groups_nr);
501 EXPORT_SYMBOL(cfs_curproc_groups_dump);
502 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
503 EXPORT_SYMBOL(cfs_cap_raise);
504 EXPORT_SYMBOL(cfs_cap_lower);
505 EXPORT_SYMBOL(cfs_cap_raised);
506 EXPORT_SYMBOL(cfs_curproc_cap_pack);
507 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
508 EXPORT_SYMBOL(cfs_capable);