Skip to content

Commit 1ffda0b

Browse files
committed
implement send_signal
1 parent 0c4b69f commit 1ffda0b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/stdlib_system.F90

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module stdlib_system
7878
public :: update
7979
public :: wait
8080
public :: kill
81+
public :: send_signal
8182
public :: elapsed
8283
public :: is_windows
8384

@@ -139,6 +140,10 @@ module stdlib_system
139140

140141
!! Kill a process
141142
procedure :: kill => process_kill
143+
144+
!! Send (POSIX) signal to a process
145+
!! no-op on Windows
146+
procedure :: send_signal => process_send_signal
142147

143148
!! Get process ID
144149
procedure :: pid => process_get_ID
@@ -408,6 +413,36 @@ module subroutine process_kill(process, success)
408413
logical, intent(out) :: success
409414
end subroutine process_kill
410415
end interface kill
416+
417+
interface send_signal
418+
!! version: experimental
419+
!!
420+
!! sends (POSIX) signal to a running process.
421+
!! ([Specification](../page/specs/stdlib_system.html#send_signal))
422+
!!
423+
!! ### Summary
424+
!! Provides a method to send POSIX signals to a running process.
425+
!! Returns a boolean flag indicating whether the operation was successful.
426+
!!
427+
!! ### Description
428+
!!
429+
!! This interface allows for the sending of signal to an external process.
430+
!! If the signal is sent successfully, the `success` output flag is set to `.true.`, otherwise `.false.`.
431+
!! This function is useful for controlling and managing processes
432+
!!
433+
!! @note This operation may be system-dependent and could fail if the underlying user does not have
434+
!! the necessary rights to kill a process. It is a no-op on Windows
435+
!!
436+
module subroutine process_send_signal(process, signal, success)
437+
!> The process object to send a signal to
438+
class(process_type), intent(inout) :: process
439+
!> The integer representation of the signal
440+
!> Example: 9 for SIGKILL
441+
integer, intent(in) :: signal
442+
!> Boolean flag indicating whether the operation was successful
443+
logical, intent(out) :: success
444+
end subroutine process_send_signal
445+
end interface send_signal
411446

412447
interface sleep
413448
!! version: experimental

src/stdlib_system_subprocess.F90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ logical(c_bool) function process_system_kill(pid) bind(C, name='process_kill')
4343
implicit none
4444
integer(process_ID), intent(in), value :: pid
4545
end function process_system_kill
46+
47+
logical(c_bool) function process_system_send_signal(pid, signal) bind(C, name='process_send_signal')
48+
import c_bool, process_ID
49+
implicit none
50+
integer(process_ID), intent(in), value :: pid
51+
integer, intent(in), value :: signal
52+
end function process_system_send_signal
4653

4754
! System implementation of a wait function
4855
subroutine process_wait(seconds) bind(C,name='process_wait')
@@ -473,6 +480,40 @@ module subroutine process_kill(process, success)
473480
end if
474481

475482
end subroutine process_kill
483+
484+
! Send POSIX signal to a process
485+
module subroutine process_send_signal(process, signal, success)
486+
class(process_type), intent(inout) :: process
487+
! Signal number
488+
integer, intent(in) :: signal
489+
! Return a boolean flag for successful operation
490+
logical, intent(out) :: success
491+
492+
integer(c_int) :: exit_code
493+
logical(c_bool) :: running
494+
495+
success = .true.
496+
497+
! No need to
498+
if (process%completed) return
499+
if (process%id == FORKED_PROCESS) return
500+
501+
success = logical(process_system_send_signal(process%id, signal))
502+
503+
if (success) then
504+
505+
call process_query_status(process%id, wait=C_FALSE, is_running=running, exit_code=exit_code)
506+
process%completed = .not.running
507+
508+
if (process%completed) then
509+
! Process completed, may have returned an error code
510+
process%exit_code = exit_code
511+
call save_completed_state(process,delete_files=.true.)
512+
end if
513+
514+
end if
515+
516+
end subroutine process_send_signal
476517

477518
subroutine save_completed_state(process,delete_files)
478519
class(process_type), intent(inout) :: process

0 commit comments

Comments
 (0)