From: manish.r Date: Fri, 27 Nov 2020 12:50:53 +0000 (+0530) Subject: Add shell kill command X-Git-Tag: submit/tizen_6.0/20201211.061124^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=55fcc323ab959717816e60b48e38b53ce324341b;p=sdk%2Ftarget%2Fsdbd.git Add shell kill command Change-Id: Iaeea193a68fb6ae7c023c4f88b55332471e8d93f Signed-off-by: manish.r (cherry picked from commit 2009505f8ee1cd0fe46012ec9fa54c0dc6b03f67) --- diff --git a/src/services.c b/src/services.c index f26d0ae..e0cec14 100755 --- a/src/services.c +++ b/src/services.c @@ -46,7 +46,9 @@ #include #include - +#include +#include +#include #include "sdbd_plugin.h" #include "plugin.h" @@ -553,11 +555,22 @@ int create_userprocess(const char *cmd, pid_t *pid, char * const argv[], char * while (connect(sock, (struct sockaddr *)&addr, slen) == -1 && trycnt < 300) { D("try to connect socket %s, %d times.\n", sockpath, trycnt++); - /* sleep maximum 100 times */ + /* sleep maximum 300 times */ usleep(10000); } if (trycnt == 300) { E("failed to connect, errno: %d\n", errno); + D("kill main shell process %d\n",*pid); + int kill_status = kill(*pid,SIGTERM); + int status; + D("Kill status:%d",kill_status); + if(kill_status!=-1) { + D("shell process pid %d killed with wait return value %d\n",*pid,waitpid(*pid, &status, 0)); + } + else + { + E("Kill failed with erro number:%d\n",errno); + } if (sdb_close(sock) == -1) { E("close sock error, %d\n", errno); } diff --git a/src/subprocess.c b/src/subprocess.c index 7ad8529..aac1f2b 100755 --- a/src/subprocess.c +++ b/src/subprocess.c @@ -23,11 +23,24 @@ #include #include #include - +#include #include "sysdeps.h" +#define LOG_TAG "SDBD_USER_SERVICE" +#include "log.h" #include "sdb.h" #define SHELL_COMMAND "/bin/sh" +int child_pid = 0; +void handle_sig_term(int sig) { + D("sdbu signal handler called\n"); + if(child_pid > 0) + { + D("sdbu killing child with PID:%d\n",child_pid); + kill(child_pid,SIGKILL); + } + exit(0); +} + /* to send ptm fd to sdbd main */ static ssize_t send_fd(int fd, void *ptr, size_t nbytes, int sendfd) { @@ -85,16 +98,23 @@ int main (int argc, char **argv, char **envp) ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); if(ptm < 0){ fprintf(stderr, "sdbu cannot open /dev/ptmx, errno: %d\n",errno); + E("sdbu cannot open /dev/ptmx, errno: %d\n",errno); return ret; } if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0 ){ fprintf(stderr, "sdbu trouble with /dev/ptmx, errno: %d\n", errno); + E("sdbu trouble with /dev/ptmx, errno: %d\n", errno); sdb_close(ptm); return ret; } fprintf(stderr, "sdbu ptm opening success\n"); + D("sdbu ptm opening success\n"); + + /* Handle the SIGTERM Signal generated in case the process takes a long time to start*/ + signal(SIGTERM, handle_sig_term); + D("sdbu signal handler set with, errno: %d\n", errno); pid = fork(); if(pid < 0) { @@ -103,17 +123,24 @@ int main (int argc, char **argv, char **envp) return ret; } + /* sdbd-user's child. This just open pts and exec sh */ if (pid == 0) { + D("sdbu in server process\n"); + //usleep(500*10000); + //sD("after sllep\n"); + child_pid=0; int pts; setsid(); pts = unix_open(devname, O_RDWR | O_CLOEXEC); if(pts < 0) { fprintf(stderr, "sdbu: child failed to open pseudo-term slave %s\n", devname); + E("sdbu: child failed to open pseudo-term slave %s\n", devname); exit(-1); } fprintf(stderr, "sdbu: child open pts %s\n", devname); + E("sdbu: child open pts %s\n", devname); sdb_close(ptm); @@ -127,6 +154,7 @@ int main (int argc, char **argv, char **envp) sdb_close(fd); } else { fprintf(stderr, "sdbu: child unable to open %s due to errno:%d\n", tmptext, errno); + E("sdbu: child unable to open %s due to errno:%d\n", tmptext, errno); } } @@ -136,6 +164,7 @@ int main (int argc, char **argv, char **envp) fprintf(stderr, "sdbu: child exec %s failed, errno: %d\n", SHELL_COMMAND, errno); exit(-1); } else { + /* sdbd-user process. This process open the ptm and unix socket to send ptm. */ /* socket create and open and bind, listen, accept and send fd here. */ @@ -144,18 +173,26 @@ int main (int argc, char **argv, char **envp) int sock, s; char c; pid_t mypid = getpid(); + child_pid=pid; + D("sdbu child_pipd=%d,pid=%d,mypid=%d\n",child_pid,pid,mypid); + + D("sdbu in child process\n"); + //usleep(500*10000); + //D("after sleep\n"); snprintf(tmptext, sizeof tmptext, "/tmp/.sdbduser_%d.sock", (int)mypid); sock = socket(PF_LOCAL, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "sdbu socket error, %d\n", errno); + E("sdbu socket error, %d\n", errno); return -1; } char *sockpath = strndup(tmptext, strlen(tmptext)); if (sockpath == NULL) { fprintf(stderr, "sdbu socket path error, %d\n", errno); + E("sdbu socket path error, %d\n", errno); sdb_close(sock); return -1; } @@ -167,19 +204,25 @@ int main (int argc, char **argv, char **envp) int slen = offsetof(struct sockaddr_un, sun_path) + strlen(sockpath); if (bind(sock, (struct sockaddr *)&addr, slen) == -1) { fprintf(stderr, "sdbu socket bind error, %d\n", errno); + E("sdbu socket bind error, %d\n", errno); goto socket_fail; } if (listen(sock, 5) == -1) { fprintf(stderr, "sdbu listen error, %d\n", errno); + E("sdbu listen error, %d\n", errno); goto socket_fail; } + D("sdbu before socket accept\n"); if ((s = sdb_socket_accept(sock, NULL, 0)) == -1) { fprintf(stderr, "sdbu accept error, %d\n", errno); + E("sdbu accept error, %d\n", errno); goto socket_fail; } else { + D("sdbu after socket accept\n"); /* send ptm fd to sdbd */ if (send_fd(s, &c, 1, ptm) != 0) { fprintf(stderr, "sdbu send fd error, %d\n", errno); + E("sdbu send fd error, %d\n", errno); } sdb_close(s); }