#include <termios.h>
#include <sys/ioctl.h>
-
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include "sdbd_plugin.h"
#include "plugin.h"
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);
}
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
-
+#include <signal.h>
#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)
{
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) {
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);
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);
}
}
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. */
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;
}
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);
}