Browse Source

Cloned from Jude Nelson's git repository.

release-1.0.2
Aitor 2 years ago
commit
c1feb209e4
  1. 28
      .gitignore
  2. 5
      CONTRIBUTORS
  3. 5
      LICENSE.ISC
  4. 166
      LICENSE.LGPLv3+
  5. 142
      Makefile
  6. 26
      README.md
  7. 92
      libpstat.c
  8. 86
      libpstat.h
  9. 12
      libpstat.pc.in
  10. 24
      os/common.c
  11. 26
      os/common.h
  12. 225
      os/linux.c
  13. 46
      os/linux.h
  14. 8
      pkg/Makefile
  15. 44
      pkg/pkg-deb.sh
  16. 24
      pkg/pkg-scripts/pkg-libpstat-deb.sh
  17. 24
      pkg/pkg-scripts/pkg-libpstat-dev-deb.sh
  18. 24
      pkg/pkg-scripts/pkg-libpstat-util-deb.sh
  19. 96
      tools/pstat.c
  20. 26
      tools/pstat.h

28
.gitignore

@ -0,0 +1,28 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app

5
CONTRIBUTORS

@ -0,0 +1,5 @@
Jude Nelson - main author
judecn@gmail.com
Kylie McClain - Makefile variable standardization
somasis@exherbo.org

5
LICENSE.ISC

@ -0,0 +1,5 @@
Copyright (c) 2014, Jude Nelson (judecn@gmail.com) et al.
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

166
LICENSE.LGPLv3+

@ -0,0 +1,166 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

142
Makefile

