Skip to content

Commit

Permalink
Fix SIGINT handling, don't use lsof
Browse files Browse the repository at this point in the history
  • Loading branch information
olsen232 committed Jun 16, 2023
1 parent ef749f2 commit 804946f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
26 changes: 7 additions & 19 deletions cli_helper/kart.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ int is_helper_enabled()
/**
* @brief Exit signal handler for SIGALRM
*/
void exit_on_sigalrm(int sig)
void exit_on_alarm(int sig)
{
int semval = semctl(semid, SEMNUM, GETVAL);
if (semval < 0)
Expand All @@ -209,21 +209,6 @@ void exit_on_sigalrm(int sig)
exit(exit_code);
}

char *socket_filename;

/**
* @brief Exit signal handler for SIGINT
*/
void exit_on_sigint(int sig)
{
putchar('\n');
const char* kill_cmd_template = "lsof -t '%s' | xargs kill";
char* kill_cmd = malloc(strlen(kill_cmd_template) + strlen(socket_filename) + 2);
sprintf(kill_cmd, kill_cmd_template, socket_filename);
system(kill_cmd);
exit(128 + sig);
}

int main(int argc, char **argv, char **environ)
{
char cmd_path[PATH_MAX];
Expand All @@ -236,6 +221,10 @@ int main(int argc, char **argv, char **environ)
{
debug("enabled %s, pid=%d\n", cmd_path, getpid());

// Make this process the leader of a process group:
// The procress-group ID (pgid) will be the same as the pid.
setpgrp();

// start or use an existing helper process
char **env_ptr;

Expand Down Expand Up @@ -282,7 +271,7 @@ int main(int argc, char **argv, char **environ)
int fp = open(getcwd(NULL, 0), O_RDONLY);
int fds[4] = {fileno(stdin), fileno(stdout), fileno(stderr), fp};

socket_filename = malloc(strlen(getenv("HOME")) + strlen(".kart.socket") + 2);
char *socket_filename = malloc(strlen(getenv("HOME")) + strlen(".kart.socket") + 2);
sprintf(socket_filename, "%s/%s", getenv("HOME"), ".kart.socket");
int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);

Expand Down Expand Up @@ -385,8 +374,7 @@ int main(int argc, char **argv, char **environ)
memcpy((int *)CMSG_DATA(cmsg), fds, sizeof(fds));
msg.msg_controllen = cmsg->cmsg_len;

signal(SIGALRM, exit_on_sigalrm);
signal(SIGINT, exit_on_sigint);
signal(SIGALRM, exit_on_alarm);

if (sendmsg(socket_fd, &msg, 0) < 0)
{
Expand Down
10 changes: 10 additions & 0 deletions kart/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def helper(ctx, socket_filename, timeout, args):
else:
# child
_helper_log("post-fork")

payload, fds = recv_json_and_fds(client, maxfds=4)
if not payload or len(fds) != 4:
click.echo(
Expand Down Expand Up @@ -169,6 +170,15 @@ def helper(ctx, socket_filename, timeout, args):
f"Payload:\n{repr(payload)}",
)
else:
try:
# Join the process group of the calling process - so that if they get killed, we get killed to.
os.setpgid(0, calling_environment["pid"])
except Exception as e:
# Kart will still work even if this fails: it just means SIGINT Ctrl+C might not work properly.
# We'll just log it and hope for the best.
_helper_log(f"error joining caller's process group: {e}")
pass

sys.argv[1:] = calling_environment["argv"][1:]
_helper_log(f"cmd={' '.join(calling_environment['argv'])}")
os.environ.clear()
Expand Down

0 comments on commit 804946f

Please sign in to comment.