From: Vito Mule Date: Thu, 28 Apr 2016 17:30:41 +0000 (+0100) Subject: adding plog X-Git-Tag: v23.0rc1~12^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=28c26cbf2580a24c0ee014ad3579b3de36a825f2;p=psmisc adding plog --- diff --git a/AUTHORS b/AUTHORS index 5114799..02fb2be 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,3 +1,4 @@ Werner Almesberger Craig Small peekfd written by Trent Waddington +pslog written by Vito Mule' diff --git a/Makefile.am b/Makefile.am index 003420b..412ca2b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/README.md b/README.md index cf22edd..783c19a 100644 --- 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 index 0000000..5a09997 --- /dev/null +++ b/doc/pslog.1 @@ -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’ wrote pslog in 2015. Please send bug +reports to . diff --git a/src/pslog.c b/src/pslog.c new file mode 100644 index 0000000..f0aa83c --- /dev/null +++ b/src/pslog.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +} diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index e9bf8a4..696a44a 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -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 index 0000000..060e963 --- /dev/null +++ b/testsuite/pslog.test/pslog.exp @@ -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" +