Implemantation of socket support for data output
authorIgor Oblakov <ioblakov@dev.rtsoft.ru>
Wed, 8 Aug 2018 16:21:39 +0000 (19:21 +0300)
committerAleksei Vereshchagin <avereschagin@dev.rtsoft.ru>
Tue, 28 Aug 2018 22:24:18 +0000 (01:24 +0300)
README.md
profctl.c

index fbe74d0eace87b3b5e891d63bbf87720750cae2d..1253f7f346c050619687666685e337b0bf4c519c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -14,21 +14,25 @@ profctl [options...]
 
 ### Options
 
-* `-a`, `--appid=ID`  
+* `-a`, `--appid=ID`
   Tizen Application ID
-* `-p, --pipe=NAME`  
+* `-p, --pipe=NAME`
   name of pipe file
-* `-e, --exec=NAME`  
+* `-e, --exec=NAME`
   execution to launch
-* `-o, --error=NAME`  
+* `-o, --error=NAME`
   redefines path to log file
-* `-v, --verbose`  
+* `-v, --verbose`
   verbose output
-* `-l, --list`  
+* `-l, --list`
   list Tizen Application
-* `-i, --info`  
+* `-i, --info`
   show info
-* `-t, --timeout=SEC`  
+* `-t, --timeout=SEC`
   waiting timeout in seconds
-* `-w, --pipe-owner`  
+* `-w, --pipe-owner`
   make profctl responsible on pipe management (create/unlink)
