www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Spawning a process, then killing it on SIGINT

reply aliak <something something.com> writes:
Is there a way to go about killing a process after spawning it on 
a SIGINT?

I can't do this for e.g. because kill is not  nogc.

Pid currentSpawnedPid;
extern(C) void killCurrentPidHandler(int sig) nothrow  nogc 
 system {
   kill(currentSpawnedPid, sig);
}

int main() {
   currentSpawnedPid = spawnProcess(["docker-compose", "up"], 
stdin, stdout, stderr);
   signal(SIGINT, &killCurrentPidHandler);
   return wait(currentSpawnedPid);
}

Any other ways to go about this?

Cheers,
- Ali
Nov 23 2019
next sibling parent reply mipri <mipri minimaltype.com> writes:
On Saturday, 23 November 2019 at 09:54:48 UTC, aliak wrote:
 Is there a way to go about killing a process after spawning it 
 on a SIGINT?

 I can't do this for e.g. because kill is not  nogc.
Well, this works: import std; import core.stdc.signal; extern(C) int kill(int pid, int sig) nothrow nogc system; int currentSpawnedPid; extern(C) void killCurrentPidHandler(int sig) nothrow nogc system { if (currentSpawnedPid > 1) kill(currentSpawnedPid, sig); } int main() { auto pid = spawnProcess(["sleep", "50s"], stdin, stdout, stderr); currentSpawnedPid = pid.processID; signal(SIGINT, &killCurrentPidHandler); return wait(pid); }
Nov 23 2019
parent aliak <something something.com> writes:
On Saturday, 23 November 2019 at 10:09:51 UTC, mipri wrote:
 On Saturday, 23 November 2019 at 09:54:48 UTC, aliak wrote:
 Is there a way to go about killing a process after spawning it 
 on a SIGINT?

 I can't do this for e.g. because kill is not  nogc.
Well, this works: import std; import core.stdc.signal; extern(C) int kill(int pid, int sig) nothrow nogc system; int currentSpawnedPid; extern(C) void killCurrentPidHandler(int sig) nothrow nogc system { if (currentSpawnedPid > 1) kill(currentSpawnedPid, sig); } int main() { auto pid = spawnProcess(["sleep", "50s"], stdin, stdout, stderr); currentSpawnedPid = pid.processID; signal(SIGINT, &killCurrentPidHandler); return wait(pid); }
Thanks, looks like I'll have to go that route.
Nov 24 2019
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 11/23/19 4:54 AM, aliak wrote:
 Is there a way to go about killing a process after spawning it on a SIGINT?
 
 I can't do this for e.g. because kill is not  nogc.
 
 Pid currentSpawnedPid;
 extern(C) void killCurrentPidHandler(int sig) nothrow  nogc  system {
    kill(currentSpawnedPid, sig);
 }
 
 int main() {
    currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, 
 stdout, stderr);
    signal(SIGINT, &killCurrentPidHandler);
    return wait(currentSpawnedPid);
 }
 
 Any other ways to go about this?
Yeah, fix phobos. kill should be nogc/nothrow, and probably safe. -Steve
Nov 23 2019
parent reply aliak <something something.com> writes:
On Saturday, 23 November 2019 at 12:19:27 UTC, Steven 
Schveighoffer wrote:
 On 11/23/19 4:54 AM, aliak wrote:
 Is there a way to go about killing a process after spawning it 
 on a SIGINT?
 
 I can't do this for e.g. because kill is not  nogc.
 
 Pid currentSpawnedPid;
 extern(C) void killCurrentPidHandler(int sig) nothrow  nogc 
  system {
    kill(currentSpawnedPid, sig);
 }
 
 int main() {
    currentSpawnedPid = spawnProcess(["docker-compose", "up"], 
 stdin, stdout, stderr);
    signal(SIGINT, &killCurrentPidHandler);
    return wait(currentSpawnedPid);
 }
 
 Any other ways to go about this?
Yeah, fix phobos. kill should be nogc/nothrow, and probably safe. -Steve
Looked in to it, seems step one is getting phobos compiling with dip1008 :/ Kill uses enforce.
Nov 24 2019
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 11/24/19 10:36 AM, aliak wrote:
 On Saturday, 23 November 2019 at 12:19:27 UTC, Steven Schveighoffer wrote:
 On 11/23/19 4:54 AM, aliak wrote:
 Is there a way to go about killing a process after spawning it on a 
 SIGINT?

 I can't do this for e.g. because kill is not  nogc.

 Pid currentSpawnedPid;
 extern(C) void killCurrentPidHandler(int sig) nothrow  nogc  system {
    kill(currentSpawnedPid, sig);
 }

 int main() {
    currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, 
 stdout, stderr);
    signal(SIGINT, &killCurrentPidHandler);
    return wait(currentSpawnedPid);
 }

 Any other ways to go about this?
Yeah, fix phobos. kill should be nogc/nothrow, and probably safe.
Looked in to it, seems step one is getting phobos compiling with dip1008 :/ Kill uses enforce.
Oof, yeah. I saw the short kill function and thought it was simply calling the kill syscall, but it's calling something more complex. Error handling in general in Phobos is somewhat of a mixed bag. Would be great to have a clearly defined error handling scheme, or even one that's configurable. Or as you said, make dip1008 mandatory ;) -Steve
Nov 24 2019