File indexing completed on 2024-05-19 04:58:31

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "processinfo.h"
0019 
0020 #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
0021 #include <sys/stat.h>
0022 #include <dirent.h>
0023 #include <unistd.h>
0024 
0025 #include <iostream>
0026 #include <cstdio>
0027 #include <cstdlib>
0028 #include <cstring>
0029 #include <cstdarg>
0030 #endif
0031 
0032 ProcessInfo::ProcessInfo(const QString &name)
0033     : m_name(name)
0034 {
0035 }
0036 
0037 bool ProcessInfo::isRunning() const
0038 {
0039 #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
0040     pid_t pid = GetPIDbyName(qPrintable(m_name));
0041     // -1 = process not found
0042     // -2 = /proc fs access error
0043     return (pid != -1 && pid != -2);
0044 #else
0045     return false;
0046 #endif
0047 }
0048 
0049 #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
0050 bool ProcessInfo::IsNumeric(const char* ccharptr_CharacterList) const
0051 {
0052     for (; *ccharptr_CharacterList; ccharptr_CharacterList++) {
0053         if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9') {
0054             return false;
0055         }
0056     }
0057 
0058     return true;
0059 }
0060 
0061 pid_t ProcessInfo::GetPIDbyName(const char* cchrptr_ProcessName) const
0062 {
0063     char chrarry_CommandLinePath[260]  ;
0064     char chrarry_NameOfProcess[300]  ;
0065     char* chrptr_StringToCompare = NULL ;
0066     pid_t pid_ProcessIdentifier = (pid_t) - 1 ;
0067     struct dirent* de_DirEntity = NULL ;
0068     DIR* dir_proc = NULL ;
0069 
0070     dir_proc = opendir("/proc/") ;
0071     if (dir_proc == NULL) {
0072         perror("Couldn't open the /proc/ directory") ;
0073         return (pid_t) - 2 ;
0074     }
0075 
0076     // Loop while not NULL
0077     while ((de_DirEntity = readdir(dir_proc))) {
0078 #ifndef __HAIKU__
0079         if (de_DirEntity->d_type == DT_DIR) {
0080             if (IsNumeric(de_DirEntity->d_name)) {
0081                 strcpy(chrarry_CommandLinePath, "/proc/") ;
0082                 strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
0083                 strcat(chrarry_CommandLinePath, "/cmdline") ;
0084                 FILE* fd_CmdLineFile = fopen(chrarry_CommandLinePath, "rt") ;   // open the file for reading text
0085                 if (fd_CmdLineFile) {
0086                     int r = fscanf(fd_CmdLineFile, "%20s", chrarry_NameOfProcess) ; // read from /proc/<NR>/cmdline
0087 
0088                     fclose(fd_CmdLineFile);  // close the file prior to exiting the routine
0089 
0090                     if (r < 1) {
0091                         continue;
0092                     }
0093 
0094                     if (strrchr(chrarry_NameOfProcess, '/')) {
0095                         chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') + 1 ;
0096                     }
0097                     else {
0098                         chrptr_StringToCompare = chrarry_NameOfProcess ;
0099                     }
0100 
0101                     //printf("Process name: %s\n", chrarry_NameOfProcess);
0102                     //printf("Pure Process name: %s\n", chrptr_StringToCompare );
0103 
0104                     if (!strcmp(chrptr_StringToCompare, cchrptr_ProcessName)) {
0105                         pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
0106                         closedir(dir_proc) ;
0107                         return pid_ProcessIdentifier ;
0108                     }
0109                 }
0110             }
0111         }
0112 #endif
0113     }
0114 
0115     closedir(dir_proc) ;
0116     return pid_ProcessIdentifier ;
0117 }
0118 
0119 #endif