Skip to content

Commit

Permalink
Merge pull request #1 from embeddedTS/integrate-procps
Browse files Browse the repository at this point in the history
Integrate procps directly
  • Loading branch information
ts-kris authored Sep 16, 2024
2 parents 17b097b + 3027a72 commit 96c21c9
Show file tree
Hide file tree
Showing 14 changed files with 3,174 additions and 23 deletions.
517 changes: 498 additions & 19 deletions LICENSE

Large diffs are not rendered by default.

12 changes: 9 additions & 3 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)
project('idleinject', 'c', version : run_command('./script/getver.sh', check : false).stdout().strip())
idleinject_files = files(
'src/idleinject.c',
'src/alloc.c',
'src/escape.c',
'src/pwcache.c',
'src/readproc.c',
)
executable('idleinject', idleinject_files, install : true)
2 changes: 2 additions & 0 deletions script/getver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
git describe --exact-match --tags 2>/dev/null || git rev-parse --short HEAD
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 96c21c9

Please sign in to comment.