/******************************************************************************* * Copyright (c) 2007, Perforce Software, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE * SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *******************************************************************************/ /* * Program to start/stop multiple Perforce servers on a machine */ #define NEED_GETUID #include <stdhdrs.h> #include <stdarg.h> #include <strbuf.h> #include <error.h> #include <errorlog.h> #include <vararray.h> #include <strdict.h> #include <strtable.h> #include <options.h> #include <ident.h> #include "p4dctlerr.h" #include "cmdline.h" #include "server.h" #include "config.h" /* * Ident String */ Ident ident = { IdentMagic "P4DCTL" "/" ID_OS "/" ID_REL "/" ID_PATCH " (built with " ID_API " Perforce API)", ID_Y "/" ID_M "/" ID_D }; void ReportError( Error *e ) { AssertLog.Report( e ); e->Clear(); } int StartServers( Config &config, int startAll, StrPtr &name, int type, int &started, Error *e ) { int i; int tried = 0; Server * s; // // Iterate over servers // for( started = 0, i = 0; s = config.GetServer( i ); i++ ) { if( startAll || name == s->Name() ) { if( !s->KindOf( type ) ) continue; if( !s->IsValid( e ) ) { AssertLog.Report( e ); e->Clear(); continue; } switch( s->CheckAccess( e ) ) { case CHECK_DENIED: e->Clear(); e->Set( CtlErr::SkippedServer ) << s->Name() << s->Label() << s->Owner(); AssertLog.ReportNoTag( e ); e->Clear(); continue; case CHECK_DENIED_ROOT: AssertLog.Report( e ); e->Clear(); continue; default: break; } tried++; if( !s->Start( e ) ) { if( e->Test() ) { AssertLog.Report( e ); e->Clear(); } } else if( e->Test() ) { // Returned true with an error means it was already // running tried--; AssertLog.Report( e ); e->Clear(); } else { e->Set( CtlErr::StartedServer ) << s->Name() << s->Label(); AssertLog.ReportNoTag( e ); e->Clear(); started++; } } } return tried == started; } int StopServers( Config &config, int stopAll, StrPtr &name, int type, int &stopped, Error *e ) { int i; int tried = 0; Server * s; // // Iterate over servers // for( stopped = 0, i = 0; s = config.GetServer( i ); i++ ) { if( stopAll || name == s->Name() ) { if( !s->KindOf( type ) ) continue; if( !s->IsValid( e ) ) { AssertLog.Report( e ); e->Clear(); continue; } switch( s->CheckAccess( e ) ) { case CHECK_DENIED: e->Clear(); e->Set( CtlErr::SkippedServer ) << s->Name() << s->Label() << s->Owner(); AssertLog.ReportNoTag( e ); e->Clear(); continue; case CHECK_DENIED_ROOT: AssertLog.Report( e ); e->Clear(); continue; default: break; } if( !s->Running( e ) ) { e->Set(CtlErr::ServerNotRunning) << s->Name() << s->Label(); AssertLog.ReportNoTag( e ); e->Clear(); continue; } tried++; if( !s->Stop( e ) ) { AssertLog.ReportNoTag( e ); e->Clear(); } else { e->Set( CtlErr::StoppedServer ) << s->Name() << s->Label(); AssertLog.ReportNoTag( e ); e->Clear(); stopped++; } } } return tried == stopped; } void CheckServers( Config &config, int checkAll, StrPtr &name, int type, Error *e ) { int i; Server * s; // // Iterate over servers // for( i = 0; s = config.GetServer( i ); i++ ) { if( checkAll || name == s->Name() ) { if( !s->KindOf( type ) ) continue; if( !s->IsValid( e ) ) { AssertLog.Report( e ); e->Clear(); continue; } switch( s->CheckAccess( e ) ) { case CHECK_DENIED: e->Clear(); e->Set( CtlErr::SkippedServer ) << s->Name() << s->Label() << s->Owner(); AssertLog.ReportNoTag( e ); e->Clear(); continue; case CHECK_DENIED_ROOT: AssertLog.Report( e ); e->Clear(); continue; default: break; } if( s->Running( e ) ) { e->Set( CtlErr::ServerRunning ) << s->Name() << s->Label(); AssertLog.ReportNoTag( e ); e->Clear(); continue; } e->Set( CtlErr::ServerNotRunning ) << s->Name() << s->Label(); AssertLog.ReportNoTag( e ); e->Clear(); } } } void RotateJournals( Config &config, int rotateAll, StrPtr &name, Error *e ) { int i; Server * s; P4D * p4d = 0; // // Iterate over servers // for( i = 0; s = config.GetServer( i ); i++ ) { if( rotateAll || name == s->Name() ) { if( !s->KindOf( SERVER_P4D ) ) continue; if( !s->IsValid( e ) ) { AssertLog.Report( e ); e->Clear(); continue; } switch( s->CheckAccess( e ) ) { case CHECK_DENIED: e->Clear(); e->Set( CtlErr::SkippedServer ) << s->Name() << s->Label() << s->Owner(); AssertLog.ReportNoTag( e ); e->Clear(); continue; case CHECK_DENIED_ROOT: AssertLog.Report( e ); e->Clear(); continue; default: break; } p4d = (P4D *) s; // Blurt out our message e->Set( CtlErr::JnlRotate ) << p4d->Name(); AssertLog.ReportNoTag( e ); e->Clear(); p4d->RotateJournal( 1, e ); if( e->Test() ) AssertLog.Report( e ); e->Clear(); } } } void CheckpointServers( Config &config, int ckpAll, StrPtr &name, Error *e ) { int i; Server * s; P4D * p4d = 0; // // Iterate over servers // for( i = 0; s = config.GetServer( i ); i++ ) { if( ckpAll || name == s->Name() ) { if( !s->KindOf( SERVER_P4D ) ) continue; if( !s->IsValid( e ) ) { AssertLog.Report( e ); e->Clear(); continue; } switch( s->CheckAccess( e ) ) { case CHECK_DENIED: e->Clear(); e->Set( CtlErr::SkippedServer ) << s->Name() << s->Label() << s->Owner(); AssertLog.ReportNoTag( e ); e->Clear(); continue; case CHECK_DENIED_ROOT: AssertLog.Report( e ); e->Clear(); continue; default: break; } p4d = (P4D *) s; // Blurt out our message e->Set( CtlErr::Checkpointing ) << p4d->Name(); AssertLog.ReportNoTag( e ); e->Clear(); p4d->Checkpoint( 1, e ); if( e->Test() ) AssertLog.Report( e ); e->Clear(); } } } void CmdStart( Config &config, int argc, char **argv, Error *e ) { Options opts; StrRef name; StrPtr * opt; int started = 0; int startAll = 0; int status = 0; int type = SERVER_ALL; opts.Parse( argc, argv, "at:", OPT_OPT, CtlErr::UsageStart, e ); if( e->Test() ) return; if( opts[ 'a' ] ) startAll++; if( opt = opts[ 't' ] ) type = config.ServerType( opt->Text() ); if( !argc && !startAll ) { e->Set( CtlErr::UsageStart ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } status = StartServers( config, startAll, name, type, started, e ); e->Set( CtlErr::StartedServers ) << started; AssertLog.ReportNoTag( e ); e->Clear(); if( !status ) e->Set( CtlErr::SomeFailedStarts ); } void CmdStop( Config &config, int argc, char **argv, Error *e ) { Options opts; StrRef name; StrPtr * opt; int stopped = 0; int stopAll = 0; int status = 0; int type = SERVER_ALL; opts.Parse( argc, argv, "at:", OPT_OPT, CtlErr::UsageStop, e ); if( e->Test() ) return; if( opts[ 'a' ] ) stopAll++; if( opt = opts[ 't' ] ) type = config.ServerType( opt->Text() ); if( !argc && !stopAll ) { e->Set( CtlErr::UsageStop ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } status = StopServers( config, stopAll, name, type, stopped, e ); e->Set( CtlErr::StoppedServers ) << stopped; AssertLog.ReportNoTag( e ); e->Clear(); if( !status ) e->Set( CtlErr::SomeFailedStops ); } void CmdRestart( Config &config, int argc, char **argv, Error *e ) { Options opts; StrRef name; StrPtr * opt; int started = 0; int stopped = 0; int startAll = 0; int status = 0; int type = SERVER_ALL; opts.Parse( argc, argv, "at:", OPT_OPT, CtlErr::UsageRestart, e ); if( e->Test() ) return; if( opts[ 'a' ] ) startAll++; if( opt = opts[ 't' ] ) type = config.ServerType( opt->Text() ); if( !argc && !startAll ) { e->Set( CtlErr::UsageRestart ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } // // In restart we only care about failures to start. Failures to stop // are glossed over since they're less important // StopServers( config, startAll, name, type, stopped, e ); e->Set( CtlErr::StoppedServers ) << stopped; AssertLog.ReportNoTag( e ); e->Clear(); status = StartServers( config, startAll, name, type, started, e ); e->Set( CtlErr::StartedServers ) << started; AssertLog.ReportNoTag( e ); e->Clear(); if( !status ) e->Set( CtlErr::SomeFailedStarts ); } void CmdStatus( Config &config, int argc, char **argv, Error *e ) { Options opts; StrPtr * opt; StrRef name; int checkAll = 0; int type = SERVER_ALL; opts.Parse( argc, argv, "at:", OPT_OPT, CtlErr::UsageStatus, e ); if( e->Test() ) return; if( opts[ 'a' ] ) checkAll++; if( opt = opts[ 't' ] ) type = config.ServerType( opt->Text() ); if( !argc && !checkAll ) { e->Set( CtlErr::UsageStatus ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } CheckServers( config, checkAll, name, type, e ); } void CmdCheckpoint( Config &config, int argc, char **argv, Error *e ) { Options opts; StrPtr * opt; StrRef name; int ckpAll = 0; opts.Parse( argc, argv, "a", OPT_OPT, CtlErr::UsageCheckpoint, e ); if( e->Test() ) return; if( opts[ 'a' ] ) ckpAll++; if( !argc && !ckpAll ) { e->Set( CtlErr::UsageCheckpoint ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } CheckpointServers( config, ckpAll, name, e ); } void CmdJournal( Config &config, int argc, char **argv, Error *e ) { Options opts; StrPtr * opt; StrRef name; int rotateAll = 0; opts.Parse( argc, argv, "a", OPT_OPT, CtlErr::UsageJournal, e ); if( e->Test() ) return; if( opts[ 'a' ] ) rotateAll++; if( !argc && !rotateAll ) { e->Set( CtlErr::UsageJournal ); ReportError( e ); return; } if( argc ) { argc--; name.Set( *argv++ ); } RotateJournals( config, rotateAll, name, e ); } // // Should we force the user to use only our safe config paths? Basically, // if the process has an euid of zero, then messing with the configuration // on the command line is dangerous. // extern int yydebug; int main( int argc, char **argv ) { const char *safe_cfg_file = "/etc/p4d.conf"; const char *safe_pid_dir = "/var/run"; const char *cfg_file = safe_cfg_file; const char *pid_dir = safe_pid_dir; const char *cmd = 0; int debug = 0; int quiet = 0; Options opts; StrPtr * o; Error *e = new Error; Config config; AssertLog.SetTag( "p4dctl" ); opts.Parse( --argc, ++argv, "c:p:qv:V", OPT_ANY, CtlErr::UsageMain, e ); AssertLog.Abort( e ); if( o = opts[ 'c' ] ) cfg_file = o->Text(); if( o = opts[ 'p' ] ) pid_dir = o->Text(); if( o = opts[ 'v' ] ) debug = o->Atoi(); if( opts[ 'q' ] ) quiet++; if( debug > 5 ) yydebug = 1; // // If we're running as root, then you can't pass arbitrary config // files or pid directories. // if( !geteuid() && ( pid_dir != safe_pid_dir || cfg_file != safe_cfg_file )) { pid_dir = safe_pid_dir; cfg_file = safe_cfg_file; e->Set( CtlErr::UnsafeConfig ); AssertLog.Abort( e ); } if( opts[ 'V' ] ) { StrBuf t; ident.GetMessage( &t ); printf( "%s", t.Text() ); return 0; } // // Drop privs now, before parsing config file // Config::DropPrivs( e ); AssertLog.Abort( e ); // // Load our configuration // config.SetPidDir( pid_dir ); config.SetDebug( debug ); config.Parse( cfg_file, e ); AssertLog.Abort( e ); // // If there are no more args, obviously that's a problem... // if( !argc ) { e->Set( CtlErr::UsageMain ); AssertLog.ReportNoTag( e ); return 0; } // // What's left in argc, and argv are our commands and any arguments/flags // to those commands. So // argc--; cmd = *argv++; // From here on in, be quiet if we've been asked to. if( quiet ) AssertLog.SetSyslog(); if( !strcmp( "start", cmd ) ) CmdStart( config, argc, argv, e ); else if( !strcmp( "stop", cmd ) ) CmdStop( config, argc, argv, e ); else if( !strcmp( "restart", cmd ) ) CmdRestart( config, argc, argv, e ); else if( !strcmp( "status", cmd ) ) CmdStatus( config, argc, argv, e ); else if( !strcmp( "checkpoint", cmd ) ) CmdCheckpoint( config, argc, argv, e ); else if( !strcmp( "journal", cmd ) ) CmdJournal( config, argc, argv, e ); else { e->Set( CtlErr::UsageMain ); e->Set( CtlErr::BadCommand ) << cmd; AssertLog.Abort( e ); } return e->Test(); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 7841 | brett |
Initial branch of the Unix Perforce Server Control tool. See if this can be made to operate under Windows. |
||
//guest/tony_smith/perforce/p4dctl/src/p4dctl.cpp | |||||
#4 | 7179 | Tony Smith |
Fix bug introduced in previous change. In my attempt to sort out the declaration of yytext, I declared it as an array rather than a pointer, and that could cause crashes. I also added support for debugging the parser in the course of fixing this and that's a useful thing to have anyway. Updated linux26x86 binary |
||
#3 | 6319 | Tony Smith |
Fix to segfault on bad command syntax. If someone ran almost any command without specifying either the -a flag, or a server name then p4dctl would segfault. This changes reworks the command line handling to throw a usage-message in that case |
||
#2 | 6184 | Tony Smith | Some minor changes to debugging output | ||
#1 | 5945 | Tony Smith |
Release p4dctl, a program for starting/stopping Perforce services on Unix operating systems. Similar to, and developed in concert with, Sven Erik Knop's p4dcfg. For example: p4dctl start -a Can start multiple P4D, P4P, P4Web, or P4FTP servers in one easy command line. It can be executed by root, or by the 'owners' of the configured services and it maintains pidfiles no matter who uses it (so they remain accurate). An init script using p4dctl will typically just use: p4dctl start -a p4dctl stop -a p4dctl restart -a And check the exit status. |