kernel32_windows.go 8.66 KB
Newer Older
Amos Wenger's avatar
Amos Wenger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
package syscallex

import (
	"syscall"
	"unsafe"

	"golang.org/x/sys/windows"
)

// JobObjectInfoClass
// cf. https://msdn.microsoft.com/en-us/library/windows/desktop/ms686216%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
const (
	JobObjectInfoClass_JobObjectBasicProcessIdList                 = 3
	JobObjectInfoClass_JobObjectAssociateCompletionPortInformation = 7
	JobObjectInfoClass_JobObjectExtendedLimitInformation           = 9
)

// JobObjectBasicLimitInformation.LimitFlags
const (
	JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000
)

// job object completion statuses, thanks wine!
// cf. https://www.winehq.org/pipermail/wine-cvs/2013-October/097834.html
const (
	JOB_OBJECT_MSG_END_OF_JOB_TIME       = 1
	JOB_OBJECT_MSG_END_OF_PROCESS_TIME   = 2
	JOB_OBJECT_MSG_ACTIVE_PROCESS_LIMIT  = 3
	JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO   = 4
	JOB_OBJECT_MSG_NEW_PROCESS           = 6
	JOB_OBJECT_MSG_EXIT_PROCESS          = 7
	JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS = 8
	JOB_OBJECT_MSG_PROCESS_MEMORY_LIMIT  = 9
	JOB_OBJECT_MSG_JOB_MEMORY_LIMIT      = 10
)

type JobObjectAssociateCompletionPort struct {
	CompletionKey  syscall.Handle
	CompletionPort syscall.Handle
}

const (
	CREATE_SUSPENDED      = 0x00000004
	PROCESS_ALL_ACCESS    = syscall.STANDARD_RIGHTS_REQUIRED | syscall.SYNCHRONIZE | 0xfff
	THREAD_SUSPEND_RESUME = 0x0002

	TH32CS_SNAPPROCESS = 0x00000002
)

type ThreadEntry32 struct {
	Size           uint32
	TUsage         uint32
	ThreadID       uint32
	OwnerProcessID uint32
	BasePri        int32
	DeltaPri       int32
	Flags          uint32
}

type ProcessEntry32 struct {
	Size              uint32
	CntUsage          uint32
	ProcessID         uint32
	DefaultHeapID     uintptr
	ModuleID          uint32
	CntThreads        uint32
	ParentProcessID   uint32
	PriorityClassBase int32
	Flags             uint32
	ExeFile           [MAX_PATH]uint16
}

var (
	modkernel32 = windows.NewLazySystemDLL("kernel32.dll")

	procCreateJobObject           = modkernel32.NewProc("CreateJobObjectW")
	procSetInformationJobObject   = modkernel32.NewProc("SetInformationJobObject")
	procQueryInformationJobObject = modkernel32.NewProc("QueryInformationJobObject")
	procAssignProcessToJobObject  = modkernel32.NewProc("AssignProcessToJobObject")

	procGetCurrentThread    = modkernel32.NewProc("GetCurrentThread")
	procOpenThreadToken     = modkernel32.NewProc("OpenThreadToken")
	procGetDiskFreeSpaceExW = modkernel32.NewProc("GetDiskFreeSpaceExW")

	procOpenThread    = modkernel32.NewProc("OpenThread")
	procResumeThread  = modkernel32.NewProc("ResumeThread")
	procThread32First = modkernel32.NewProc("Thread32First")
	procThread32Next  = modkernel32.NewProc("Thread32Next")

	procCreateToolhelp32Snapshot = modkernel32.NewProc("CreateToolhelp32Snapshot")
	procProcess32FirstW          = modkernel32.NewProc("Process32FirstW")
	procProcess32NextW           = modkernel32.NewProc("Process32NextW")

	procQueryFullProcessImageNameW = modkernel32.NewProc("QueryFullProcessImageNameW")
)

