]> granicus.if.org Git - psmisc/commitdiff
adding plog
authorVito Mule <mulevito@gmail.com>
Thu, 28 Apr 2016 17:30:41 +0000 (18:30 +0100)
committerVito Mule <mulevito@gmail.com>
Sat, 30 Apr 2016 09:21:14 +0000 (10:21 +0100)
AUTHORS
Makefile.am
README.md
doc/pslog.1 [new file with mode: 0644]
src/pslog.c [new file with mode: 0644]
testsuite/Makefile.am
testsuite/pslog.test/pslog.exp [new file with mode: 0644]

diff --git a/AUTHORS b/AUTHORS
index 5114799e42d899934a8138f8e5036b704483532d..02fb2beca94fe3ac67591822584cb78b5afda576 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,3 +1,4 @@
 Werner Almesberger <werner@almesberger.net>
 Craig Small <csmall@small.dropbear.id.au>
 peekfd written by Trent Waddington <trent.waddington@gmail.com>
+pslog written by Vito Mule' <mule.vito@gmail.com>
index 003420b2b574d0b4762311b2dee6b137e411e447..412ca2b96ebd77969b813459c09306a8898bcdba 100644 (file)
@@ -17,6 +17,7 @@ SUBDIRS = \
 
 bin_PROGRAMS = \
               src/killall \
+              src/pslog \
               src/prtstat \
               src/pstree
 
@@ -64,6 +65,7 @@ src_fuser_LDADD = @LIBINTL@
 src_killall_SOURCES = src/killall.c src/comm.h src/signals.c src/signals.h src/i18n.h
 src_killall_LDADD = @LIBINTL@ @SELINUX_LIB@
 src_peekfd_SOURCES = src/peekfd.c
+src_pslog_SOURCES = src/pslog.c
 src_pstree_SOURCES = src/pstree.c src/comm.h src/i18n.h
 src_pstree_LDADD = @LIBINTL@ @TERMCAP_LIB@ @SELINUX_LIB@
 src_prtstat_SOURCES = src/prtstat.c src/prtstat.h
@@ -72,7 +74,7 @@ nodist_src_killall_SOURCES = signames.h
 
 BUILT_SOURCES = src/signames.h
 
-EXTRA_DIST = src/signames.c 
+EXTRA_DIST = src/signames.c
 
 CLEANFILES = src/signames.h
 
index cf22edd6a54f883412871192d37a641c6288b735..783c19a793362f7b11bfb3c258e4f45407cbb3fd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -6,7 +6,8 @@ A package of small utilities that use the proc file-system.
 
 * *fuser* - Identifies processes using files or sockets
 * *killall* - kills processes by name, e.g. killall -HUP named
-* *prtstat* - print statistics of a process
+* *prtstat* - prints statistics of a process
+* *pslog* - prints log path(s) of a process
 * *pstree* - shows the currently running processes as a tree
 * *peekfd* - shows the data travelling over a file descriptor
 
@@ -25,7 +26,7 @@ fuser doesn't hang, but it is much slower.
 ### Translations
 My thanks for the various translators who have cheerfully given me the po
 files to make psmisc speak different languages.  If your language is not
-supported then let me know, all it takes is translating one file in 
+supported then let me know, all it takes is translating one file in
 a certain manner.
 
 ### Icons
