Index: trunk/tools/slayerd/proc_solaris.cc |
— | — | @@ -0,0 +1,88 @@ |
| 2 | +/* Copyright (c) 2007-2009 River Tarnell <river@loreley.flyingparchment.org.uk>. */ |
| 3 | +/* |
| 4 | + * Permission is granted to anyone to use this software for any purpose, |
| 5 | + * including commercial applications, and to alter it and redistribute it |
| 6 | + * freely. This software is provided 'as-is', without any express or implied |
| 7 | + * warranty. |
| 8 | + */ |
| 9 | + |
| 10 | +/* $Id$ */ |
| 11 | + |
| 12 | +#include <sys/stat.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <dirent.h> |
| 15 | +#include <fcntl.h> |
| 16 | +#include <procfs.h> |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <iostream> |
| 20 | +#include <fstream> |
| 21 | +#include <boost/shared_ptr.hpp> |
| 22 | +#include <boost/filesystem.hpp> |
| 23 | +#include <boost/lexical_cast.hpp> |
| 24 | +#include <boost/format.hpp> |
| 25 | + |
| 26 | +#include "process.h" |
| 27 | + |
| 28 | +struct process_solaris : process { |
| 29 | + pid_t pid() const { return _pid; } |
| 30 | + std::string command() const { return _comm; } |
| 31 | + std::string cmdline() const { return _fullcomm; } |
| 32 | + std::size_t rss() const { return _rss; } |
| 33 | + std::size_t vsize() const { return _vsize; } |
| 34 | + uid_t uid() const { return _uid; } |
| 35 | + |
| 36 | + pid_t _pid; |
| 37 | + std::string _comm; |
| 38 | + std::string _fullcomm; |
| 39 | + std::size_t _vsize; |
| 40 | + uid_t _uid; |
| 41 | + std::size_t _rss; |
| 42 | +}; |
| 43 | + |
| 44 | +std::vector<process::pointer> |
| 45 | +enumerate_processes() |
| 46 | +{ |
| 47 | + DIR *dir; |
| 48 | + dirent *dent; |
| 49 | + std::vector<process::pointer> processes; |
| 50 | + int pagesz = sysconf(_SC_PAGE_SIZE); |
| 51 | + |
| 52 | + if ((dir = opendir("/proc")) == NULL) |
| 53 | + throw std::runtime_error("opendir(/proc) failed"); |
| 54 | + |
| 55 | + while ((dent = readdir(dir)) != NULL) { |
| 56 | + int fd; |
| 57 | + psinfo_t info; |
| 58 | + std::string infopath = boost::io::str(boost::format( |
| 59 | + "/proc/%s/psinfo") % dent->d_name); |
| 60 | + |
| 61 | + if ((fd = open(infopath.c_str(), O_RDONLY)) == -1) |
| 62 | + continue; |
| 63 | + |
| 64 | + retry: |
| 65 | + if (read(fd, (char *) &info, sizeof(info)) < 0) { |
| 66 | + close(fd); |
| 67 | + |
| 68 | + if (errno == EAGAIN) |
| 69 | + goto retry; |
| 70 | + |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + close(fd); |
| 75 | + |
| 76 | + process_solaris *p = new process_solaris; |
| 77 | + p->_pid = info.pr_pid; |
| 78 | + p->_uid = info.pr_uid; |
| 79 | + p->_rss = info.pr_rssize * 1024; |
| 80 | + p->_vsize = info.pr_size * 1024; |
| 81 | + p->_comm = info.pr_fname; |
| 82 | + p->_fullcomm = info.pr_psargs; |
| 83 | + |
| 84 | + processes.push_back(process::pointer(p)); |
| 85 | + } |
| 86 | + |
| 87 | + closedir(dir); |
| 88 | + return processes; |
| 89 | +} |
Index: trunk/tools/slayerd/daemon.c |
— | — | @@ -0,0 +1,65 @@ |
| 2 | +/* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ */ |
| 3 | + |
| 4 | +/*- |
| 5 | + * Copyright (c) 1990, 1993 |
| 6 | + * The Regents of the University of California. All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * 3. Neither the name of the University nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 21 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 24 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | + * SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +#include <fcntl.h> |
| 34 | +#include <stdlib.h> |
| 35 | +#include <unistd.h> |
| 36 | + |
| 37 | +int |
| 38 | +daemon(nochdir, noclose) |
| 39 | + int nochdir, noclose; |
| 40 | +{ |
| 41 | + int fd; |
| 42 | + |
| 43 | + switch (fork()) { |
| 44 | + case -1: |
| 45 | + return (-1); |
| 46 | + case 0: |
| 47 | + break; |
| 48 | + default: |
| 49 | + _exit(0); |
| 50 | + } |
| 51 | + |
| 52 | + if (setsid() == -1) |
| 53 | + return (-1); |
| 54 | + |
| 55 | + if (!nochdir) |
| 56 | + (void)chdir("/"); |
| 57 | + |
| 58 | + if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) { |
| 59 | + (void)dup2(fd, STDIN_FILENO); |
| 60 | + (void)dup2(fd, STDOUT_FILENO); |
| 61 | + (void)dup2(fd, STDERR_FILENO); |
| 62 | + if (fd > STDERR_FILENO) |
| 63 | + (void)close(fd); |
| 64 | + } |
| 65 | + return (0); |
| 66 | +} |
Index: trunk/tools/slayerd/slayerd.cc |
— | — | @@ -19,6 +19,8 @@ |
20 | 20 | #include <cerrno> |
21 | 21 | #include <cstring> |
22 | 22 | #include <csignal> |
| 23 | +#include <signal.h> |
| 24 | +#include <stdio.h> |
23 | 25 | #include <iostream> |
24 | 26 | #include <map> |
25 | 27 | #include <fstream> |
— | — | @@ -29,6 +31,10 @@ |
30 | 32 | #include "config.h" |
31 | 33 | #include "util.h" |
32 | 34 | |
| 35 | +#if defined(__sun) && defined(__SVR4) |
| 36 | +extern "C" int daemon(int, int); |
| 37 | +#endif |
| 38 | + |
33 | 39 | namespace { |
34 | 40 | std::string CONFFILE = "/etc/slayerd/slayerd.conf"; |
35 | 41 | } |
— | — | @@ -56,13 +62,13 @@ |
57 | 63 | ; |
58 | 64 | } |
59 | 65 | |
60 | | -void |
| 66 | +extern "C" void |
61 | 67 | rmpidfile(void) |
62 | 68 | { |
63 | 69 | unlink(config.pidfile.c_str()); |
64 | 70 | } |
65 | 71 | |
66 | | -void |
| 72 | +extern "C" void |
67 | 73 | do_signal(int sig) |
68 | 74 | { |
69 | 75 | rmpidfile(); |
— | — | @@ -91,9 +97,9 @@ |
92 | 98 | } |
93 | 99 | |
94 | 100 | pf << getpid() << '\n'; |
95 | | - atexit(rmpidfile); |
96 | | - signal(SIGINT, do_signal); |
97 | | - signal(SIGTERM, do_signal); |
| 101 | + std::atexit(rmpidfile); |
| 102 | + std::signal(SIGINT, do_signal); |
| 103 | + std::signal(SIGTERM, do_signal); |
98 | 104 | return true; |
99 | 105 | } |
100 | 106 | |
Index: trunk/tools/slayerd/process.cc |
— | — | @@ -10,6 +10,8 @@ |
11 | 11 | # include "proc_linux.cc" |
12 | 12 | #elif defined(__FreeBSD__) |
13 | 13 | # include "proc_freebsd.cc" |
| 14 | +#elif defined(__sun) && defined(__SVR4) |
| 15 | +# include "proc_solaris.cc" |
14 | 16 | #else |
15 | 17 | # error dont know how to enumerate processes on this platform |
16 | 18 | #endif |
Index: trunk/tools/slayerd/Makefile |
— | — | @@ -1,4 +1,4 @@ |
2 | | -VERSION = 1.7 |
| 2 | +VERSION = 1.8 |
3 | 3 | |
4 | 4 | PREFIX ?= /usr/local |
5 | 5 | CONFDIR ?= /etc/slayerd |
— | — | @@ -8,21 +8,34 @@ |
9 | 9 | |
10 | 10 | CXX = g++ |
11 | 11 | CXXFLAGS = -O0 -g3 -ggdb |
12 | | -CPPFLAGS = -DETCDIR=\"$(CONFDIR)\" -DPREFIX=\"$(PREFIX)\" -I/usr/local/include |
13 | | -LDFLAGS = -L/usr/local/lib |
14 | | -SRCS = slayerd.cc process.cc config.cc util.cc |
15 | | -OBJS = $(SRCS:.cc=.o) |
| 12 | +CPPFLAGS = -DETCDIR=\"$(CONFDIR)\" -DPREFIX=\"$(PREFIX)\" |
| 13 | +LDFLAGS = |
| 14 | +OBJS = slayerd.o process.o config.o util.o |
16 | 15 | LIBS = |
17 | 16 | |
18 | 17 | ifeq ($(shell uname),FreeBSD) |
19 | | -LIBS += -lkvm |
20 | | -GROUP = wheel |
| 18 | +CPPFLAGS += -I/usr/local/include |
| 19 | +LDFLAGS += -L/usr/local/lib |
| 20 | +LIBS += -lkvm |
| 21 | +GROUP = wheel |
21 | 22 | endif |
22 | 23 | |
| 24 | +ifeq ($(shell uname),SunOS) |
| 25 | +CC = cc |
| 26 | +CXX = CC |
| 27 | +CFLAGS = -m64 -xO4 -g |
| 28 | +CXXFLAGS = -m64 -xO4 -g -library=stlport4 |
| 29 | +LDFLAGS += -library=stlport4 |
| 30 | +CPPFLAGS += -I/opt/TSboost-1.38/include |
| 31 | +LDFLAGS += -L/opt/TSboost-1.38/lib/amd64 -R/opt/TSboost-1.38/lib/amd64 |
| 32 | +OBJS += daemon.o |
| 33 | +BOOST_TAG = -mt |
| 34 | +endif |
| 35 | + |
23 | 36 | all: slayerd |
24 | 37 | |
25 | 38 | slayerd: $(OBJS) |
26 | | - $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $@ -lboost_filesystem $(LIBS) |
| 39 | + $(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJS) -o $@ -lboost_filesystem$(BOOST_TAG) -lboost_system$(BOOST_TAG) $(LIBS) |
27 | 40 | |
28 | 41 | install: slayerd |
29 | 42 | install -d $(DESTDIR)$(SBINDIR) |
— | — | @@ -39,5 +52,6 @@ |
40 | 53 | clean: |
41 | 54 | rm -f slayerd *.o |
42 | 55 | |
43 | | -.PHONY: clean install |
| 56 | +.PHONY: clean install all |
44 | 57 | .SUFFICES: .cc .o |
| 58 | +process.o: process.cc proc_linux.cc proc_solaris.cc proc_freebsd.cc |