00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #if __linux__
00026 #include <stdint.h>
00027 #endif
00028 #include "ShortIdImpl.H"
00029 #include "SRPC.H"
00030 #include "ShortIdSRPC.H"
00031 #include "VestaConfig.H"
00032 #include "VRWeed.H"
00033 #include "logging.H"
00034 #include <time.h>
00035 #include <assert.h>
00036 #include <Thread.H>
00037 #include <LimService.H>
00038
00039 #define DEF_MAX_RUNNING 32
00040 #define DEF_MAX_BLOCKED 8
00041 #define STACK_SIZE 512000L
00042
00043
00044
00045
00046
00047
00048 static unsigned int readTimeout = 300;
00049
00050
00051
00052 void
00053 ShortIdReceptionist(SRPC *srpc, int proc_id, void *arg)
00054 {
00055
00056 signal(SIGPIPE, SIG_IGN);
00057 signal(SIGQUIT, SIG_DFL);
00058 signal(SIGSEGV, SIG_DFL);
00059 signal(SIGABRT, SIG_DFL);
00060 signal(SIGILL, SIG_DFL);
00061 signal(SIGBUS, SIG_DFL);
00062
00063
00064
00065 srpc->enable_read_timeout(readTimeout);
00066
00067 switch (proc_id) {
00068 case ShortIdSRPC::AcquireShortIdBlock:
00069 {
00070 ShortIdBlock bk;
00071 AcquireShortIdBlock(bk, (bool) srpc->recv_int());
00072 srpc->recv_end();
00073 srpc->send_int((int) bk.start);
00074 srpc->send_int((int) bk.leaseExpires);
00075 srpc->send_bytes((const char *) bk.bits, sizeof(bk.bits));
00076 srpc->send_end();
00077 }
00078 break;
00079 case ShortIdSRPC::RenewShortIdBlock:
00080 {
00081 ShortIdBlock bk;
00082 bk.start = (ShortId) srpc->recv_int();
00083 srpc->recv_end();
00084 bool res = RenewShortIdBlock(bk);
00085 srpc->send_int((int) bk.leaseExpires);
00086 srpc->send_int((int) res);
00087 srpc->send_end();
00088 }
00089 break;
00090 case ShortIdSRPC::ReleaseShortIdBlock:
00091 {
00092 ShortIdBlock bk;
00093 bk.start = (ShortId) srpc->recv_int();
00094 srpc->recv_end();
00095 ReleaseShortIdBlock(bk);
00096 srpc->send_end();
00097 }
00098 break;
00099 case ShortIdSRPC::OldKeepDerived:
00100 case ShortIdSRPC::KeepDerived:
00101 {
00102 ShortIdsFile sf = (ShortIdsFile) srpc->recv_int();
00103 time_t time;
00104 time = (time_t) srpc->recv_int();
00105 bool force = false;
00106 if (proc_id != ShortIdSRPC::OldKeepDerived) {
00107 (bool) srpc->recv_int();
00108 }
00109 srpc->recv_end();
00110 int res = KeepDerived(sf, time, force);
00111 srpc->send_int(res);
00112 srpc->send_end();
00113 }
00114 break;
00115 case ShortIdSRPC::Checkpoint:
00116 srpc->recv_end();
00117 Checkpoint();
00118 srpc->send_end();
00119 break;
00120 case ShortIdSRPC::GetWeedingState:
00121 {
00122 srpc->recv_end();
00123 ShortIdsFile ds, ss;
00124 time_t dt, st;
00125 bool swip, dip, ddun, cip;
00126 GetWeedingState(ds, dt, ss, st, swip, dip, ddun, cip);
00127 srpc->send_int((int) ds);
00128 srpc->send_int((int) dt);
00129 srpc->send_int((int) ss);
00130 srpc->send_int((int) st);
00131 srpc->send_int((int) swip);
00132 srpc->send_int((int) dip);
00133 srpc->send_int((int) ddun);
00134 srpc->send_int((int) cip);
00135 srpc->send_end();
00136 }
00137 break;
00138 default:
00139 Repos::dprintf(DBG_ALWAYS, "ShortIdReceptionist got unknown proc_id %d; "
00140 "client %s\n", proc_id, srpc->remote_socket().cchars());
00141 srpc->send_failure(SRPC::version_skew,
00142 "ShortIdSRPC: Unknown proc_id");
00143 break;
00144 }
00145 }
00146
00147 void
00148 ShortIdFailure(SRPC* srpc, const SRPC::failure &f, void* arg)
00149 {
00150 if (f.r == SRPC::partner_went_away && !Repos::isDebugLevel(DBG_SRPC)) return;
00151 const char* client;
00152 try {
00153 client = srpc->remote_socket().cchars();
00154 } catch (SRPC::failure f) {
00155 client = "unknown";
00156 }
00157 Repos::dprintf(DBG_ALWAYS, "ShortIdReceptionist got SRPC::failure: %s (%d); "
00158 "client %s\n", f.msg.cchars(), f.r, client);
00159 }
00160
00161 void
00162 ShortIdServerExport(char *serverName)
00163 {
00164 static Text port;
00165 int max_running=DEF_MAX_RUNNING, max_blocked=DEF_MAX_BLOCKED;
00166 Text unused;
00167 try {
00168 port = VestaConfig::get_Text("Repository", "ShortIdSRPC_port");
00169 if(VestaConfig::get("Repository", "ShortIdSRPC_max_running", unused))
00170 max_running = VestaConfig::get_int("Repository",
00171 "ShortIdSRPC_max_running");
00172 if(VestaConfig::get("Repository", "ShortIdSRPC_max_blocked", unused))
00173 max_blocked = VestaConfig::get_int("Repository",
00174 "ShortIdSRPC_max_blocked");
00175 if(VestaConfig::get("Repository", "ShortIdSRPC_read_timeout", unused))
00176 readTimeout = VestaConfig::get_int("Repository",
00177 "ShortIdSRPC_read_timeout");
00178 } catch (VestaConfig::failure f) {
00179 Repos::dprintf(DBG_ALWAYS,
00180 "VestaConfig::failure in ShortIdServerExport: %s\n",
00181 f.msg.cchars());
00182 abort();
00183 }
00184 static LimService receptionist(port, ShortIdSRPC::version, max_running,
00185 max_blocked, ShortIdReceptionist,
00186 ShortIdFailure, NULL, STACK_SIZE);
00187 receptionist.Forked_Run();
00188 }