@ -0,0 +1,142 @@
ROOT_DIR := $(shell dirname "$(realpath $(lastword $(MAKEFILE_LIST)))")
BUILD ?= $(ROOT_DIR)/build
CC ?= cc
CFLAGS ?= -Wall -g -fPIC -std=c99
LIB :=
INC := -I. -I$(BUILD) -I$(BUILD)/include/pstat
C_SRCS:= $(wildcard *.c) $(wildcard os/*.c)
TOOL_C_SRCS := $(wildcard tools/*.c)
HEADERS := $(wildcard *.h) $(wildcard os/*.h)
OBJ := $(patsubst %.c,$(BUILD)/%.o,$(C_SRCS))
TOOL_OBJ := $(patsubst %.c,$(BUILD)/%.o,$(TOOL_C_SRCS))
DEFS := -D_THREAD_SAFE -D__STDC_FORMAT_MACROS
VERSION_MAJOR := 1
VERSION_MINOR := 0
VERSION_PATCH := 2
LIBPSTAT := libpstat.so
LIBPSTAT_SO := libpstat.so.$(VERSION_MAJOR)
LIBPSTAT_LIB := libpstat.so.$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)
PSTAT := tools/pstat
PREFIX ?= /usr/local
DESTDIR ?= /
INCLUDE_PREFIX ?= $(PREFIX)
BINDIR ?= $(DESTDIR)/$(PREFIX)/bin
LIBDIR ?= $(DESTDIR)/$(PREFIX)/lib
INCLUDE_DIR ?= $(DESTDIR)/$(INCLUDE_PREFIX)/include/pstat
PKGCONFIGDIR ?= $(DESTDIR)/$(PREFIX)/lib/pkgconfig
OS ?= LINUX
BUILD_LIBPSTAT_LIB := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT_LIB))
INSTALL_LIBPSTAT_LIB := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT_LIB))
BUILD_LIBPSTAT_SO := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT_SO))
INSTALL_LIBPSTAT_SO := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT_SO))
BUILD_LIBPSTAT := $(patsubst %,$(BUILD)/$(LIBDIR)/%,$(LIBPSTAT))
INSTALL_LIBPSTAT := $(patsubst $(BUILD)/$(LIBDIR)/%,$(LIBDIR)/%,$(BUILD_LIBPSTAT))
# NOTE: don't install os/ headers, since they're different for each OS.
BUILD_HEADERS := $(patsubst %,$(BUILD)/$(INCLUDE_DIR)/%,$(HEADERS))
INSTALL_HEADERS := $(patsubst $(BUILD)/$(INCLUDE_DIR)/%,$(INCLUDE_DIR)/%,$(BUILD_HEADERS))
BUILD_PC_FILE := $(BUILD)/libpstat.pc
INSTALL_PC_FILE := $(PKGCONFIGDIR)/libpstat.pc
BUILD_PSTAT := $(patsubst tools/%,$(BUILD)/$(BINDIR)/%,$(PSTAT))
INSTALL_PSTAT := $(patsubst $(BUILD)/$(BINDIR)/%,$(BINDIR)/%,$(BUILD_PSTAT))
all: tools libs
tools: $(BUILD_PSTAT)
libs: $(BUILD_HEADERS) $(BUILD_PC_FILE) $(BUILD_LIBPSTAT) $(BUILD_LIBPSTAT_SO) $(BUILD_LIBPSTAT_LIB)
$(BUILD_PC_FILE): libpstat.pc.in
@mkdir -p "$(shell dirname "$@")"
@cat $< | \
sed -e 's~@PREFIX@~$(PREFIX)~g;' | \
sed -e 's~@INCLUDEDIR@~$(INCLUDE_DIR)~g;' | \
sed -e 's~@VERSION@~$(VERSION)~g; ' | \
sed -e 's~@LIBS@~$(LIB)~g; ' | \
sed -e 's~@LIBDIR@~$(LIBDIR)~g; ' | \
sed -e 's~@VERSION_MAJOR@~$(VERSION_MAJOR)~g; ' | \
sed -e 's~@VERSION_MINOR@~$(VERSION_MINOR)~g; ' | \
sed -e 's~@VERSION_PATCH@~$(VERSION_PATCH)~g; ' > $@
$(BUILD_LIBPSTAT_LIB): $(OBJ)
@mkdir -p "$(shell dirname "$@")"
$(CC) $(CFLAGS) -shared -Wl,-soname,$(LIBPSTAT_SO) -o "$@" $(OBJ) $(LIBINC) $(LIB)
$(BUILD_LIBPSTAT_SO): $(BUILD_LIBPSTAT_LIB)
@mkdir -p "$(shell dirname "$@")"
@ln -sf "$(shell basename "$<")" "$@"
$(BUILD_LIBPSTAT): $(BUILD_LIBPSTAT_SO)
@mkdir -p "$(shell dirname "$@")"
@ln -sf "$(shell basename "$<")" "$@"
$(BUILD_PSTAT): $(TOOL_OBJ) $(BUILD_LIBPSTAT)
@mkdir -p "$(shell dirname "$@")"
$(CC) $(CFLAGS) -o "$@" $(TOOL_OBJ) $(LIBINC) -L$(BUILD)/$(LIBDIR) -lpstat
$(BUILD)/$(INCLUDE_DIR)/%.h: %.h
@mkdir -p "$(shell dirname "$@")"
@cp -a "$<" "$@"
$(INSTALL_LIBPSTAT): $(BUILD_LIBPSTAT)
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
$(INSTALL_LIBPSTAT_SO): $(BUILD_LIBPSTAT_SO)
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
$(INSTALL_LIBPSTAT_LIB): $(BUILD_LIBPSTAT_LIB)
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
$(INCLUDE_DIR)/%.h: $(BUILD)/$(INCLUDE_DIR)/%.h
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
$(INSTALL_PC_FILE): $(BUILD_PC_FILE)
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
$(INSTALL_PSTAT): $(BUILD_PSTAT)
@mkdir -p "$(shell dirname "$@")"
cp -a "$<" "$@"
libs-install: $(INSTALL_LIBPSTAT) $(INSTALL_LIBPSTAT_SO) $(INSTALL_LIBPSTAT_LIB)
tools-install: $(INSTALL_PSTAT)
headers-install: $(INSTALL_HEADERS)
install: libs-install tools-install headers-install
$(BUILD)/%.o : %.c
@mkdir -p "$(shell dirname "$@")"
$(CC) $(CFLAGS) -o "$@" $(INC) -c "$<" $(DEFS) -D_$(OS)
.PHONY: clean
clean:
rm -f $(BUILD_HEADERS) $(BUILD_LIBPSTAT_LIB) $(BUILD_LIBPSTAT_SO) $(BUILD_LIBPSTAT) $(BUILD_PC_FILE) $(BUILD_HEADERS) $(BUILD_PSTAT) $(OBJ)
.PHONY: uninstall
uninstall:
rm -f $(INSTALL_HEADERS) $(INSTALL_LIBPSTAT_LIB) $(INSTALL_LIBPSTAT_SO) $(INSTALL_LIBPSTAT) $(INSTALL_PC_FILE) $(INSTALL_PSTAT)
print-%: ; @echo $*=$($*)

26
README.md

@ -0,0 +1,26 @@
libpstat: OS-agnostic process stat
==================================
libpstat is a small library for getting information on running processes in an OS-agnostic way. The source is structured to make it easy to add support for additional operating systems. Currently, Linux is the only supported OS.
Building
--------
To build:
$ make OS=$OS_NAME
Substitute $OS_NAME for "LINUX".
Installing
----------
To install libpstat to /lib and headers to /usr/include/pstat:
$ sudo make install PREFIX=/ INCLUDE_PREFIX=/usr
**Advanced:** If you want to install libpstat's headers to a custom location, you can do so by setting the `INCLUDE_DIR` variable instead of the `INCLUDE_PREFIX` variable. For example, passing `INCLUDE_DIR=/custom/location/` would install the headers directly to `/custom/location`, whereas `INCLUDE_PREFIX=/custom/location` would install the headers to `/custom/location/include/pstat`.
Documentation
-------------
TODO

92
libpstat.c

@ -0,0 +1,92 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "libpstat.h"
#ifdef _LINUX
#include "os/linux.h"
#endif
#ifdef _UNKNOWN
#error "Undefined OS target"
#endif
// insert more OSs here
uint64_t pstat_supported_features() {
return pstat_os_supported_features();
}
// allocate a new pstat
struct pstat* pstat_new() {
return (struct pstat*)calloc( sizeof(struct pstat), 1 );
}
// free a pstat
void pstat_free( struct pstat* ps ) {
free( ps );
}
// get the status of a running processes
// return 0 on success
// return negative on error
int pstat( pid_t pid, struct pstat* ps, int flags ) {
return pstat_os( pid, ps, flags );
}
pid_t pstat_get_pid( struct pstat* ps ) {
return ps->pid;
}
bool pstat_is_running( struct pstat* ps ) {
return ps->running;
}
bool pstat_is_deleted( struct pstat* ps ) {
return ps->deleted;
}
int pstat_get_path( struct pstat* ps, char* pathbuf ) {
if( pathbuf != NULL ) {
strcpy( pathbuf, ps->path );
}
return (int)strlen( ps->path );
}
int pstat_get_stat( struct pstat* ps, struct stat* sb ) {
if( !ps->running ) {
return -ENODATA;
}
memcpy( sb, &ps->bin_stat, sizeof(struct stat) );
return 0;
}
uint64_t pstat_get_starttime( struct pstat* ps ) {
return ps->starttime;
}

86
libpstat.h

@ -0,0 +1,86 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#ifndef _LIBPSTAT_H_
#define _LIBPSTAT_H_
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
// set of feature flags the implementation may support
#define LIBPSTAT_RUNNING 0x1 // implementation can tell that a process is running
#define LIBPSTAT_BINARY 0x2 // implementation can tell the path and status a process's exec'ed binary image
#define LIBPSTAT_STARTTIME 0x4 // implementation can tell when the process started
#ifdef __cplusplus
extern "C" {
#endif
// process status structure
struct pstat;
// features
uint64_t pstat_supported_features();
// memory management
struct pstat* pstat_new();
void pstat_free( struct pstat* ps );
// stat a running process.
// return 0 on success, which is qualified to mean "We could fill in at least one pstat field, besides the PID"
// return -errno on error (specific to the OS)
int pstat( pid_t pid, struct pstat* ps, int flags );
// query a pstat structure...
pid_t pstat_get_pid( struct pstat* ps );
// if LIBPSTAT_RUNNING is supported
bool pstat_is_running( struct pstat* ps );
// if LIBPSTAT_BINARY is supported
bool pstat_is_deleted( struct pstat* ps );
int pstat_get_path( struct pstat* ps, char* pathbuf );
int pstat_get_stat( struct pstat* ps, struct stat* sb );
// if LIBPSTAT_STARTTIME is supported
uint64_t pstat_get_starttime( struct pstat* ps );
#ifdef __cplusplus
}
#endif
#endif

12
libpstat.pc.in

@ -0,0 +1,12 @@
prefix=@PREFIX@
exec_prefix=${prefix}
libdir=@LIBDIR@
includedir=@INCLUDEDIR@
Name: pstat
Description: Process status library
Version: @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@
Requires:
Libs: -lpstat @LIBS@
Cflags: -I${includedir}

24
os/common.c

@ -0,0 +1,24 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "common.h"
// put anything common to OSs here

26
os/common.h

@ -0,0 +1,26 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#ifndef _PSTAT_OS_COMMON_H_
#define _PSTAT_OS_COMMON_H_
#include "libpstat.h"
#endif

225
os/linux.c

@ -0,0 +1,225 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
// Linux-specific implementation
#ifdef _LINUX
#include "linux.h"
uint64_t pstat_os_supported_features() {
return LIBPSTAT_RUNNING | LIBPSTAT_BINARY | LIBPSTAT_STARTTIME;
}
static ssize_t read_uninterrupted( int fd, char* buf, size_t len ) {
ssize_t num_read = 0;
while( (unsigned)num_read < len ) {
ssize_t nr = read( fd, buf + num_read, len - num_read );
if( nr < 0 ) {
int errsv = -errno;
if( errsv == -EINTR ) {
continue;
}
return errsv;
}
if( nr == 0 ) {
break;
}
num_read += nr;
}
return num_read;
}
// stat a running process on linux
// return 0 on success, which is qualified to mean "We could fill in at least one field, besides the PID"
// return -errno on error (due to open(), readlink(), or fstat())
// return -EIO in particular if we could not parse /proc/[PID]/stat
int pstat_os( pid_t pid, struct pstat* ps, int flags ) {
int rc = 0;
char path[PATH_MAX+1];
char bin_path[PATH_MAX+1];
char stat_buf[ 8192 ];
int fd = 0;
ssize_t nr = 0;
size_t deleted_len = strlen( " (deleted)" );
// fields in /proc/[PID]/stat. We want the starttime (which is the 22nd field)
char* stat_ptr = NULL;
int num_paren = 0;
bool paren_found = false;
int field_count = 0;
uint64_t starttime = 0;
memset( path, 0, PATH_MAX+1 );
memset( bin_path, 0, PATH_MAX+1 );
memset( stat_buf, 0, 8192 );
memset( ps, 0, sizeof(struct pstat) );
ps->pid = pid;
sprintf( path, "/proc/%d/exe", pid );
// open the process binary, if we can
fd = open( path, O_RDONLY );
if( fd < 0 ) {
rc = -errno;
// not running?
if( rc == -ENOENT ) {
ps->running = false;
return 0;
}
// something else
return rc;
}
// the fact that this path exists means that it's running
ps->running = true;
// verify that it isn't deleted
nr = readlink( path, bin_path, PATH_MAX );
if( nr < 0 ) {
rc = -errno;
close( fd );
return rc;
}
// on Linux, if the bin_path ends in " (deleted)", we're guaranteed that this binary no longer exists.
if( strlen( bin_path ) > strlen( " (deleted)") && strcmp( bin_path + strlen(bin_path) - deleted_len, " (deleted)" ) == 0 ) {
ps->deleted = true;
close( fd );
return 0;
}
rc = fstat( fd, &ps->bin_stat );
close( fd );
if( rc < 0 ) {
rc = -errno;
return rc;
}
ps->deleted = false;
strncpy( ps->path, bin_path, PATH_MAX );
// on Linux, we can see when the process started. Fill this in from /proc/[PID]/stat
sprintf( path, "/proc/%d/stat", pid );
fd = open( path, O_RDONLY );
if( fd < 0 ) {
rc = -errno;
// not running?
if( rc == -ENOENT ) {
ps->running = false;
return 0;
}
return rc;
}
rc = read_uninterrupted( fd, stat_buf, 8191 );
if( rc < 0 ) {
return rc;
}
close( fd );
rc = 0;
// read ahead on stat_buf to the end of the second field (the binary path, in parentheses)
stat_ptr = stat_buf;
while( *stat_ptr != '\0' ) {
if( *stat_ptr == '(' ) {
num_paren++;
paren_found = true;
}
else if( *stat_ptr == ')' ) {
num_paren--;
}
if( num_paren == 0 && paren_found ) {
// end of the second field
stat_ptr++;
break;
}
stat_ptr++;
}
// read ahead to the 22nd field
field_count = 3;
while( field_count < 22 ) {
while( *stat_ptr == ' ' ) {
stat_ptr++;
}
if( *stat_ptr == '\0' ) {
// not found
rc = -EIO;
break;
}
field_count++;
while( *stat_ptr != ' ' ) {
stat_ptr++;
}
if( *stat_ptr == '\0' ) {
// not found
rc = -EIO;
break;
}
}
while( *stat_ptr == ' ' ) {
stat_ptr++;
}
starttime = (uint64_t)strtoull( stat_ptr, NULL, 10 );
if( starttime == 0 ) {
// failed to parse
rc = -EIO;
return rc;
}
ps->starttime = starttime;
return rc;
}
#endif // #define _LINUX

46
os/linux.h

@ -0,0 +1,46 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#ifndef _PSTAT_OS_LINUX_H_
#define _PSTAT_OS_LINUX_H_
#define _BSD_SOURCE
#include "common.h"
#include <unistd.h>
#include <fcntl.h>
// this is private
struct pstat {
pid_t pid;
bool running; // whether or not the process is running. If this is false, then the fields beneath will not be initialized
char path[PATH_MAX+1]; // path to the process. set to NULL if not initialized
bool deleted; // whether or not the process binary is deleted (in which case, proc_stat will be uninitialized)
struct stat bin_stat; // stat(2) result from the binary path
uint64_t starttime; // time (in jiffies) when this process started
};
uint64_t pstat_os_supported_features();
int pstat_os( pid_t pid, struct pstat* ps, int flags );
#endif

8
pkg/Makefile

@ -0,0 +1,8 @@
all: deb
deb:
./pkg-deb.sh
.PHONY: clean
clean:
/bin/rm -f *.deb

44
pkg/pkg-deb.sh

@ -0,0 +1,44 @@
#!/bin/bash
FSKIT_DIR=../
BUILD=./build
SCRIPTS=./pkg-scripts
build_package() {
local dir=$1
local build_target=$2
local install_target=$3
local package_script=$4
local install_prefix=$5
local install_dir=$BUILD/$target
rm -rf $install_dir
mkdir $install_dir
if [ -n $build_target ]; then
make -C $dir $build_target
fi
if [ -n $install_target ]; then
mkdir -p $install_dir/$install_prefix
make -C $dir $install_target PREFIX=$(realpath $install_dir/$install_prefix)
fi
if [ -n $package_script ]; then
$SCRIPTS/$package_script $install_dir
fi
}
rm -rf $BUILD
# build libpstat
build_package ../ libpstat libpstat-install pkg-libpstat-deb.sh /usr/local
# build libpstat-dev
build_package ../ "" libpstat-dev-install pkg-libpstat-dev-deb.sh /usr/local
# build libpstat-util
build_package ../ pstat pstat-install pkg-libpstat-util-deb.sh /usr/local
rm -rf $BUILD

24
pkg/pkg-scripts/pkg-libpstat-deb.sh

@ -0,0 +1,24 @@
#!/bin/bash
if ! [ $1 ]; then
echo "Usage: $0 PACKAGE_ROOT"
exit 1
fi
ROOT=$1
NAME="libpstat"
VERSION="0.$(date +%Y\%m\%d\%H\%M\%S)"
DEPS="libc6 libssl1.0.0"
DEPARGS=""
for pkg in $DEPS; do
DEPARGS="$DEPARGS -d $pkg"
done
source /usr/local/rvm/scripts/rvm
rm -f $NAME-0*.deb
fpm --force -s dir -t deb -a $(uname -m) -v $VERSION -n $NAME $DEPARGS -C $ROOT --license "LGPLv3+/ISC" --maintainer "Jude Nelson <judecn@gmail.com>" --url "https://github.com/jcnelson/libpstat" --description "Process status library." $(ls $ROOT)

24
pkg/pkg-scripts/pkg-libpstat-dev-deb.sh

@ -0,0 +1,24 @@
#!/bin/bash
if ! [ $1 ]; then
echo "Usage: $0 PACKAGE_ROOT"
exit 1
fi
ROOT=$1
NAME="libpstat-dev"
VERSION="0.$(date +%Y\%m\%d\%H\%M\%S)"
DEPS="libc6 libpstat"
DEPARGS=""
for pkg in $DEPS; do
DEPARGS="$DEPARGS -d $pkg"
done
source /usr/local/rvm/scripts/rvm
rm -f $NAME-0*.deb
fpm --force -s dir -t deb -a $(uname -m) -v $VERSION -n $NAME $DEPARGS -C $ROOT --license "LGPLv3+/ISC" --maintainer "Jude Nelson <judecn@gmail.com>" --url "https://github.com/jcnelson/libpstat" --description "Process status library development files." $(ls $ROOT)

24
pkg/pkg-scripts/pkg-libpstat-util-deb.sh

@ -0,0 +1,24 @@
#!/bin/bash
if ! [ $1 ]; then
echo "Usage: $0 PACKAGE_ROOT"
exit 1
fi
ROOT=$1
NAME="libpstat-util"
VERSION="0.$(date +%Y\%m\%d\%H\%M\%S)"
DEPS="libpstat"
DEPARGS=""
for pkg in $DEPS; do
DEPARGS="$DEPARGS -d $pkg"
done
source /usr/local/rvm/scripts/rvm
rm -f $NAME-0*.deb
fpm --force -s dir -t deb -a $(uname -m) -v $VERSION -n $NAME $DEPARGS -C $ROOT --license "LGPLv3+/ISC" --maintainer "Jude Nelson <judecn@gmail.com>" --url "https://github.com/jcnelson/libpstat" --description "Process status library utilities." $(ls $ROOT)

96
tools/pstat.c

@ -0,0 +1,96 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "pstat.h"
void usage( char const* prog_name ) {
fprintf(stderr, "Usage: %s PID [PID...]\n", prog_name);
exit(1);
}
void print_pstat( struct pstat* ps ) {
struct stat bin_stat;
char path[PATH_MAX+1];
printf("PSTAT_PID=%d\n", pstat_get_pid( ps ) );
printf("PSTAT_RUNNING=%s\n", pstat_is_running( ps ) ? "1" : "0" );
if( !pstat_is_running( ps ) ) {
return;
}
if( !pstat_is_deleted( ps ) ) {
pstat_get_stat( ps, &bin_stat );
pstat_get_path( ps, path );
printf("PSTAT_BINARY=\"%s\"\n", path );
printf("PSTAT_DELETED=%s\n", pstat_is_deleted( ps ) ? "1": "0" );
printf("PSTAT_INODE=%ld\n", bin_stat.st_ino );
printf("PSTAT_SIZE=%ld\n", (long)bin_stat.st_size );
printf("PSTAT_MODTIME=%ld\n", bin_stat.st_mtime );
printf("PSTAT_STARTTIME=%" PRIu64 "\n", pstat_get_starttime( ps ) );
}
else {
printf("PSTAT_DELETED=1\n");
}
}
int main( int argc, char** argv ) {
pid_t pid = 0;
int rc = 0;
struct pstat* ps = pstat_new();
if( ps == NULL ) {
exit(1);
}
if( argc <= 1 ) {
usage( argv[0] );
}
for( int i = 1; i < argc; i++ ) {
pid = 0;
rc = sscanf( argv[i], "%d", &pid );
if( rc != 1 ) {
usage( argv[0] );
}
rc = pstat( pid, ps, 0 );
if( rc != 0 ) {
fprintf( stderr, "pstat(%d) rc = %d\n", pid, rc );
exit(1);
}
print_pstat( ps );
}
pstat_free( ps );
return 0;
}

26
tools/pstat.h

@ -0,0 +1,26 @@
/*
libpstat: a library for getting information about running processes
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU Lesser General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#ifndef _PSTAT_H_
#define _PSTAT_H_
#include "libpstat.h"
#endif
Loading…
Cancel
Save