From 7964953816e093580a312a5d99c532f83c27d144 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:44:39 +0100 Subject: [PATCH 01/16] renamed file renamed file properly to disk_partitions --- .../functions/{disk_partions.md => disk_partitions.md} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename doc/linux/functions/{disk_partions.md => disk_partitions.md} (91%) diff --git a/doc/linux/functions/disk_partions.md b/doc/linux/functions/disk_partitions.md similarity index 91% rename from doc/linux/functions/disk_partions.md rename to doc/linux/functions/disk_partitions.md index 4697d5e..91cd309 100644 --- a/doc/linux/functions/disk_partions.md +++ b/doc/linux/functions/disk_partitions.md @@ -1,6 +1,6 @@ -# disk_partions +# disk_partitions -This function returns a type DiskUsage. +This function returns a sequence of type DiskPartition. # The function ```nim @@ -41,4 +41,4 @@ proc disk_partitions*(all=false): seq[DiskPartition] = # The type -- [DiskUsage](../types/DiskUsage.md) \ No newline at end of file +- [DiskPartition](../types/DiskPartition.md) \ No newline at end of file From 100e8c8b6b7e7f7cc17ad33cd5a961de49be63cd Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:45:08 +0100 Subject: [PATCH 02/16] Create DiskPartition.md added documentation for the DiskPartition type --- doc/linux/types/DiskPartition.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 doc/linux/types/DiskPartition.md diff --git a/doc/linux/types/DiskPartition.md b/doc/linux/types/DiskPartition.md new file mode 100644 index 0000000..3a43d12 --- /dev/null +++ b/doc/linux/types/DiskPartition.md @@ -0,0 +1,19 @@ +# DiskPartition + +DiskPartition is a type that hold disk partition information + +# the type + +```nim +type DiskPartition* = object of RootObj + device*: string + mountpoint*: string + fstype*: string + opts*: string +``` + +# information +device : the device name of the partition +mountpoint : the mountpoint of the partition +fstype : the file system type of the partition +opts : options of the partition \ No newline at end of file From 1794d044f4bf838db7b307a58765015367c48d16 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:45:35 +0100 Subject: [PATCH 03/16] Update disk_partitions.md added a link to the newly created DiskParition documentation --- doc/windows/functions/disk_partitions.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/doc/windows/functions/disk_partitions.md b/doc/windows/functions/disk_partitions.md index 3a33119..f7695f8 100644 --- a/doc/windows/functions/disk_partitions.md +++ b/doc/windows/functions/disk_partitions.md @@ -69,14 +69,6 @@ proc disk_partitions*( all=false ): seq[DiskPartition] = discard SetErrorMode( 0 ) ``` -# Type DiskPartition - -```nim -type DiskPartition* = object of RootObj - device*: string - mountpoint*: string - fstype*: string - opts*: string - -``` +# The type +- [DiskPartition](../types/DiskPartition.md) From 1c7d4dab65c594d95391838a607705012ce02bda Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:45:52 +0100 Subject: [PATCH 04/16] Create DiskPartition.md added DiskPartition type documentation --- doc/windows/types/DiskPartition.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 doc/windows/types/DiskPartition.md diff --git a/doc/windows/types/DiskPartition.md b/doc/windows/types/DiskPartition.md new file mode 100644 index 0000000..3a43d12 --- /dev/null +++ b/doc/windows/types/DiskPartition.md @@ -0,0 +1,19 @@ +# DiskPartition + +DiskPartition is a type that hold disk partition information + +# the type + +```nim +type DiskPartition* = object of RootObj + device*: string + mountpoint*: string + fstype*: string + opts*: string +``` + +# information +device : the device name of the partition +mountpoint : the mountpoint of the partition +fstype : the file system type of the partition +opts : options of the partition \ No newline at end of file From bdf17f6ba3e6291b9a7264f548914d1979273d1a Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:46:16 +0100 Subject: [PATCH 05/16] Update psutil_linux.nim existsFile is deprecated, and now using fileExists instead --- src/psutil/psutil_linux.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index 35b66bd..9567021 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -644,7 +644,7 @@ proc swap_memory*(): SwapMemory = percent:percent, sin:0, sout:0 ) # try to get pgin/pgouts - if not existsFile( PROCFS_PATH / "vmstat" ): + if not fileExists( PROCFS_PATH / "vmstat" ): # see https://github.com/giampaolo/psutil/issues/722 echo( "'sin' and 'sout' swap memory stats couldn't be determined ", "and were set to 0" ) From baaab86052dfe716e0cf7afc44330dac317ff763 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:47:45 +0100 Subject: [PATCH 06/16] Update psutil_linux.nim replaced ..colon with 0..colon, because ..b is deprecated to 0..b. --- src/psutil/psutil_linux.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index 9567021..3fbfe92 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -706,7 +706,7 @@ proc per_nic_net_io_counters*(): TableRef[string, NetIO] = for line in lines( PROCFS_PATH / "net/dev" ): if not( ":" in line ): continue let colon = line.rfind(':') - let name = line[..colon].strip() + let name = line[0..colon].strip() let lst = line[(colon + 1)..len(line) - 1].strip.replace("\x00", "").splitWhitespace let fields = mapIt(lst, parseInt(it)) From c6382f0345ab1e4585b20f9faae9861d2e000d21 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:48:32 +0100 Subject: [PATCH 07/16] Update psutil_linux.nim replaced ..10 with 0..10, because ..b is deprecated to 0..b. --- src/psutil/psutil_linux.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index 3fbfe92..eeb0a3d 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -903,7 +903,7 @@ iterator process_inet( file: string, family: int, socketType: int, inodes : Orde for line in file.lines: try: - let strings = line.splitWhitespace()[..10] + let strings = line.splitWhitespace()[0..10] laddr = strings[1] raddr = strings[2] status = strings[3] From 8c366dfa96c727a8280a1f12c7c1d6d5fedeb8e6 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:49:46 +0100 Subject: [PATCH 08/16] Update psutil_posix.nim cuchar is deprecated, thus replaced with char --- src/psutil/psutil_posix.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/psutil/psutil_posix.nim b/src/psutil/psutil_posix.nim index 6878e66..921da72 100644 --- a/src/psutil/psutil_posix.nim +++ b/src/psutil/psutil_posix.nim @@ -37,9 +37,9 @@ type mem_start*: culong mem_end*: culong base_addr*: cushort - irq*: cuchar - dma*: cuchar - port*: cuchar ## # 3 bytes spare + irq*: char + dma*: char + port*: char ## # 3 bytes spare type INNER_C_UNION_9261176668105079294* = object {.union.} From bebefff68538398e962172bbbc10b50e8370b8e1 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:51:44 +0100 Subject: [PATCH 09/16] Update psutil_posix.nim implicit conversion from string to cstring, thus added the cast to avoid CStringConv compile errors in the future --- src/psutil/psutil_posix.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psutil/psutil_posix.nim b/src/psutil/psutil_posix.nim index 921da72..ed32cf1 100644 --- a/src/psutil/psutil_posix.nim +++ b/src/psutil/psutil_posix.nim @@ -156,7 +156,7 @@ proc psutil_convert_ipaddr(address: ptr SockAddr, family: posix.TSa_Family): str else: addrlen = sizeof(SockAddr_in6).uint32 - let err = getnameinfo( address, addrlen, result, resultLen, nil, 0, NI_NUMERICHOST ) + let err = getnameinfo( address, addrlen, cast[cstring](result), resultLen, nil, 0, NI_NUMERICHOST ) if err != 0: # // XXX we get here on FreeBSD when processing 'lo' / AF_INET6 # // broadcast. Not sure what to do other than returning None. From bca06e1784b5f9893c5304b00f88dc70c6c07d51 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:52:59 +0100 Subject: [PATCH 10/16] Update psutil_posix.nim type pragmas follow the type name; this form of writing pragmas is deprecated --- src/psutil/psutil_posix.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/psutil/psutil_posix.nim b/src/psutil/psutil_posix.nim index ed32cf1..8c13e41 100644 --- a/src/psutil/psutil_posix.nim +++ b/src/psutil/psutil_posix.nim @@ -42,10 +42,10 @@ type port*: char ## # 3 bytes spare type - INNER_C_UNION_9261176668105079294* = object {.union.} + INNER_C_UNION_9261176668105079294* {.union.} = object ifrn_name*: array[IFNAMSIZ, char] ## # Interface name, e.g. "en0". - INNER_C_UNION_7660000764852079517* = object {.union.} + INNER_C_UNION_7660000764852079517* {.union.} = object ifru_addr*: SockAddr ifru_dstaddr*: SockAddr ifru_broadaddr*: SockAddr From aebe70fd801066edf02c93259f3bc121708980a2 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:54:55 +0100 Subject: [PATCH 11/16] Update common.nim See corresponding Defect; DivByZeroError is deprecated, thus use DivByZeroDefect instead --- src/psutil/common.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/psutil/common.nim b/src/psutil/common.nim index 9751d24..ee1ce40 100644 --- a/src/psutil/common.nim +++ b/src/psutil/common.nim @@ -111,7 +111,7 @@ proc usage_percent*[T](used: T, total: T, places=0): float = ## Calculate percentage usage of 'used' against 'total'. try: result = (used.int / total.int) * 100 - except DivByZeroError: + except DivByZeroDefect: result = if used is float or total is float: 0.0 else: 0 if places != 0: return round(result, places) From b82180021cd2711ea97eda644d1dc351abdcbcda Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Fri, 18 Mar 2022 12:26:17 +0100 Subject: [PATCH 12/16] added pid_kill added the pid_kill function, which will send a SIGKILL to the specified PID. --- src/psutil/psutil_linux.nim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index eeb0a3d..817ef0a 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -2,7 +2,8 @@ import algorithm, math, net, os, posix, sequtils, sets, strutils, tables, times import strformat import common, psutil_posix - +from posix_utils import sendSignal +from posix import Pid, SIGKILL ################################################################################ const PROCFS_PATH = "/proc" @@ -206,6 +207,22 @@ proc pids_cmdline*(pids: seq[int]): seq[string] = for pid in pids: ret.add(pid_cmdline(pid)) +# TODO: add pid_kill function here +proc pid_kill*(pid: int) = + + if not pid_exists(pid): + raise newException(OSError, "PID " & $(pid) & " doesn't exist") + + ## Function for sending a kill signal to the specified pid + var temp: Pid = int32(pid) + + try: + sendSignal(temp, SIGKILL) + except OSError: + raise getCurrentException() + + + proc pid_name*(pid: int): string = ## Function for getting the process name of a pid ## not to be mixed with pid_cmdline. This only gets the From a6ba4b7826be9ea693ace693f1bb9139c551ab9f Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Fri, 18 Mar 2022 12:44:49 +0100 Subject: [PATCH 13/16] added pid_kill proc added pid_kill procedure for killing the specified pid. This opens the process and then terminates it. Raises an OSError for an error --- src/psutil/psutil_windows.nim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/psutil/psutil_windows.nim b/src/psutil/psutil_windows.nim index 8d254c4..7ba8dc1 100644 --- a/src/psutil/psutil_windows.nim +++ b/src/psutil/psutil_windows.nim @@ -990,4 +990,19 @@ proc pid_exists*(pid: int): bool = proc pid_cmdline*(pid: int): string = raise newException( Exception, "Function is unimplemented!") +proc pid_kill*(pid: int) = + + ## Function for killing the specified pid + ## will check if pid exists, and if not raises OSError + ## if the pid does exist, then will attempt to OpenProcess + if not pid_exists(pid): + raise newException(OSError, "PID " & $(pid) & " doesn't exist") + + var handle = OpenProcess(0x001F0FFF, FALSE, cast[DWORD](pid)) + if handle == 0: + raise newException(OSError, $(GetLastError())) + if TerminateProcess(handle, 0): + return + else: + raise newException(OSError, $(GetLastError())) \ No newline at end of file From 2a2707bcbaf8f236e69100ed03e7d4de2a6ee2a4 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Fri, 18 Mar 2022 13:00:27 +0100 Subject: [PATCH 14/16] added casts to arguments in functions Do to casting errors, i've added casts to where I was getting them. --- src/psutil/psutil_windows.nim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/psutil/psutil_windows.nim b/src/psutil/psutil_windows.nim index 7ba8dc1..c6ab945 100644 --- a/src/psutil/psutil_windows.nim +++ b/src/psutil/psutil_windows.nim @@ -148,7 +148,7 @@ proc pid_name*(processID: int): string = if EnumProcessModules( hProcess, hMod.addr, cast[DWORD](sizeof(hMod)), cbNeeded.addr): - GetModuleBaseName( hProcess, hMod, szProcessName, + GetModuleBaseName( hProcess, hMod, cast[LPWSTR](szProcessName.addr), cast[DWORD](szProcessName.len) ) else: @@ -192,7 +192,7 @@ proc pid_path*(pid: int): string = if processHandle.addr != nil or processHandle == cast[HANDLE](1) or processHandle == cast[HANDLE](NULL): - if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), filename, cast[PDWORD](dwSize.addr)) == FALSE: + if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), cast[LPSTR](filename.addr), cast[PDWORD](dwSize.addr)) == FALSE: raiseError() @@ -231,7 +231,7 @@ proc try_pid_path*(pid: int): string = if processHandle.addr != nil or processHandle == cast[HANDLE](1) or processHandle == cast[HANDLE](NULL): - if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), filename, cast[PDWORD](dwSize.addr)) == FALSE: + if QueryFullProcessImageNameA(processHandle, cast[DWORD](0), cast[LPSTR](filename.addr), cast[PDWORD](dwSize.addr)) == FALSE: result = "" @@ -355,7 +355,7 @@ proc pid_user*(pid: int): string = GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr)) - if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE: + if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE: raiseError() let user = wcUser[0..^1] @@ -409,7 +409,7 @@ proc try_pid_user*(pid: int): string = GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr)) - if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE: + if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE: return "" let user = wcUser[0..^1] @@ -462,7 +462,7 @@ proc pid_domain*(pid: int): string = GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr)) - if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE: + if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE: raiseError() let domain = wcDomain[0..^1] @@ -512,7 +512,7 @@ proc pid_domain_user*(pid: int): (string, string) = GetTokenInformation(hToken, tokenUser, pUser.addr, cast[DWORD](dwLength), cast[PDWORD](dwLength.addr)) #== FALSE: # raiseError() - if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, wcUser, dwUserLength.addr, wcDomain, dwDomainLength.addr, peUse.addr) == FALSE: + if LookupAccountSidW(cast[LPCWSTR](NULL), pUser.User.Sid, cast[LPWSTR](wcUser.addr), dwUserLength.addr, cast[LPWSTR](wcDomain.addr), dwDomainLength.addr, peUse.addr) == FALSE: raiseError() let user = wcUser[0..^1] From 892b019969b83fed05f49d777747be8038d8b501 Mon Sep 17 00:00:00 2001 From: TechnoFox <45339160+Techno-Fox@users.noreply.github.com> Date: Fri, 18 Mar 2022 13:01:08 +0100 Subject: [PATCH 15/16] added handle checks i've added some checks for OpenProcess return variable. --- src/psutil/psutil_windows.nim | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/psutil/psutil_windows.nim b/src/psutil/psutil_windows.nim index c6ab945..d662afe 100644 --- a/src/psutil/psutil_windows.nim +++ b/src/psutil/psutil_windows.nim @@ -126,6 +126,7 @@ proc pids*(): seq[int] = for i in 0.. Date: Fri, 18 Mar 2022 14:11:09 +0100 Subject: [PATCH 16/16] added pid_kill proc added the pid_kill proc to the psutil.nim file, also exporting the platform so user can access all the function without needing to do "psutil/platform_linux" or "psutil/platform_windows" --- src/psutil.nim | 8 ++++++++ src/psutil/psutil_linux.nim | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/psutil.nim b/src/psutil.nim index dc876bd..7a39633 100644 --- a/src/psutil.nim +++ b/src/psutil.nim @@ -53,6 +53,10 @@ proc pid_exists*( pid: int ): bool = else: return platform.pid_exists(pid) +proc pid_kill*(pid: int) = + ## Function for killing specified pid + ## will raise OSError on error + platform.pid_kill(pid) proc cpu_count*(logical=true): int = # Return the number of logical CPUs in the system. @@ -278,6 +282,7 @@ export boot_time export uptime export users export pids +export pid_kill export cpu_times export per_cpu_times export cpu_stats @@ -291,3 +296,6 @@ export net_if_stats export disk_io_counters export per_disk_io_counters export net_connections + +# export the platform for access to all the procedures +export platform \ No newline at end of file diff --git a/src/psutil/psutil_linux.nim b/src/psutil/psutil_linux.nim index 817ef0a..1ec7592 100644 --- a/src/psutil/psutil_linux.nim +++ b/src/psutil/psutil_linux.nim @@ -207,13 +207,11 @@ proc pids_cmdline*(pids: seq[int]): seq[string] = for pid in pids: ret.add(pid_cmdline(pid)) -# TODO: add pid_kill function here proc pid_kill*(pid: int) = - + ## Function for sending a kill signal to the specified pid if not pid_exists(pid): raise newException(OSError, "PID " & $(pid) & " doesn't exist") - ## Function for sending a kill signal to the specified pid var temp: Pid = int32(pid) try: