Removed /usr/local from CDPATH
[clearscm.git] / cq / cqd.pl
1 #!cqperl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =head1 NAME $RCSfile: cqd.pl,v $
8
9 Clearquest Daemon - Daemon to provide access to Clearquest database
10
11 This daemon instanciates an instance of the Clearquest::DBService to service 
12 requests for information of a Clearquest database or to update a Clearquest
13 database.
14
15 =head1 VERSION
16
17 =over
18
19 =item Author
20
21 Andrew DeFaria <Andrew@ClearSCM.com>
22
23 =item Revision
24
25 $Revision: 2.4 $
26
27 =item Created:
28
29 Mon Oct 24 16:19:15 PDT 2011
30
31 =item Modified:
32
33 $Date: 2013/03/15 00:15:32 $
34
35 =back
36
37 =head1 SYNOPSIS
38
39  Usage: cqd.pl [-u|sage] [-v|erbose] [-d|ebug]
40                [-logfile <logfile>] [-[no]daemon]
41                [-s|erver <server>] [-p|ort <n>]
42
43  Where:
44    -u|sage:     Displays usage
45    -v|erbose:   Be verbose
46    -de|bug:     Output debug messages
47
48    -s|erver <server>:   Server to talk to (Default: from conf file or
49                         environment)
50    -p|ort <n>           Port nbr to use (Default: from conf file or
51                         environment)
52    -m|ultithreaded
53    -logfile <logfile>:  Where to log output (Default: STDOUT)
54    -[no]daemon:         Enter daemon mode (Default: Enter daemon mode)
55
56    -s|erver:    For module = client or rest this is the name of the server 
57                 that will be providing the service
58    -p|ort:      For module = client, this is the point on the server to talk
59                 through.
60
61 =head1 Options
62
63 Options are keep in the cq.conf file in etc. They specify the default options
64 listed below. Or you can export the option name to the env(1) to override the
65 defaults in cq.conf. Finally you can programmatically set the options when you
66 call new by passing in a %parms hash. To specify the %parms hash key remove the
67 CQ_ portion and lc the rest.
68
69 =for html <blockquote>
70
71 =over
72
73 =item CQ_SERVER
74
75 Clearquest::Server name to connect to (Default: from config file)
76
77 =item CQ_PORT
78
79 Clearquest::Server port to connect to (Default: from config file)
80
81 =back
82
83 =cut
84
85 use Config;
86 use File::Spec;
87 use FindBin;
88 use Getopt::Long;
89
90 use CQPerlExt;
91
92 use lib "$FindBin::Bin/../lib";
93
94 use Clearquest::Server;
95 use Display;
96 use Utils;
97
98 my $VERSION  = '$Revision: 2.4 $';
99   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
100
101 my %opts;
102
103 GetOptions (
104   \%opts,
105   verbose => sub { set_verbose },
106   debug   => sub { set_debug },
107   usage   => sub { Usage },
108   'server=s',
109   'port=i',
110   'logfile=s',
111   'multithreaded!',
112   'daemon!',
113   'serviceClient=s',
114   'socket=s',
115 ) || Usage;
116
117 my %parms = (
118   CQ_SERVER        => $opts{server},
119   CQ_PORT          => $opts{port},
120   CQ_MULTITHREADED => $opts{multithreaded},
121 );
122
123 my $cqservice = Clearquest::Server->new (%parms);
124
125 if ($opts{serviceClient}) {
126   $cqservice->{clientname} = $opts{serviceClient};
127   
128   debug "In cqd.pl with -serviceClient $cqservice->{clientname} - opening socket";
129   
130   open my $client, '+<&=', *STDIN
131     or error "Unable to open socket connection to client", 1;
132   
133   $client->autoflush (1);
134   
135   debug "Socket open - servicing client = $client";
136   $cqservice->_serviceClient ($client);
137   debug "Returned from servicing client";
138   
139   exit;
140 } # if
141
142 my $announcement  = "$FindBin::Script v$VERSION ";
143    $announcement .= $cqservice->multithreaded 
144                   ? '(Multithreaded)' 
145                   : '(Singlethreaded)';
146
147 verbose $announcement;
148
149 if ($opts{daemon} and !get_debug and !defined $DB::OUT) {
150   print $DB::OUT "Debugging\n" if get_debug;
151   
152   my ($logfile) = ($FindBin::Script =~ /(.*)\.pl$/);
153    
154   $opts{logfile} ||= "$logfile.log";
155   
156   $logfile = File::Spec->rel2abs ($opts{logfile});
157   
158   verbose "Entering daemon mode (Server pid: $$ - logging to $logfile)";
159   
160   if ($Config{perl} eq 'ratlperl') {
161     error "Unable to daemonize with cqperl", 1;
162   } else {
163     EnterDaemonMode $opts{logfile}, $opts{logfile};
164   } # if
165 } # if
166
167 delete $opts{daemon};
168 delete $opts{multithreaded};
169
170 verbose 'Starting Server';
171 $cqservice->startServer;
172
173 verbose 'Shutting down server';
174
175 exit;