From d97a3de34d6da20d747279eae8e05c32594e2e4d Mon Sep 17 00:00:00 2001 From: vanhofen Date: Thu, 11 Aug 2016 13:52:49 +0200 Subject: [PATCH] helpers.chh/h: add some helper functions Partial cherry pick from: https://bitbucket.org/neutrino-images/ni-neutrino-hd Signed-off-by: Thilo Graf Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/43c7e0ed0992c0fd0d6a473312ed5533f2e641af Author: vanhofen Date: 2016-08-11 (Thu, 11 Aug 2016) ------------------ This commit was generated by Migit --- src/system/helpers.cpp | 108 +++++++++++++++++++++++++++++++++++++++++ src/system/helpers.h | 5 ++ 2 files changed, 113 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 59d64524a..17afa8c1a 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -50,6 +50,8 @@ //#include #include #include +#include +#define MD5_DIGEST_LENGTH 16 using namespace std; int mySleep(int sec) { @@ -1273,6 +1275,112 @@ std::string Lang2ISO639_1(std::string& lang) return ret; } +// returns the pid of the first process found in /proc +int getpidof(const char *process) +{ + DIR *dp; + struct dirent *entry; + struct stat statbuf; + + if ((dp = opendir("/proc")) == NULL) + { + fprintf(stderr, "Cannot open directory /proc\n"); + return -1; + } + + while ((entry = readdir(dp)) != NULL) + { + // get information about the file/folder + lstat(entry->d_name, &statbuf); + // files under /proc which start with a digit are processes + if (S_ISDIR(statbuf.st_mode) && isdigit(entry->d_name[0])) + { + // 14 chars for /proc//status + 0 + char procpath[14 + strlen(entry->d_name)]; + char procname[50]; + FILE *file; + + sprintf(procpath, "/proc/%s/status", entry->d_name); + + if (! (file = fopen(procpath, "r")) ) { + continue; + } + + fscanf(file,"%*s %s", procname); + fclose(file); + + // only 15 char available + if (strncmp(procname, process, 15) == 0) { + return atoi(entry->d_name); + } + } + } + closedir (dp); + return 0; +} + +std::string filehash(const char *file) +{ +#if 0 + int fd; + int i; + unsigned long size; + struct stat s_stat; + unsigned char hash[MD5_DIGEST_LENGTH]; + void *buff; + std::ostringstream os; + + memset(hash, 0, MD5_DIGEST_LENGTH); + + fd = open(file, O_RDONLY | O_NONBLOCK); + if (fd > 0) + { + // Get the size of the file by its file descriptor + fstat(fd, &s_stat); + size = s_stat.st_size; + + buff = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); + MD5((const unsigned char *)buff, size, hash); + munmap(buff, size); + + // Print the MD5 sum as hex-digits. + for(i = 0; i < MD5_DIGEST_LENGTH; ++i) { + os.width(2); + os.fill('0'); + os << std::hex << static_cast(hash[i]); + } + close(fd); + } + return os.str(); +#endif + int i; + unsigned char hash[MD5_DIGEST_LENGTH]; + std::ostringstream os; + + md5_file(file, 1, (unsigned char*) hash); + // Print the MD5 sum as hex-digits. + for(i = 0; i < MD5_DIGEST_LENGTH; ++i) { + os.width(2); + os.fill('0'); + os << std::hex << static_cast(hash[i]); + } + return os.str(); +} + +std::string get_path(const char *path) +{ + if(path[0] == '/' && strstr(path,"/var") == 0) + { + std::string varc = "/var"; + varc += path; + + if(file_exists(varc.c_str())) + return varc; + } + + return path; +} + string readLink(string lnk) { char buf[PATH_MAX]; diff --git a/src/system/helpers.h b/src/system/helpers.h index 48ac50a06..57fe0216d 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -146,4 +146,9 @@ std::string getJFFS2MountPoint(int mtdPos); std::string Lang2ISO639_1(std::string& lang); std::string readLink(std::string lnk); +int getpidof(const char *process); +std::string filehash(const char * file); +std::string get_path(const char * path); +inline bool file_exists(const std::string file) { return file_exists(file.c_str()); } + #endif