+* `-s, --server=port`
+  server port for control and data output
+* `-S, --stat=port`
+  server port for statistic output
index 3df795bb206eb8dcecb01625bce39f53c74dc66f..a80e2db45833173b189570d72a51e7b186518e5d 100644 (file)
--- a/profctl.c
+++ b/profctl.c
@@ -22,6 +22,8 @@
 #include <sys/stat.h>
 #include <sys/xattr.h>
 #include <sys/statvfs.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
 #include <fcntl.h>
 
 struct app_info {
@@ -40,10 +42,16 @@ static struct termios sterm;
 static char *appid = NULL;
 static char *pname = NULL;
 static char *ename = NULL;
-static const char *oname = NULL;
+static char *oname = NULL;
 
 static int isPipeOwner = 0;
 
+static int controlPort = -1;
+static int statPort = -1;
+static FILE *cfile;
+static FILE *ofile;
+static FILE *sfile;
+
 static struct option long_options[] = {
        {"appid",       required_argument, 0, 'a'},
        {"pipe",        required_argument, 0, 'p'},
@@ -54,6 +62,8 @@ static struct option long_options[] = {
        {"list",        no_argument,       0, 'l'},
        {"info",        no_argument,       0, 'i'},
        {"pipe-owner",  no_argument,       0, 'w'},
+       {"server",      required_argument, 0, 's'},
+       {"stat",        required_argument, 0, 'S'},
        {0,         0,                 0,  0}
 };
 
@@ -87,7 +97,9 @@ static void *wait_thread(void *arg)
 static void finish()
 {
 #if TIZEN
-       tcsetattr(0,TCSANOW, &sterm);
+       if (controlPort < 0) {
+               tcsetattr(0,TCSANOW, &sterm);
+       }
 #endif /* TIZEN */
        if (tf != NULL) {
                fclose(tf);
@@ -95,6 +107,7 @@ static void finish()
                        unlink(pname);
                }
        }
+       fflush(ofile);
        kill(getpid(), SIGKILL);
 }
 
@@ -143,7 +156,7 @@ static void *outstat(void *arg)
        long psize = sysconf(_SC_PAGESIZE);
        struct statvfs fstat;
 
-       printf("psize %ld\n", psize);
+       fprintf(sfile, "psize %ld\n", psize);
 
        if (sstat == NULL ) {
                perror("open /proc/stat");
@@ -277,11 +290,13 @@ static void *outstat(void *arg)
                        ? (fstat.f_bsize / 1024) * fstat.f_bavail
                        : fstat.f_bavail / (1024 / fstat.f_bsize);
 
-               printf("%ld.%d %d" " %s %s %s" " %s %s" " %s %s %ld %ld\n",
+               fprintf(sfile,
+                       "%ld.%d %d" " %s %s %s" " %s %s" " %s %s %ld %ld\n",
                        t, millisec, ncpu,
                        stats[1], stats[3], stats[4], /* user system idle */
                        pstats[14 - 1], pstats[15 - 1], /* puser psystem */
                        mt[1], mf[1], available, pages * psize);
+               fflush(sfile);
 
                usleep(timeoutMicrosec);
        }
@@ -321,7 +336,7 @@ static int process_option(int argc, char **argv)
 {
        int option_index;
 
-       switch(getopt_long(argc, argv, "-a:p:vle:o:it:w",
+       switch(getopt_long(argc, argv, "-a:p:vle:o:it:ws:S:",
                long_options, &option_index)) {
        case 1:
                if (appid == NULL) {
@@ -342,6 +357,8 @@ static int process_option(int argc, char **argv)
        case 'i': doinfo = 1; break;
        case 't': timeoutMicrosec = atoi(optarg) * 1000; break;
        case 'w': isPipeOwner = 1; break;
+       case 's': controlPort = atoi(optarg); break;
+       case 'S': statPort = atoi(optarg); break;
        default:
                return -1;
        }
@@ -481,6 +498,46 @@ static void openFileProcess()
        }
 }
 
+static int openPort(int port)
+{
+       int result;
+       int enable = 1;
+       struct sockaddr_in saddr = {0};
+       struct sockaddr_in caddr;
+       int csize = sizeof(caddr);
+       int sock = socket(AF_INET, SOCK_STREAM, 0);
+
+       if (sock < 0) {
+               perror("socket");
+               exit(1);
+       }
+
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
+       {
+               perror("setsockopt");
+               exit(1);
+       }
+
+       saddr.sin_family = AF_INET;
+       saddr.sin_addr.s_addr = INADDR_ANY;
+       saddr.sin_port = htons(port);
+
+       if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
+       {
+               perror("bind");
+               exit(1);
+       }
+
+       listen(sock, 5);
+
+       result = accept(sock, (struct sockaddr *) &caddr, &csize);
+       if (result < 0) {
+               perror("accept");
+               exit(1);
+       }
+       return result;
+}
+
 int main(int argc, char **argv)
 {
 #if TIZEN
@@ -489,6 +546,10 @@ int main(int argc, char **argv)
        char *line = NULL;
        size_t len = 0;
 
+       cfile = stdin;
+       ofile = stdout;
+       sfile = stdout;
+
        while(!process_option(argc, argv));
 
 #if TIZEN
@@ -505,6 +566,13 @@ int main(int argc, char **argv)
        freopen(oname, "w", stderr);
        setbuf(stderr, NULL);
 
+       if (verbose) {
+               int i;
+               for (i = 0; i < argc; i++) {
+                       fprintf(stderr, "argv[%d] = %s\n", i, argv[i]);
+               }
+       }
+
        if (pname && isPipeOwner) {
                unlink(pname);
                if (mkfifo(pname, 0666)) {
@@ -521,16 +589,37 @@ int main(int argc, char **argv)
        }
 
 #if TIZEN
-       /* stty */
-       tcgetattr(0, &term);
-       sterm = term;
+       if (controlPort < 0 && statPort < 0) {
+               /* stty */
+               tcgetattr(0, &term);
+               sterm = term;
 
-       term.c_lflag &= ~(ECHO|ECHONL); /* no echo and so on */
-       term.c_oflag &= ~OPOST; /* no additional CR and so on */
+               term.c_lflag &= ~(ECHO|ECHONL); /* no echo and so on */
+               term.c_oflag &= ~OPOST; /* no additional CR and so on */
 
-       tcsetattr(0,TCSANOW, &term);
+               tcsetattr(0,TCSANOW, &term);
+       }
 #endif /* TIZEN */
 
+       if (controlPort > 0) {
+               int s = openPort(controlPort);
+               cfile = fdopen(s, "r");
+               ofile = fdopen(s, "w");
+               if (cfile == NULL || ofile == NULL) {
+                       perror("fdopen");
+                       exit(1);
+               }
+       }
+
+       if (doinfo && (statPort > 0)) {
+               int s = openPort(statPort);
+               sfile = fdopen(s, "w");
+               if (sfile == NULL) {
+                       perror("fdopen");
+                       exit(1);
+               }
+       }
+
        atexit(finish);
 
        if (pname) {
@@ -543,7 +632,7 @@ int main(int argc, char **argv)
 #endif /* TIZEN */
 
        /* Read command loop */
-       while((len = getline(&line, &len, stdin)) != -1) {
+       while((len = getline(&line, &len, cfile)) != -1) {
                if (verbose) {
                        fprintf(stderr, "line :%s#\n", line);
                }