⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readproc.c

📁 openmosix使用工具.mtop, mosmon, mps等。用于控制openmosix。openmosix 使用工具.mtop, mosmon, mps等。用于控制openmosix。openm
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * New Interface to Process Table -- PROCTAB Stream (a la Directory streams) * Copyright (C) 1996 Charles L. Blake. * Copyright (C) 1998 Michael K. Johnson * May be distributed under the conditions of the * GNU Library General Public License; a copy is in ../COPYING.LIB *//*** Modified on 27 Nov 2003 by Moreno 'baro' Baricevic <baro@democritos.it>** Added openMosix proc files handling for "nmigs", "lock", "cantmove".** Modified or added sections are delimited by "OMINFO" comments.**** "where" info was already handled (previously added by Mathieu Cousin** <Mathieu.Cousin@crm.mot.com>) and now merged with new modifications.*/#include "version.h"#include "readproc.h"#include "devname.h"#include "ps.h"#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <unistd.h>#include <fcntl.h>#include <sys/dir.h>#include <sys/types.h>#include <sys/stat.h>#define Do(x) (flags & PROC_ ## x)	/* convenient shorthand *//* initiate a process table scan */PROCTAB* openproc(int flags, ...) {    va_list ap;    PROCTAB* PT = xmalloc(sizeof(PROCTAB));    if (!Do(PID) && !(PT->procfs = opendir("/proc")))    	return NULL;    PT->flags = flags;    va_start(ap, flags);		/*  Init args list */    if (Do(PID))    	PT->pids = va_arg(ap, pid_t*);    else if (Do(TTY))    	PT->ttys = va_arg(ap, dev_t*);    else if (Do(UID)) {    	PT->uids = va_arg(ap, uid_t*);	PT->nuid = va_arg(ap, int);    } else if (Do(STAT))    	PT->stats = va_arg(ap, char*);    va_end(ap);				/*  Clean up args list */    if (Do(ANYTTY) && Do(TTY))	PT->flags = PT->flags & ~PROC_TTY; /* turn off TTY flag */    return PT;}/* terminate a process table scan */void closeproc(PROCTAB* PT) {    if (PT->procfs) closedir(PT->procfs);    if (PT)         free(PT);}/* deallocate the space allocated by readproc if the passed rbuf was NULL */void freeproc(proc_t* p) {    if (!p)	/* in case p is NULL */	return;    /* ptrs are after strings to avoid copying memory when building them. */    /* so free is called on the address of the address of strvec[0]. */    if (p->cmdline)	free((void*)*p->cmdline);    if (p->environ)	free((void*)*p->environ);    free(p);}/* stat2proc() makes sure it can handle arbitrary executable file basenames   for `cmd', i.e. those with embedded whitespace or embedded ')'s.  Such names   confuse %s (see scanf(3)), so the string is split and %39c is used instead.   (except for embedded ')' "(%[^)]c)" would work.*/void stat2proc(char* S, proc_t* P) {    char* tmp = strrchr(S, ')');	/* split into "PID (cmd" and "<rest>" */    *tmp = '\0';			/* replace trailing ')' with NUL */    /* parse these two strings separately, skipping the leading "(". */    memset(P->cmd, 0, sizeof P->cmd);	/* clear even though *P xcalloc'd ?! */    sscanf(S, "%d (%39c", &P->pid, P->cmd);    sscanf(tmp + 2,			/* skip space after ')' too */	   "%c %d %d %d %d %d %lu %lu %lu %lu %lu %ld %ld %ld %ld %d "	   "%d %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %LX %LX %LX %LX %lu",	   &P->state, &P->ppid, &P->pgrp, &P->session, &P->tty, &P->tpgid,	   &P->flags, &P->min_flt, &P->cmin_flt, &P->maj_flt, &P->cmaj_flt,	   &P->utime, &P->stime, &P->cutime, &P->cstime, &P->priority, &P->nice,	   &P->timeout, &P->it_real_value, &P->start_time, &P->vsize, &P->rss,	   &P->rss_rlim, &P->start_code, &P->end_code, &P->start_stack,	   &P->kstk_esp, &P->kstk_eip, &P->signal, &P->blocked, &P->sigignore,	   &P->sigcatch, &P->wchan);    if (P->tty == 0)	P->tty = -1;  /* the old notty val, update elsewhere bef. moving to 0 */    if (linux_version_code < LINUX_VERSION(1,3,39)) {	P->priority = 2*15 - P->priority;	/* map old meanings to new */	P->nice = 15 - P->nice;    }    if (linux_version_code < LINUX_VERSION(1,1,30) && P->tty != -1)	P->tty = 4*0x100 + P->tty;		/* when tty wasn't full devno */}void statm2proc(char* s, proc_t* P) {    sscanf(s, "%ld %ld %ld %ld %ld %ld %ld",	   &P->size, &P->resident, &P->share,	   &P->trs, &P->lrs, &P->drs, &P->dt);}void nulls2sep(char* str, int len, char sep) {    int i;    for (i = 0; i < len; i++)    	if (str[i] == 0)    	    str[i] = sep;}int file2str(char *directory, char *what, char *ret, int cap) {    static char filename[80];    int fd, num_read;    sprintf(filename, "%s/%s", directory, what);    if ( (fd       = open(filename, O_RDONLY, 0)) == -1 ) return -1;    if ( (num_read = read(fd, ret, cap - 1))      <= 0 ) return -1;    ret[num_read] = 0;    close(fd);    return num_read;}/* OMINFO { *//* adapted from old readwhere() *//*** Read integer from /proc/<PID>/<file>** e.g.: from oM_files where, nmigs, lock (or any proc file with an integer stored)** Does not check the validity of the value read from file ('-1' may be a problem).*/int readint ( const char * dir , const char * file ){   static char filename[80];   static char buffer[6];   int fd, num_read, ival;   sprintf( filename , "%s/%s" , dir , file );   if ( ( fd = open( filename , O_RDONLY , 0 ) ) == -1 )     return -1;   num_read = read( fd , buffer , sizeof( buffer ) - 1 );   close( fd );   if ( num_read <= 0 )     return -1;   ival = atoi( buffer );   return ival;} /* readint *//* adapted from ompsinfo.c *//*** read cantmove string*/const char * readcantmove ( const char * dir , const int lock ){// What is the maximum length of "cantmove"?// Assumes the total length of the "char *mosix_stay_string[]" values (fs/proc/array.c), "extern_#" excluded.//    monkey, mmap_dev, VM86_mode, daemon, priv_inst, mem_lock, clone_vm, rt_sched, direct_io, init_proc, kiobuf. user_lock, ********#define CANT_BUF_SIZE 128   static char filename[80];   int fd, num_read;   const char * migratable = ( lock ) ? OM_MIG_LOCKED : OM_MIGRATABLE ;   char * buffer = (char *) malloc( CANT_BUF_SIZE );	/* need this since the output of mps is page buffered */							/* !!! this is a memory leak if used by mtop !!! */   if ( buffer == NULL )     return "(*** OUT OF MEMORY ***)";   memset( buffer , 0 , CANT_BUF_SIZE );   sprintf( filename , "%s/cantmove" , dir );   fd = open( filename , O_RDONLY , 0 );   if ( fd == -1 )   {      free( buffer );      return OM_NOT_AVAIL ;   }   num_read = read( fd , buffer , CANT_BUF_SIZE - 1 );   close( fd );   if ( num_read <= 0 )   {      free( buffer );      return ( num_read == 0 ) ? migratable : OM_NOT_AVAIL ;   }   {      register char * ptr = buffer;      while ( ++ptr && *ptr )        if ( *ptr == '\n' || *ptr == '\r' )          *ptr = '\0';   }   return buffer;#undef CANT_BUF_SIZE} /* readcantmove *//* } OMINFO */char** file2strvec(char* directory, char* what) {    char buf[2048];	/* read buf bytes at a time */    char *p, *rbuf = 0, *endbuf, **q, **ret;    int fd, tot = 0, n, c, end_of_file = 0;    int align;    sprintf(buf, "%s/%s", directory, what);    if ( (fd = open(buf, O_RDONLY, 0) ) == -1 ) return NULL;    /* read whole file into a memory buffer, allocating as we go */    while ((n = read(fd, buf, sizeof buf - 1)) > 0) {	if (n < sizeof buf - 1)	    end_of_file = 1;	if (n == 0 && rbuf == 0)	    return NULL;	/* process died between our open and read */	if (n < 0) {	    if (rbuf)		free(rbuf);	    return NULL;	/* read error */	}	if (end_of_file && buf[n-1])		/* last read char not null */	    buf[n++] = '\0';			/* so append null-terminator */	rbuf = xrealloc(rbuf, tot + n);		/* allocate more memory */	memcpy(rbuf + tot, buf, n);		/* copy buffer into it */	tot += n;				/* increment total byte ctr */	if (end_of_file)	    break;    }    close(fd);    if (n <= 0 && !end_of_file) {	if (rbuf) free(rbuf);	return NULL;		/* read error */    }    endbuf = rbuf + tot;			/* count space for pointers */    align = (sizeof(char*)-1) - ((tot + sizeof(char*)-1) & (sizeof(char*)-1));    for (c = 0, p = rbuf; p < endbuf; p++)    	if (!*p)	    c += sizeof(char*);    c += sizeof(char*);				/* one extra for NULL term */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -