From 8e698d8d7f888304c0dd5b8aae06a4ff5928d83c Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sat, 28 Jan 2017 23:17:03 +0100 Subject: [PATCH] helpers: add run_pty() function This runs an external command inside a pty. Running inside a pty, external commands using stdio(3) will disable stdout buffering when running from a terminal, which is often desirable. Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/9d78d7072d11294ed0f4a51cb4955a2147115c7a Author: Stefan Seyfried Date: 2017-01-28 (Sat, 28 Jan 2017) ------------------ This commit was generated by Migit --- src/Makefile.am | 1 + src/system/helpers.cpp | 19 ++++++++++++++++++- src/system/helpers.h | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index 8af131987..e2f8a3ba6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -123,6 +123,7 @@ neutrino_LDADD = \ $(PUGIXML_LIBS) \ -ldvbsi++ \ -ljpeg \ + -lutil \ -lOpenThreads \ -lrt -lpthread diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index f668dd735..2f207ac2b 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -4,7 +4,7 @@ License: GPL (C) 2012-2013 the neutrino-hd developers - (C) 2012-2015 Stefan Seyfried + (C) 2012-2017 Stefan Seyfried This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,6 +30,7 @@ #include #include #include +#include /* forkpty*/ #include #include #include @@ -222,6 +223,22 @@ FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type) } return(fp); } + +int run_pty(pid_t &pid, const char *cmdstring) +{ + int master = -1; + if ((pid = forkpty(&master, NULL, NULL, NULL)) < 0) + return -1; + else if (pid == 0) { + int maxfd = getdtablesize(); + for(int i = 3; i < maxfd; i++) + close(i); + execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); + exit(0); + } + return master; +} + #if 0 int mkdirhier(const char *pathname, mode_t mode) { diff --git a/src/system/helpers.h b/src/system/helpers.h index 431851d76..53205f8e1 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -41,6 +41,7 @@ int my_system(const char * cmd); int my_system(int argc, const char *arg, ...); /* argc is number of arguments including command */ FILE* my_popen( pid_t& pid, const char *cmdstring, const char *type); +int run_pty(pid_t &pid, const char *cmdstring); int safe_mkdir(const char * path); inline int safe_mkdir(std::string path) { return safe_mkdir(path.c_str()); }