Skip to content

Commit

Permalink
Directly integrate parts of libproc to this codebase
Browse files Browse the repository at this point in the history
The 4.x release of libprocps, aka libproc2, no longer exposes API
calls that are needed by idleinject. The rationale from libprocps
developer was that those never should have been exposed and were
only intended for internal use by the library.

It was more straightforward to integrate the necessary sources to
this codebase than to modify the idleinject application to work with
the new library release. This was done as opposed to requiring the
older 3.x library version at runtime to make building this more
portable.

Because this now integrates LGPL code directly, the project license
from this point forward is now LGPL2.1 to match libprocps' library.

Signed-off-by: Kris Bahnsen <[email protected]>
  • Loading branch information
ts-kris committed Sep 13, 2024
1 parent 17b097b commit 038e9eb
Show file tree
Hide file tree
Showing 13 changed files with 3,163 additions and 22 deletions.
517 changes: 498 additions & 19 deletions LICENSE

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
project('idleinject', 'c')
procpsdep = dependency('libprocps')
executable('idleinject', 'src/idleinject.c', install : true, dependencies : procpsdep)
idleinject_files = files(
'src/idleinject.c',
'src/alloc.c',
'src/escape.c',
'src/pwcache.c',
'src/readproc.c',
)
executable('idleinject', idleinject_files, install : true)
96 changes: 96 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* alloc.c - memory allocation functions
* Copyright (C) 1992-1998 by Michael K. Johnson, [email protected]
* Copyright 2002 Albert Cahalan
*
* 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
*/

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "alloc.h"

static void xdefault_error(const char *restrict fmts, ...) __attribute__((format(printf,1,2)));
static void xdefault_error(const char *restrict fmts, ...) {
va_list va;

va_start(va, fmts);
vfprintf(stderr, fmts, va);
va_end(va);
}

message_fn xalloc_err_handler = xdefault_error;


void *xcalloc(size_t size) {
void * p;

if (size == 0)
++size;
p = calloc(1, size);
if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
return p;
}

void *xmalloc(size_t size) {
void *p;

if (size == 0)
++size;
p = malloc(size);
if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
return(p);
}

void *xrealloc(void *oldp, size_t size) {
void *p;

if (size == 0)
++size;
p = realloc(oldp, size);
if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
return(p);
}

char *xstrdup(const char *str) {
char *p = NULL;

if (str) {
size_t size = strlen(str) + 1;
if (size < 1) {
xalloc_err_handler("%s refused to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
p = malloc(size);
if (!p) {
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
exit(EXIT_FAILURE);
}
memcpy(p, str, size);
}
return(p);
}
20 changes: 20 additions & 0 deletions src/alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PROCPS_PROC_ALLOC_H
#define PROCPS_PROC_ALLOC_H

#include "procps.h"

EXTERN_C_BEGIN

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;

extern void *xcalloc(size_t size) MALLOC;
extern void *xmalloc(size_t size) MALLOC;
extern void *xrealloc(void *oldp, size_t size) MALLOC;
extern char *xstrdup(const char *str) MALLOC;

EXTERN_C_END

#endif
18 changes: 18 additions & 0 deletions src/devname.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef PROC_DEVNAME_H
#define PROC_DEVNAME_H

#include "procps.h"
#include "readproc.h"

EXTERN_C_BEGIN

#define ABBREV_DEV 1 /* remove /dev/ */
#define ABBREV_TTY 2 /* remove tty */
#define ABBREV_PTS 4 /* remove pts/ */

extern unsigned dev_to_tty(char *__restrict ret, unsigned chop, dev_t dev_t_dev, int pid, unsigned int flags);

extern int tty_to_dev(const char *__restrict const name);

EXTERN_C_END
#endif
Loading

0 comments on commit 038e9eb

Please sign in to comment.