Ho bisogno di un'API C/C++ che mi permetta di elencare i processi in esecuzione su un sistema Linux e di elencare i file aperti da ogni processo.
Faccio non voglio finire a leggere direttamente il/proc/file system.
Qualcuno può pensare a un modo per farlo?
http://procps.sourceforge.net/
http://procps.cvs.sourceforge.net/viewvc/procps/procps/proc/readproc.c?view=markup
È la fonte di ps e altri strumenti di processo. Usano infatti proc (indicando che è probabilmente il modo convenzionale e migliore). La loro fonte è abbastanza leggibile. Il file
/procps-3.2.8/proc/readproc.c
Può essere utile Anche un suggerimento utile come pubblicato da ephemient è collegato all'API fornita da libproc, che dovrebbe essere disponibile nel repository (o già installato direi) ma sarà necessaria la variazione "-dev" per le intestazioni e cosa no.
In bocca al lupo
Se non vuoi leggere da '/ proc. Quindi puoi considerare di scrivere un modulo Kernel che implementerà la tua propria chiamata di sistema. E la tua chiamata di sistema dovrebbe essere scritta in modo che possa ottenere l'elenco dei processi correnti, come ad esempio:
/* ProcessList.c
Robert Love Chapter 3
*/
#include < linux/kernel.h >
#include < linux/sched.h >
#include < linux/module.h >
int init_module(void)
{
struct task_struct *task;
for_each_process(task)
{
printk("%s [%d]\n",task->comm , task->pid);
}
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Cleaning Up.\n");
}
Il codice sopra è preso dal mio articolo qui su http://linuxgazette.net/133/saha.html . Una volta che hai la tua chiamata di sistema, puoi chiamarla dal tuo programma spaziale utente.
Ecco qua (C/C++):
Potresti averlo trovato qui: http://ubuntuforums.org/showthread.php?t=657097
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h> // for opendir(), readdir(), closedir()
#include <sys/stat.h> // for stat()
#ifdef __cplusplus
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#endif
#define PROC_DIRECTORY "/proc/"
#define CASE_SENSITIVE 1
#define CASE_INSENSITIVE 0
#define EXACT_MATCH 1
#define INEXACT_MATCH 0
int IsNumeric(const char* ccharptr_CharacterList)
{
for ( ; *ccharptr_CharacterList; ccharptr_CharacterList++)
if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9')
return 0; // false
return 1; // true
}
int strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive)
{
if (intCaseSensitive)
return !strcmp(s1, s2);
else
return !strcasecmp(s1, s2);
}
int strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive)
{
if (intCaseSensitive)
return (int) strstr(haystack, needle);
else
return (int) strcasestr(haystack, needle);
}
#ifdef __cplusplus
pid_t GetPIDbyName(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch)
#else
pid_t GetPIDbyName_implements(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch)
#endif
{
char chrarry_CommandLinePath[100] ;
char chrarry_NameOfProcess[300] ;
char* chrptr_StringToCompare = NULL ;
pid_t pid_ProcessIdentifier = (pid_t) -1 ;
struct dirent* de_DirEntity = NULL ;
DIR* dir_proc = NULL ;
int (*CompareFunction) (const char*, const char*, int) ;
if (intExactMatch)
CompareFunction = &strcmp_Wrapper;
else
CompareFunction = &strstr_Wrapper;
dir_proc = opendir(PROC_DIRECTORY) ;
if (dir_proc == NULL)
{
perror("Couldn't open the " PROC_DIRECTORY " directory") ;
return (pid_t) -2 ;
}
// Loop while not NULL
while ( (de_DirEntity = readdir(dir_proc)) )
{
if (de_DirEntity->d_type == DT_DIR)
{
if (IsNumeric(de_DirEntity->d_name))
{
strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ;
strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
strcat(chrarry_CommandLinePath, "/cmdline") ;
FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ; // open the file for reading text
if (fd_CmdLineFile)
{
fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; // read from /proc/<NR>/cmdline
fclose(fd_CmdLineFile); // close the file prior to exiting the routine
if (strrchr(chrarry_NameOfProcess, '/'))
chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ;
else
chrptr_StringToCompare = chrarry_NameOfProcess ;
//printf("Process name: %s\n", chrarry_NameOfProcess);
//printf("Pure Process name: %s\n", chrptr_StringToCompare );
if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) )
{
pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
closedir(dir_proc) ;
return pid_ProcessIdentifier ;
}
}
}
}
}
closedir(dir_proc) ;
return pid_ProcessIdentifier ;
}
#ifdef __cplusplus
pid_t GetPIDbyName(const char* cchrptr_ProcessName)
{
return GetPIDbyName(cchrptr_ProcessName, CASE_INSENSITIVE, EXACT_MATCH) ;
}
#else
// C cannot overload functions - fixed
pid_t GetPIDbyName_Wrapper(const char* cchrptr_ProcessName, ... )
{
int intTempArgument ;
int intInputArguments[2] ;
// intInputArguments[0] = 0 ;
// intInputArguments[1] = 0 ;
memset(intInputArguments, 0, sizeof(intInputArguments) ) ;
int intInputIndex ;
va_list argptr;
va_start( argptr, cchrptr_ProcessName );
for (intInputIndex = 0; (intTempArgument = va_arg( argptr, int )) != 15; ++intInputIndex)
{
intInputArguments[intInputIndex] = intTempArgument ;
}
va_end( argptr );
return GetPIDbyName_implements(cchrptr_ProcessName, intInputArguments[0], intInputArguments[1]);
}
#define GetPIDbyName(ProcessName,...) GetPIDbyName_Wrapper(ProcessName, ##__VA_ARGS__, (int) 15)
#endif
int main()
{
pid_t pid = GetPIDbyName("bash") ; // If -1 = not found, if -2 = proc fs access error
printf("PID %d\n", pid);
return EXIT_SUCCESS ;
}
PS e ogni altro strumento (eccetto per i moduli Kernel) letti da /proc
. /proc
è uno speciale filesystem creato al volo dal kernel in modo che i processi in modalità utente possano leggere dati che altrimenti saranno disponibili solo per il kernel.
Il modo consigliato è quindi la lettura da /proc
.
Puoi vedere in modo intuitivo il filesystem /proc
per vedere come è strutturato . Per ogni processo c'è un /proc/pid
dove pid è il numero identificativo del processo. All'interno di questa cartella ci sono diversi file che includono diversi dati sul processo corrente Se corri
strace ps -aux
vedrai come il programma ps
legge questi dati da /proc
.
C'è una libreria libprocps
dal progetto procps-ng
. Su Ubuntu 13.04, se fai strace ps
, puoi vedere che ps
usa libprocps
.
L'unico modo per farlo senza leggere/proc sarebbe chiamare "ps aux", passare attraverso ogni riga, leggere la seconda colonna (il PID) e chiamare lsof -p [PID] con esso.
... Suggerirei di leggere/proc;)
Leggere proc non è male. Non posso mostrarti in C++, ma il seguente codice D dovrebbe indirizzarti nella giusta direzione:
import std.stdio; import std.string; import std.file; import std.regexp; import std.c.linux.linux; alias std.string.split explode; string srex = "^/proc/[0-9] + $"; string trex = "Stato: [\ t] [SR]" ; RegExp rex; RegExp rext; string [] scanPidDirs (string target) { stringa [] risultato; callback bool (DirEntry * de) { se (de.isdir) { if (rex.find (de.name)> = 0) { string [] a = explode (de.name, "/"); string pid = a [a.length-1]; string x = cast (stringa) std.file.read (de.name ~ "/status"); int n = rext.find (x); if (n> = 0) { x = cast (stringa) std.file.read (de.name ~ "/cmdline"); // Questo è null terminato if (x.length) x.length = x.length-1; a = esplode (x, "/"); se (a.lunghezza) x = a [a.length-1]; altro x = ""; se (x == target) { risultato ~ = pid ~ "/" ~ x; } } } } return true; } listdir ("/ proc", & callback); return result.dup; } void main (string [] args) { rex = new RegExp (srex); rext = new RegExp (trex); string [] a = scanPidDirs (args [1]); se (! a.length) { writefln ("Not found"); ritorno; } writefln ("% d matching processes", a.length); foreach (s; a) { string [] p = explode (s, "/"); int pid = atoi (p [0]); writef ("Stop% s (% d)?", s, pid); string r = readln (); if (r == "Y\n" || r == "y\n") kill (pid, SIGUSR1); } }