diff --git a/doc/pslog.1 b/doc/pslog.1
new file mode 100644 (file)
index 0000000..5a09997
--- /dev/null
@@ -0,0 +1,36 @@
+'\" t
+.\" (The preceding line is a note to broken versions of man to tell
+.\" them to pre-process this man page with tbl)
+.\" Man page for pwdx
+.\" Licensed under version 2 of the GNU General Public License.
+.\" Copyright 2015 Vito Mule’.
+.\" Based on the pwdx(1) man page by Nicholas Miell.
+.\"
+.TH PSLOG 1 "September 12, 2015” "Linux" "Linux User's Manual"
+.SH NAME
+pslog\- report current logs path of a process
+
+.SH SYNOPSIS
+.nf
+pslog pid...
+pslog -V
+.fi
+
+.SH DESCRIPTION
+The pslog command reports the current working logs of a process.
+
+.SH "GENERAL OPTIONS"
+.TS
+l l l.
+-V  show version  Displays version of program.
+.TE
+
+.SH "SEE ALSO"
+ps(1) pgrep(1) pwdx(1)
+
+.SH STANDARDS
+No standards apply.
+
+.SH AUTHOR
+Vito Mule’ <mulevito@gmail.com> wrote pslog in 2015. Please send bug
+reports to  <mulevito@gmail.com>.
diff --git a/src/pslog.c b/src/pslog.c
new file mode 100644 (file)
index 0000000..f0aa83c
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * pslog.c - print process log paths.
+ *
+ * Copyright (C) 2015 Vito Mule'
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include <dirent.h>
+#include <errno.h>
+#include <limits.h>
+#include <regex.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "i18n.h"
+
+
+static int
+usage ()
+{
+   fprintf(stderr,
+    "Usage: pslog PID...\n"
+    "       pslog -V, --version\n\n"
+
+    "  -V,--version display version information\n\n");
+  exit(255);
+}
+
+void
+print_version()
+{
+  fprintf(stderr, "pslog (PSmisc) %s\n", VERSION);
+  fprintf(stderr,
+    "Copyright (C) 2015-2016 Vito Mule'.\n\n");
+  fprintf(stderr,
+    "PSmisc comes with ABSOLUTELY NO WARRANTY.\n"
+    "This is free software, and you are welcome to redistribute it under\n"
+    "the terms of the GNU General Public License.\n"
+    "For more information about these matters, see the files named COPYING.\n");
+}
+
+int
+main(int argc, char const *argv[])
+{
+  regex_t re_log;
+  regex_t re_pid;
+
+  if (argc < 2) {
+    usage();
+  }
+
+  /*
+   * Allowed on the command line:
+   * --version
+   *  -V
+   *  /proc/nnnn
+   *  nnnn
+   *  where nnnn is any number that doesn't begin with 0.
+   *  If --version or -V are present, further arguments are ignored
+   *  completely.
+   */
+
+  regcomp(&re_pid, "^((/proc/+)?[1-9][0-9]*|-V|--version)$",
+          REG_EXTENDED|REG_NOSUB);
+
+  if (regexec(&re_pid, argv[1], 0, NULL, 0) != 0) {
+     fprintf(stderr, "pslog: invalid process id: %s\n\n", argv[1]);
+     usage();
+  }
+  else if (!strcmp("-V", argv[1]) || !strcmp("--version", argv[1])) {
+    print_version();
+    return 0;
+  }
+
+  regfree(&re_pid);
+  regcomp(&re_log, "^(.*log)$",REG_EXTENDED|REG_NOSUB);
+
+  /*
+   * At this point, all arguments are in the form /proc/nnnn
+   * or nnnn, so a simple check based on the first char is
+   * possible.
+   */
+
+  struct dirent *namelist;
+
+  char* fullpath = (char*) malloc(PATH_MAX+1);
+  if (!fullpath) {
+    perror ("malloc");
+    return 1;
+  }
+
+  char* linkpath = (char*) malloc(PATH_MAX+1);
+  if (!linkpath) {
+    perror ("malloc");
+    return 1;
+  }
+
+  char buf[PATH_MAX+1];
+  DIR *pid_dir;
+
+  if (argv[1][0] != '/') {
+    strncpy(fullpath, "/proc/", PATH_MAX);
+    strncat(fullpath, argv[1], PATH_MAX - strlen(fullpath));
+    strncat(fullpath, "/fd/", PATH_MAX - strlen(fullpath));
+  } else {
+      strncpy(fullpath, argv[1], PATH_MAX);
+      strncat(fullpath, "/fd/", PATH_MAX - strlen(fullpath));
+    }
+
+  pid_dir = opendir(fullpath);
+  if (!pid_dir) {
+    perror("opendir");
+    return 1;
+  }
+
+  fprintf(stdout, "Pid no %s:\n", argv[1]);
+
+  while((namelist = readdir(pid_dir))) {
+    strncpy(linkpath, fullpath, PATH_MAX);
+    strncat(linkpath, namelist->d_name, PATH_MAX - strlen(linkpath));
+    readlink(linkpath, buf, PATH_MAX -1);
+
+    if (regexec(&re_log, buf, 0, NULL, 0) == 0) {
+      fprintf(stdout, "Log path: %s\n", buf);
+    }
+    memset(&linkpath[0], 0, sizeof(*linkpath));
+    memset(&buf[0], 0, sizeof(buf));
+  }
+
+  free(linkpath);
+  free(fullpath);
+  regfree(&re_log);
+  return 0;
+}
index e9bf8a4915faaceae2c2e362457828f1d41b7e02..696a44a0dc9d142302bc1f20e19ff3d3ca76f072 100644 (file)
@@ -2,11 +2,12 @@ AUTOMAKE_OPTIONS = dejagnu
 export DEJAGNU
 
 # Programs that are expected across the board.
-DEJATOOL = killall 
+DEJATOOL = killall
+DEJATOOL += pslog
 
 if WANT_FUSER
   DEJATOOL += fuser
 endif
 
 
-EXTRA_DIST = 
+EXTRA_DIST =
diff --git a/testsuite/pslog.test/pslog.exp b/testsuite/pslog.test/pslog.exp
new file mode 100644 (file)
index 0000000..060e963
--- /dev/null
@@ -0,0 +1,15 @@
+#
+# Testsuite for pslog
+#
+
+set pslog "${topdir}src/pslog"
+set fake_pid "27000"
+
+set test "pslog with no arguments"
+spawn $pslog
+expect_pass "$test" "^Usage: pslog PID\\.\\.\\."
+
+set test "pslog pid not found"
+spawn $pslog ${fake_pid}
+expect_pass "$test" "opendir: No such file or directory"
+