r50327 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50326‎ | r50327 | r50328 >
Date:04:25, 8 May 2009
Author:river
Status:deferred
Tags:
Comment:
- 1.8
- support Solaris
Modified paths:
  • /trunk/tools/slayerd/Makefile (modified) (history)
  • /trunk/tools/slayerd/daemon.c (added) (history)
  • /trunk/tools/slayerd/proc_solaris.cc (added) (history)
  • /trunk/tools/slayerd/process.cc (modified) (history)
  • /trunk/tools/slayerd/slayerd.cc (modified) (history)

Diff [purge]

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 @@
2020 #include <cerrno>
2121 #include <cstring>
2222 #include <csignal>
 23+#include <signal.h>
 24+#include <stdio.h>
2325 #include <iostream>
2426 #include <map>
2527 #include <fstream>
@@ -29,6 +31,10 @@
3032 #include "config.h"
3133 #include "util.h"
3234
 35+#if defined(__sun) && defined(__SVR4)
 36+extern "C" int daemon(int, int);
 37+#endif
 38+
3339 namespace {
3440 std::string CONFFILE = "/etc/slayerd/slayerd.conf";
3541 }
@@ -56,13 +62,13 @@
5763 ;
5864 }
5965
60 -void
 66+extern "C" void
6167 rmpidfile(void)
6268 {
6369 unlink(config.pidfile.c_str());
6470 }
6571
66 -void
 72+extern "C" void
6773 do_signal(int sig)
6874 {
6975 rmpidfile();
@@ -91,9 +97,9 @@
9298 }
9399
94100 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);
98104 return true;
99105 }
100106
Index: trunk/tools/slayerd/process.cc
@@ -10,6 +10,8 @@
1111 # include "proc_linux.cc"
1212 #elif defined(__FreeBSD__)
1313 # include "proc_freebsd.cc"
 14+#elif defined(__sun) && defined(__SVR4)
 15+# include "proc_solaris.cc"
1416 #else
1517 # error dont know how to enumerate processes on this platform
1618 #endif
Index: trunk/tools/slayerd/Makefile
@@ -1,4 +1,4 @@
2 -VERSION = 1.7
 2+VERSION = 1.8
33
44 PREFIX ?= /usr/local
55 CONFDIR ?= /etc/slayerd
@@ -8,21 +8,34 @@
99
1010 CXX = g++
1111 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
1615 LIBS =
1716
1817 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
2122 endif
2223
 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+
2336 all: slayerd
2437
2538 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)
2740
2841 install: slayerd
2942 install -d $(DESTDIR)$(SBINDIR)
@@ -39,5 +52,6 @@
4053 clean:
4154 rm -f slayerd *.o
4255
43 -.PHONY: clean install
 56+.PHONY: clean install all
4457 .SUFFICES: .cc .o
 58+process.o: process.cc proc_linux.cc proc_solaris.cc proc_freebsd.cc

Status & tagging log