func CreateJobObject(
	jobAttributes *syscall.SecurityAttributes,
	name *uint16,
) (handle syscall.Handle, err error) {
	r1, _, e1 := syscall.Syscall(
		procCreateJobObject.Addr(),
		2,
		uintptr(unsafe.Pointer(jobAttributes)),
		uintptr(unsafe.Pointer(name)),
		0,
	)
	handle = syscall.Handle(r1)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

type IoCounters struct {
	ReadOperationCount  uint64
	WriteOperationCount uint64
	OtherOperationCount uint64
	ReadTransferCount   uint64
	WriteTransferCount  uint64
	OtherTransferCount  uint64
}

func SetInformationJobObject(
	jobObject syscall.Handle,
	jobObjectInfoClass uint32,
	jobObjectInfo uintptr,
	jobObjectInfoLength uintptr,
) (err error) {
	r1, _, e1 := syscall.Syscall6(
		procSetInformationJobObject.Addr(),
		4,
		uintptr(jobObject),
		uintptr(jobObjectInfoClass),
		jobObjectInfo,
		jobObjectInfoLength,
		0, 0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func QueryInformationJobObject(
	jobObject syscall.Handle,
	jobObjectInfoClass uint32,
	jobObjectInfo uintptr,
	jobObjectInfoLength uintptr,
	returnLength uintptr,
) (err error) {
	r1, _, e1 := syscall.Syscall6(
		procQueryInformationJobObject.Addr(),
		5,
		uintptr(jobObject),
		uintptr(jobObjectInfoClass),
		jobObjectInfo,
		jobObjectInfoLength,
		returnLength,
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func AssignProcessToJobObject(
	jobObject syscall.Handle,
	process syscall.Handle,
) (err error) {
	r1, _, e1 := syscall.Syscall(
		procAssignProcessToJobObject.Addr(),
		2,
		uintptr(jobObject),
		uintptr(process),
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func GetCurrentThread() syscall.Handle {
	r1, _, _ := syscall.Syscall(
		procGetCurrentThread.Addr(),
		0,
		0, 0, 0,
	)
	return syscall.Handle(r1)
}

func OpenThreadToken(
	threadHandle syscall.Handle,
	desiredAccess uint32,
	openAsSelf uint32,
	tokenHandle *syscall.Token,
) (err error) {
	r1, _, e1 := syscall.Syscall6(
		procOpenThreadToken.Addr(),
		4,
		uintptr(threadHandle),
		uintptr(desiredAccess),
		uintptr(openAsSelf),
		uintptr(unsafe.Pointer(tokenHandle)),
		0, 0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

type DiskFreeSpace struct {
	FreeBytesAvailable     uint64
	TotalNumberOfBytes     uint64
	TotalNumberOfFreeBytes uint64
}

func GetDiskFreeSpaceEx(path *uint16) (dfs *DiskFreeSpace, err error) {
	var buf DiskFreeSpace
	dfs = &buf

	r1, _, e1 := syscall.Syscall6(
		procGetDiskFreeSpaceExW.Addr(),
		4,
		uintptr(unsafe.Pointer(path)),
		uintptr(unsafe.Pointer(&buf.FreeBytesAvailable)),
		uintptr(unsafe.Pointer(&buf.TotalNumberOfBytes)),
		uintptr(unsafe.Pointer(&buf.TotalNumberOfFreeBytes)),
		0, 0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return dfs, err
}

func OpenThread(
	desiredAccess uint32,
	inheritHandle uint32,
	threadId uint32,
) (handle syscall.Handle, err error) {
	r1, _, e1 := syscall.Syscall(
		procOpenThread.Addr(),
		3,
		uintptr(desiredAccess),
		uintptr(inheritHandle),
		uintptr(threadId),
	)
	handle = syscall.Handle(r1)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func ResumeThread(
	thread syscall.Handle,
) (retCount uint32, err error) {
	r1, _, e1 := syscall.Syscall(
		procResumeThread.Addr(),
		1,
		uintptr(thread),
		0,
		0,
	)

	minusOne := int(-1)
	if r1 == uintptr(minusOne) {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	} else {
		retCount = uint32(r1)
	}
	return
}

func Thread32First(
	snapshot syscall.Handle,
	pThreadEntry *ThreadEntry32,
) (err error) {
	r1, _, e1 := syscall.Syscall(
		procThread32First.Addr(),
		2,
		uintptr(snapshot),
		uintptr(unsafe.Pointer(pThreadEntry)),
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func Thread32Next(
	snapshot syscall.Handle,
	pThreadEntry *ThreadEntry32,
) (err error) {
	r1, _, e1 := syscall.Syscall(
		procThread32Next.Addr(),
		2,
		uintptr(snapshot),
		uintptr(unsafe.Pointer(pThreadEntry)),
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func CreateToolhelp32Snapshot(
	flags uint32,
	processID uint32,
) (handle syscall.Handle, err error) {
	r1, _, e1 := syscall.Syscall(
		procCreateToolhelp32Snapshot.Addr(),
		2,
		uintptr(flags),
		uintptr(processID),
		0,
	)
	handle = syscall.Handle(r1)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func Process32First(
	snapshot syscall.Handle,
	pProcessEntry *ProcessEntry32,
) (err error) {
	r1, _, e1 := syscall.Syscall(
		procProcess32FirstW.Addr(),
		2,
		uintptr(snapshot),
		uintptr(unsafe.Pointer(pProcessEntry)),
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func Process32Next(
	snapshot syscall.Handle,
	pProcessEntry *ProcessEntry32,
) (err error) {
	r1, _, e1 := syscall.Syscall(
		procProcess32NextW.Addr(),
		2,
		uintptr(snapshot),
		uintptr(unsafe.Pointer(pProcessEntry)),
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	return
}

func QueryFullProcessImageName(
	process syscall.Handle,
	flags uint32,
) (s string, err error) {
	var bufferSize uint32 = 32 * 1024
	buffer := make([]uint16, bufferSize)

	r1, _, e1 := syscall.Syscall6(
		procQueryFullProcessImageNameW.Addr(),
		4,
		uintptr(process),
		uintptr(flags),
		uintptr(unsafe.Pointer(&buffer[0])),
		uintptr(unsafe.Pointer(&bufferSize)),
		0,
		0,
	)
	if r1 == 0 {
		if e1 != 0 {
			err = e1
		} else {
			err = syscall.EINVAL
		}
	}
	if err == nil {
		s = syscall.UTF16ToString(buffer[:bufferSize])
	}
	return
}