library: cleanup of library includes

The includes used to define a lot of things a library include
should not. It was also a bit messy what was exposed in the library
and what was not.

get_pid_digits -> procps_pid_length and exported correctly

MALLOC attribute move into relevant .c files
NORETURN attribute moved to relevant .c, not used in library
PURE attribute removed, it wasn't used
KLONG/KLF/STRTOUKL were fixed for long, so now just use long

HIDDEN attribute removed. It was for 3 functions. The PROCPS_EXPORT
seems to do the same (opposite) thing.

likely/unlikely removed from most places, its highly debateable
this does anything useful as CPUs have gotten smarter about branches.

Re-arranged the includes, ALL external programs should just #include
<proc/procps.h> then proc/procps.h includes headers for files that
have exported functions. procps.h and the headers it includes should
not use items that are not exportable (e.g. hidden functions or
macros) they go in procps-private.h
This commit is contained in:
Craig Small
2016-04-16 17:03:57 +10:00
parent d15a8901d2
commit ccb6ae8de1
38 changed files with 137 additions and 181 deletions

View File

@ -25,6 +25,10 @@
#include "alloc.h"
typedef void (*message_fn)(const char *__restrict, ...) __attribute__((format(printf,1,2)));
/* change xalloc_err_handler to override the default fprintf(stderr... */
extern message_fn xalloc_err_handler;
static void xdefault_error(const char *restrict fmts, ...) __attribute__((format(printf,1,2)));
static void xdefault_error(const char *restrict fmts, ...) {
va_list va;

View File

@ -1,12 +1,11 @@
#ifndef PROCPS_PROC_ALLOC_H
#define PROCPS_PROC_ALLOC_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS
/* change xalloc_err_handler to override the default fprintf(stderr... */
extern message_fn xalloc_err_handler;
#define MALLOC __attribute__ ((__malloc__))
extern void *xcalloc(unsigned int size) MALLOC;
extern void *xmalloc(size_t size) MALLOC;

View File

@ -1,7 +1,7 @@
#ifndef PROC_DEVNAME_H
#define PROC_DEVNAME_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS

View File

@ -22,7 +22,7 @@
#ifndef PROC_DISKSTAT_H
#define PROC_DISKSTAT_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS

View File

@ -1,8 +1,8 @@
#ifndef PROCPS_PROC_ESCAPE_H
#define PROCPS_PROC_ESCAPE_H
#include <proc/procps.h>
#include <proc/readproc.h>
#include <features.h>
__BEGIN_DECLS

View File

@ -7,7 +7,6 @@ global:
escape_strlist;
escaped_copy;
fatal_proc_unmounted;
get_pid_digits;
look_up_our_self;
lookup_wchan;
openproc;
@ -40,6 +39,7 @@ global:
procps_ns_get_name;
procps_ns_get_id;
procps_ns_read_pid;
procps_pid_length;
procps_pids_new;
procps_pids_read_next;
procps_pids_read_open;

View File

@ -23,7 +23,7 @@
#ifndef PROC_MEMINFO_H
#define PROC_MEMINFO_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS

View File

@ -23,8 +23,6 @@
#ifndef PROC_NAMESPACE_H
#define PROC_NAMESPACE_H
#include <proc/procps.h>
__BEGIN_DECLS
enum namespace_type {

View File

@ -23,6 +23,7 @@
#ifndef _PROC_PIDS_H
#define _PROC_PIDS_H
#include <features.h>
__BEGIN_DECLS
enum pids_item {

View File

@ -20,6 +20,11 @@
#include <proc/procps.h>
#define FUNCTION __attribute__((__const__)) // no access to global mem, even via ptr, and no side effect
#define PROCPS_EXPORT __attribute__ ((visibility("default")))
#define STRINGIFY_ARG(a) #a
#define STRINGIFY(a) STRINGIFY_ARG(a)
#endif

View File

@ -1,59 +1,35 @@
/*
* libprocps - Library to read proc filesystem
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PROCPS_PROC_PROCPS_H
#define PROCPS_PROC_PROCPS_H
#include <features.h>
#define KLONG long
#define KLF "l"
#define STRTOUKL strtoul
// since gcc-2.5
#define NORETURN __attribute__((__noreturn__))
#define FUNCTION __attribute__((__const__)) // no access to global mem, even via ptr, and no side effect
#if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
// won't alias anything, and aligned enough for anything
#define MALLOC __attribute__ ((__malloc__))
// no side effect, may read globals
#define PURE __attribute__ ((__pure__))
// tell gcc what to expect: if(unlikely(err)) die(err);
#define likely(x) __builtin_expect(!!(x),1)
#define unlikely(x) __builtin_expect(!!(x),0)
#define expected(x,y) __builtin_expect((x),(y))
#else
#define MALLOC
#define PURE
#define likely(x) (x)
#define unlikely(x) (x)
#define expected(x,y) (x)
#endif
#ifdef SHARED
# if SHARED==1 && (__GNUC__ > 2 || __GNUC_MINOR__ >= 96)
# define LABEL_OFFSET
# endif
#endif
#define STRINGIFY_ARG(a) #a
#define STRINGIFY(a) STRINGIFY_ARG(a)
// marks old junk, to warn non-procps-ng library users
#if ( __GNUC__ == 3 && __GNUC_MINOR__ > 0 ) || __GNUC__ > 3
#define OBSOLETE __attribute__((deprecated))
#else
#define OBSOLETE
#endif
// Like HIDDEN, but for an alias that gets created.
// In gcc-3.2 there is an alias+hidden conflict.
// Many will have patched this bug, but oh well.
#if ( __GNUC__ == 3 && __GNUC_MINOR__ > 2 ) || __GNUC__ > 3
#define HIDDEN_ALIAS(x) extern __typeof(x) x##_direct __attribute__((alias(#x),visibility("hidden")))
#else
#define HIDDEN_ALIAS(x) extern __typeof(x) x##_direct __attribute__((alias(#x)))
#endif
typedef void (*message_fn)(const char *__restrict, ...) __attribute__((format(printf,1,2)));
/* includes that show public exports go here */
#include <proc/diskstat.h>
#include <proc/escape.h>
#include <proc/meminfo.h>
#include <proc/namespace.h>
#include <proc/pids.h>
#include <proc/readproc.h>
#include <proc/readstat.h>
#include <proc/slab.h>
#include <proc/sysinfo.h>
#include <proc/version.h>
#include <proc/vmstat.h>
#include <proc/uptime.h>
#endif

View File

@ -1,8 +1,8 @@
#ifndef PROCPS_PROC_PWCACHE_H
#define PROCPS_PROC_PWCACHE_H
#include <features.h>
#include <sys/types.h>
#include <proc/procps.h>
__BEGIN_DECLS

View File

@ -42,6 +42,10 @@
#endif
#include <proc/namespace.h>
#define likely(x) __builtin_expect(!!(x),1)
#define unlikely(x) __builtin_expect(!!(x),0)
#define expected(x,y) __builtin_expect((x),(y))
// sometimes it's easier to do this manually, w/o gcc helping
#ifdef PROF
extern void __cyg_profile_func_enter(void*,void*);
@ -550,9 +554,9 @@ ENTER(0x160);
"%llu " /* start_time */
"%lu "
"%ld "
"%lu %"KLF"u %"KLF"u %"KLF"u %"KLF"u %"KLF"u "
"%lu %lu %lu %lu %lu %lu "
"%*s %*s %*s %*s " /* discard, no RT signals & Linux 2.1 used hex */
"%"KLF"u %*u %*u "
"%lu %*u %*u "
"%d %d "
"%lu %lu",
&P->state,
@ -906,7 +910,7 @@ static proc_t* simple_readproc(PROCTAB *restrict const PT, proc_t *restrict cons
// if multithreaded, some values are crap
if(p->nlwp > 1){
p->wchan = (KLONG)~0ull;
p->wchan = ~0ul;
}
/* some number->text resolving which is time consuming */
@ -1403,9 +1407,6 @@ void look_up_our_self(proc_t *p) {
free(ub.buf);
}
HIDDEN_ALIAS(readproc);
HIDDEN_ALIAS(readtask);
HIDDEN_ALIAS(readeither);
/* Convenient wrapper around openproc and readproc to slurp in the whole process
* table subset satisfying the constraints of flags and the optional PID list.
@ -1439,7 +1440,7 @@ proc_t** readproctab(unsigned flags, ...) {
return 0;
do { /* read table: */
tab = xrealloc(tab, (n+1)*sizeof(proc_t*));/* realloc as we go, using */
tab[n] = readproc_direct(PT, NULL); /* final null to terminate */
tab[n] = readproc(PT, NULL); /* final null to terminate */
} while (tab[n++]); /* stop when NULL reached */
closeproc(PT);
return tab;
@ -1473,7 +1474,7 @@ proc_data_t *readproctab2(int(*want_proc)(proc_t *buf), int(*want_task)(proc_t *
n_proc_alloc = n_proc_alloc*5/4+30; // grow by over 25%
ptab = xrealloc(ptab,sizeof(proc_t*)*n_proc_alloc);
}
tmp = readproc_direct(PT, data+n_used);
tmp = readproc(PT, data+n_used);
if(!tmp) break;
if(!want_proc(tmp)) continue;
ptab[n_proc++] = (proc_t*)(n_used++);
@ -1493,7 +1494,7 @@ proc_data_t *readproctab2(int(*want_proc)(proc_t *buf), int(*want_task)(proc_t *
n_task_alloc = n_task_alloc*5/4+1; // grow by over 25%
ttab = xrealloc(ttab,sizeof(proc_t*)*n_task_alloc);
}
t = readtask_direct(PT, tmp, data+n_used);
t = readtask(PT, tmp, data+n_used);
if(!t) break;
if(!want_task(t)) continue;
ttab[n_task++] = (proc_t*)(n_used++);
@ -1533,7 +1534,7 @@ proc_data_t *readproctab3 (int(*want_task)(proc_t *buf), PROCTAB *restrict const
}
// let this next guy allocate the necessary proc_t storage
// (or recycle it) since he can't tolerate realloc relocations
if (!(p = readeither_direct(PT,p))) break;
if (!(p = readeither(PT,p))) break;
if (want_task(p)) {
tab[n_used++] = p;
p = NULL;

View File

@ -10,8 +10,7 @@
// in the file COPYING
#include <proc/procps.h>
#include <proc/pwcache.h>
// #include <proc/pwcache.h>
#include <proc/namespace.h>
#define SIGNAL_STRING
@ -93,7 +92,7 @@ typedef struct proc_t {
sigcatch, // status mask of caught signals
_sigpnd; // status mask of PER TASK pending signals
#endif
unsigned KLONG
unsigned long
start_code, // stat address of beginning of code segment
end_code, // stat address of end of code segment
start_stack, // stat address of the bottom of stack for the process

View File

@ -22,6 +22,8 @@
#ifndef _PROC_SLAB_H
#define _PROC_SLAB_H
#include <features.h>
__BEGIN_DECLS
enum slabs_item {

View File

@ -173,34 +173,36 @@ unsigned int getslabinfo (struct slab_cache **slab){
return cSlab;
}
///////////////////////////////////////////////////////////////////////////
#define PROCFS_PID_MAX "/proc/sys/kernel/pid_max"
#define DEFAULT_PID_LENGTH 5
unsigned get_pid_digits(void){
char pidbuf[24];
char *endp;
long rc;
int fd;
static unsigned ret;
/*
* procps_pid_length
*
* Return the length of the maximum possible pid.
*
* Returns either the strlen of PROCFS_PID_MAX or the
* best-guess DEFAULT_PID_LENGTH
*/
PROCPS_EXPORT unsigned int procps_pid_length(void)
{
FILE *fp;
char pidbuf[24];
char *endp;
unsigned long int max_pid;
static int pid_length=0;
if(ret) goto out;
ret = 5;
fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
if(fd==-1) goto out;
rc = read(fd, pidbuf, sizeof pidbuf);
close(fd);
if(rc<3) goto out;
pidbuf[rc] = '\0';
rc = strtol(pidbuf,&endp,10);
if(rc<42) goto out;
if(*endp && *endp!='\n') goto out;
rc--; // the pid_max value is really the max PID plus 1
ret = 0;
while(rc){
rc /= 10;
ret++;
}
out:
return ret;
if (pid_length)
return pid_length;
pid_length = DEFAULT_PID_LENGTH;
if ((fp = fopen(PROCFS_PID_MAX, "r")) != NULL) {
if (fgets(buf, 24, fp) != NULL) {
pid_length = strlen(buf);
}
fclose(fp);
}
return pid_length;
}
///////////////////////////////////////////////////////////////////////////

View File

@ -2,8 +2,8 @@
#define PROC_SYSINFO_H
#include <sys/types.h>
#include <dirent.h>
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS
extern int have_privs; /* boolean, true if setuid or similar */
@ -11,6 +11,7 @@ extern int have_privs; /* boolean, true if setuid or similar */
long procps_cpu_count(void);
long procps_hertz_get(void);
int procps_loadavg(double *av1, double *av5, double *av15);
unsigned int procps_pid_length(void);
#define BUFFSIZE (64*1024)
typedef unsigned long long jiff;
@ -25,7 +26,6 @@ typedef struct slab_cache{
extern unsigned int getslabinfo (struct slab_cache**);
extern unsigned get_pid_digits(void) FUNCTION;
__END_DECLS
#endif /* SYSINFO_H */

View File

@ -25,8 +25,7 @@
#ifndef PROC_UPTIME_H
#define PROC_UPTIME_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS
int procps_uptime(double *uptime_secs, double *idle_secs);

View File

@ -23,8 +23,7 @@
#ifndef PROC_VERSION_H
#define PROC_VERSION_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS
int procps_linux_version(void);

View File

@ -23,7 +23,7 @@
#ifndef PROC_VMSTAT_H
#define PROC_VMSTAT_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS

View File

@ -1,7 +1,7 @@
#ifndef PROCPS_PROC_WCHAN_H
#define PROCPS_PROC_WCHAN_H
#include <proc/procps.h>
#include <features.h>
__BEGIN_DECLS