⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nt.c

📁 该项目主要是将wingdows程序直接运行在linux上
💻 C
字号:
/* * nt.c * * Copyright (C) 2006  Insigme Co., Ltd * * Authors: * - Liwei Zhou * * This software has been developed while working on the Linux Unified Kernel * project (http://linux.insigma.com.cn) in the Insigma Reaserch Institute, * which is a subdivision of Insigma Co., Ltd (http://www.insigma.com.cn). * * The project is sponsored by Insigma Co., Ltd. * * The authors can be reached at linux@insigma.com.cn. * * This program is free software; you can redistribute it and/or modify it * under the terms of  the GNU General  Public License as published by the * Free Software Foundation; either version 2 of the  License, or (at your * option) any later version. * * Revision History: *   Jan 2006 - Created. *//* * nt.c: other processes handlings * Reference to ReactOS code */#include "mm/attach.h"#include "thread.h"#include "process.h"#include "object.h"#include "w32syscall.h"#include <asm/page.h>#include <linux/mm.h>#include <linux/mman.h>#include <linux/slab.h>#include <asm/uaccess.h>#include <asm/string.h>#include <linux/personality.h>#ifdef CONFIG_UNIFIED_KERNELextern inline LONG __stdcall InterlockedExchange( LONG volatile *dest, LONG val ){	LONG ret;	__asm__ __volatile__( "lock; xchgl %0,(%1)"			: "=r" (ret) :"r" (dest), "0" (val) : "memory" );	return ret;}/* Get the information of the process */NTSTATUS STDCALLNtQueryInformationProcess(IN  HANDLE ProcessHandle,			IN  PROCESSINFOCLASS ProcessInformationClass,			OUT PVOID ProcessInformation,			IN  ULONG ProcessInformationLength,			OUT PULONG ReturnLength  OPTIONAL){	struct eprocess	*process;	struct ethread	*thread;	struct win32_object	*obj;	ULONG	length;	NTSTATUS status = STATUS_SUCCESS;	ktrace("NtQueryInformationProcess\n");	thread = thread_find();	if (!thread)		return -EINVAL;	if (ProcessHandle==NtCurrentProcess()||!ProcessHandle)		process = thread->threads_process;	else {		etget(thread);		obj = GetObject(thread, ProcessHandle,&process_objclass);		etput(thread);		if (IS_ERR(obj))			return PTR_ERR(obj);		process = obj->o_private;	}	switch (ProcessInformationClass) {		case ProcessBasicInformation:			if (ProcessInformationLength < sizeof(PROCESS_BASIC_INFORMATION))				return -EFAULT;			else {				PROCESS_BASIC_INFORMATION basic_info;				basic_info.ExitStatus = process->exit_status;				basic_info.PebBaseAddress = (DWORD)process->peb;				basic_info.AffinityMask = process->pcb.affinity;				basic_info.UniqueProcessId = (ULONG)process->unique_processid;				basic_info.InheritedFromUniqueProcessId = 0;				basic_info.BasePriority = process->pcb.base_priority;							length = sizeof(PROCESS_BASIC_INFORMATION);				if ((ULONG)ProcessInformation < TASK_SIZE) {					if (copy_to_user(ProcessInformation, &basic_info, length)) {						kdebug("copy infromation to user error\n");						return -EFAULT;					}				}				else *(PROCESS_BASIC_INFORMATION *)ProcessInformation = basic_info;			}			break;		default:			ktrace("Unimplemented information class\n");			return -EFAULT;	}	if (ReturnLength) {		if ((ULONG)ReturnLength < TASK_SIZE) {			if (copy_to_user(ReturnLength, &length, sizeof(ULONG))) {					kdebug("copy length\n");					return -EFAULT;			}		}		else *ReturnLength = length;	}	return status;} /* end NtQueryInformationProcess *//* Set the information of the process */NTSTATUS STDCALLNtSetInformationProcess(IN HANDLE ProcessHandle,			IN PROCESSINFOCLASS ProcessInformationClass,			IN PVOID ProcessInformation,			IN ULONG ProcessInformationLength){	struct eprocess	*process;	struct ethread	*thread;	struct win32_object	*obj;	NTSTATUS status = STATUS_SUCCESS;	ktrace("NtSetInformationProcess\n");	thread = thread_find();	if (!thread)		return -EINVAL;	if (ProcessHandle==NtCurrentProcess()||!ProcessHandle)		process = thread->threads_process;	else {		etget(thread);		obj = GetObject(thread, ProcessHandle,&process_objclass);		etput(thread);		if (IS_ERR(obj))			return PTR_ERR(obj);		process = obj->o_private;	}	switch (ProcessInformationClass) {		case ProcessPriorityClass:			if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))				return -EFAULT;			else {				PROCESS_PRIORITY_CLASS ppc;				if ((ULONG)ProcessInformation < TASK_SIZE) {					if (copy_from_user(&ppc, ProcessInformation, sizeof(PROCESS_PRIORITY_CLASS)))						return -EFAULT;				}				else					ppc = *(PPROCESS_PRIORITY_CLASS)ProcessInformation;								process->priority_class = ppc.PriorityClass;			}			break;		case ProcessAffinityMask:			if (ProcessInformationLength != sizeof(DWORD_PTR))				return -EFAULT;			else {				DWORD_PTR affinity;				if ((ULONG)ProcessInformation < TASK_SIZE) {					if (copy_from_user(&affinity, ProcessInformation, sizeof(DWORD_PTR)))						return -EFAULT;				}				else					affinity = *(PDWORD_PTR)ProcessInformation;				if (affinity != 1)					return -EFAULT;				else					process->pcb.affinity = affinity;				}			break;		case ProcessDefaultHardErrorMode:			if (ProcessInformationLength != sizeof(LONG))				return -EFAULT;			else {				LONG error;				if ((ULONG)ProcessInformation < TASK_SIZE) {					if (copy_from_user(&error, ProcessInformation, sizeof(LONG)))						return -EFAULT;				}				else					error = *(PLONG)ProcessInformation;				InterlockedExchange((LONG*)&process->def_hard_error_processing, error);			}			break;		case ProcessSessionInformation:			if (ProcessInformationLength != sizeof(PROCESS_SESSION_INFORMATION))				return -EFAULT;			else {				PROCESS_SESSION_INFORMATION session;				if ((ULONG)ProcessInformation < TASK_SIZE) {					if (copy_from_user(&session, ProcessInformation,							sizeof(PROCESS_SESSION_INFORMATION)))						return -EFAULT;				}				else					session = *(PPROCESS_SESSION_INFORMATION)ProcessInformation;				process->session = session.SessionId;			}		default:			ktrace("Unimplemented information class\n");			return -EFAULT;	}	return status;} /* end NtSetInformationProcess *//* Copy a handle from one process space to anther */NTSTATUS STDCALLNtDuplicateObject (IN	HANDLE SourceProcessHandle,		   IN	HANDLE SourceHandle,		   IN	HANDLE TargetProcessHandle,		   OUT	PHANDLE	 TargetHandle  OPTIONAL,		   IN	ACCESS_MASK DesiredAccess,		   IN	ULONG InheritHandle,		   IN	ULONG Options){	struct eprocess	*source_process, *target_process;	struct ethread	*thread;	win32_object	*source_proc_obj, *target_proc_obj;	win32_object	*source_obj;	win32_object	**ppobj, **epobj;	HANDLE		target_handle = NULL;	NTSTATUS status = STATUS_SUCCESS;	ktrace("NtDuplicateObject\n");	thread = thread_find();	if (!thread)		return -EINVAL;	/* get the source and target process */	etget(thread);	source_proc_obj = GetObject(thread, SourceProcessHandle, &process_objclass);	target_proc_obj = GetObject(thread, TargetProcessHandle, &process_objclass);	etput(thread);	if (IS_ERR(source_proc_obj))		return PTR_ERR(source_proc_obj);	if (IS_ERR(target_proc_obj))		return PTR_ERR(target_proc_obj);	source_process = source_proc_obj->o_private;	target_process = target_proc_obj->o_private;	/* get the source object */	read_lock(&source_process->ep_lock);	source_obj = *(win32_object**) ((char*)source_process->ep_handles +			(int)SourceHandle - sizeof(win32_object*));	read_unlock(&source_process->ep_lock);	if (!Options || Options & DUPLICATE_SAME_ACCESS) {		/* find a handle slot */		epobj = &target_process->ep_handles[MAXHANDLES];		write_lock(&target_process->ep_lock);		for (ppobj=target_process->ep_handles; ppobj<epobj; ppobj++) {			if (*ppobj == source_obj) {				ppobj++;				target_handle = (HANDLE) ((char*)ppobj - (char*)target_process->ep_handles);				break;			}		}		if (!target_process) {			for (ppobj=target_process->ep_handles; ppobj<epobj; ppobj++) {				if (!*ppobj) {					objget(source_obj);					*ppobj = source_obj;					ppobj++; /* don't use the NULL handle */					target_handle = (HANDLE) ((char*)ppobj - (char*)target_process->ep_handles);					write_unlock(&target_process->ep_lock);					goto find_obj;				}			}			write_unlock(&target_process->ep_lock);			objput(source_obj);			return -EFAULT;		}	}find_obj:	if (Options & DUPLICATE_CLOSE_SOURCE) {		write_lock(&source_process->ep_lock);		source_process->ep_handles[(int)SourceHandle - sizeof(win32_object*)] = NULL;		write_unlock(&source_process->ep_lock);		objput(source_obj);	}	if (TargetHandle) {		if ((ULONG)TargetHandle < TASK_SIZE) {			if (copy_to_user(TargetHandle, &target_handle, sizeof(HANDLE)))				return -EFAULT;		}		else *TargetHandle = target_handle;	}	return status;} /* end NtDuplicateObject */#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -