Falkon Develop
Cross-platform Qt-based web browser
processinfo.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "processinfo.h"
19
20#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
21#include <sys/stat.h>
22#include <dirent.h>
23#include <unistd.h>
24
25#include <iostream>
26#include <cstdio>
27#include <cstdlib>
28#include <cstring>
29#include <cstdarg>
30#endif
31
32ProcessInfo::ProcessInfo(const QString &name)
33 : m_name(name)
34{
35}
36
38{
39#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
40 pid_t pid = GetPIDbyName(qPrintable(m_name));
41 // -1 = process not found
42 // -2 = /proc fs access error
43 return (pid != -1 && pid != -2);
44#else
45 return false;
46#endif
47}
48
49#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS)
50bool ProcessInfo::IsNumeric(const char* ccharptr_CharacterList) const
51{
52 for (; *ccharptr_CharacterList; ccharptr_CharacterList++) {
53 if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9') {
54 return false;
55 }
56 }
57
58 return true;
59}
60
61pid_t ProcessInfo::GetPIDbyName(const char* cchrptr_ProcessName) const
62{
63 char chrarry_CommandLinePath[260] ;
64 char chrarry_NameOfProcess[300] ;
65 char* chrptr_StringToCompare = NULL ;
66 pid_t pid_ProcessIdentifier = (pid_t) - 1 ;
67 struct dirent* de_DirEntity = NULL ;
68 DIR* dir_proc = NULL ;
69
70 dir_proc = opendir("/proc/") ;
71 if (dir_proc == NULL) {
72 perror("Couldn't open the /proc/ directory") ;
73 return (pid_t) - 2 ;
74 }
75
76 // Loop while not NULL
77 while ((de_DirEntity = readdir(dir_proc))) {
78#ifndef __HAIKU__
79 if (de_DirEntity->d_type == DT_DIR) {
80 if (IsNumeric(de_DirEntity->d_name)) {
81 strcpy(chrarry_CommandLinePath, "/proc/") ;
82 strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
83 strcat(chrarry_CommandLinePath, "/cmdline") ;
84 FILE* fd_CmdLineFile = fopen(chrarry_CommandLinePath, "rt") ; // open the file for reading text
85 if (fd_CmdLineFile) {
86 int r = fscanf(fd_CmdLineFile, "%20s", chrarry_NameOfProcess) ; // read from /proc/<NR>/cmdline
87
88 fclose(fd_CmdLineFile); // close the file prior to exiting the routine
89
90 if (r < 1) {
91 continue;
92 }
93
94 if (strrchr(chrarry_NameOfProcess, '/')) {
95 chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') + 1 ;
96 }
97 else {
98 chrptr_StringToCompare = chrarry_NameOfProcess ;
99 }
100
101 //printf("Process name: %s\n", chrarry_NameOfProcess);
102 //printf("Pure Process name: %s\n", chrptr_StringToCompare );
103
104 if (!strcmp(chrptr_StringToCompare, cchrptr_ProcessName)) {
105 pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
106 closedir(dir_proc) ;
107 return pid_ProcessIdentifier ;
108 }
109 }
110 }
111 }
112#endif
113 }
114
115 closedir(dir_proc) ;
116 return pid_ProcessIdentifier ;
117}
118
119#endif
bool isRunning() const
Definition: processinfo.cpp:37
ProcessInfo(const QString &name)
Definition: processinfo.cpp:32