Changeset 2340
- Timestamp:
- Aug 9, 2011 8:10:45 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/blt4/packages/vizservers/nanoscale/server2.c
r2338 r2340 3 3 #include <unistd.h> 4 4 #include <stdlib.h> 5 #include <string.h>6 5 #include <signal.h> 7 6 #include <sys/types.h> … … 15 14 #include <getopt.h> 16 15 #include <errno.h> 17 16 #include <syslog.h> 17 #include <stdarg.h> 18 #include <tcl.h> 19 20 #define TRUE 1 21 #define FALSE 0 22 #define DEF_SERVERS "/opt/hubzero/rappture/render/lib/servers.cf" 23 24 #define ERROR(...) LogMessage(LOG_ERR, __FILE__, __LINE__, __VA_ARGS__) 25 #ifdef WANT_TRACE 26 #define TRACE(...) LogMessage(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) 27 #else 28 #define TRACE(...) 29 #endif 30 #define WARN(...) LogMessage(LOG_WARNING, __FILE__, __LINE__, __VA_ARGS__) 31 #define INFO(...) LogMessage(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) 32 33 typedef struct { 34 const char *name; /* Name of server. */ 35 int port; /* Port to listen to. */ 36 int argc; /* # of args in command. */ 37 char **argv; /* Command to execute for server. */ 38 int listenerFd; /* Descriptor the of listener socket. */ 39 } RenderServer; 40 41 static Tcl_HashTable serverTable; 42 static int debugFlag = FALSE; 43 44 void 45 LogMessage(int priority, const char *path, int lineNum, const char* fmt, ...) 46 { 47 #define MSG_LEN (2047) 48 char message[MSG_LEN+1]; 49 const char *s; 50 int length; 51 va_list lst; 52 53 va_start(lst, fmt); 54 s = strrchr(path, '/'); 55 if (s == NULL) { 56 s = path; 57 } else { 58 s++; 59 } 60 length = snprintf(message, MSG_LEN, "line %d of \"%s\": ", lineNum, s); 61 length += vsnprintf(message + length, MSG_LEN - length, fmt, lst); 62 message[MSG_LEN] = '\0'; 63 if (debugFlag) { 64 fprintf(stderr, "%s\n", message); 65 } else { 66 syslog(priority, message, length); 67 } 68 } 18 69 19 70 static void … … 24 75 exit(1); 25 76 } 26 27 typedef struct {28 const char *name;29 int port;30 int argc;31 char **argv;32 int f;33 } RenderServer;34 35 static Tcl_HashTable serverTable;36 77 37 78 static int … … 78 119 79 120 // If program is killed, drop the socket address reservation immediately. 80 bool = 1;121 bool = TRUE; 81 122 status = setsockopt(f, SOL_SOCKET, SO_REUSEADDR, &bool, sizeof(bool)); 82 123 if (status < 0) { … … 134 175 135 176 int 136 main(int argc, char * argv[])177 main(int argc, char **argv) 137 178 { 138 179 int status; 139 180 struct sockaddr_in recvAddr; 140 181 int serverFds[FD_SETSIZE]; 141 int recvPort;142 182 int n; 143 char display Var[200];183 char displaySetting[200]; 144 184 int maxCards; 145 185 int dispNum; 146 186 Tcl_HashEntry *hPtr; 147 187 Tcl_HashSearch iter; 188 const char *fileName; 148 189 149 190 dispNum = 0; 150 191 maxCards = 1; 151 recvPort = 2000;152 debug _flags = FALSE;153 strcpy(display Var, "DISPLAY=:0.0");154 if (putenv(display Var) < 0) {192 fileName = DEF_SERVERS; 193 debugFlags = FALSE; 194 strcpy(displaySetting, "DISPLAY=:0.0"); 195 if (putenv(displaySetting) < 0) { 155 196 ERROR("can't set DISPLAY variable: ", strerror(errno)); 156 197 exit(1); … … 171 212 172 213 switch(c) { 173 case 'x': 214 case 'x': /* Number of video cards */ 174 215 maxCards = strtoul(optarg, 0, 0); 175 216 if ((maxCards < 1) || (maxCards > 10)) { … … 178 219 } 179 220 break; 180 case 'd': 181 debug _flag = 1;221 case 'd': /* Debug */ 222 debugFlag = 1; 182 223 break; 183 case 'p': 184 recvPort = strtoul(optarg, 0, 0); 185 break; 186 187 case 'f': 224 225 case 'f': /* Server files */ 188 226 fileName = strdup(optarg); 189 227 break; … … 191 229 default: 192 230 fprintf(stderr,"Don't know what option '%c'.\n", c); 193 help(argv[0]);231 Help(argv[0]); 194 232 return 1; 195 233 } 196 234 } 235 236 if (!ParseServersFile(fileName)) { 237 exit(1); 238 } 197 239 198 240 if (serverTable.numEntries == 0) { … … 201 243 } 202 244 203 if (!ParseServersFile(fileName)) { 204 exit(1); 205 } 206 207 // Bind this address to the socket. 208 recvAddr.sin_family = AF_INET; 209 recvAddr.sin_port = htons(recvPort); 210 recvAddr.sin_addr.s_addr = htonl(INADDR_ANY); 211 212 status = bind(sendFd, (struct sockaddr *)&recvAddr, sizeof(recvAddr)); 213 if (status < 0) { 214 ERROR("can't bind to address: ", strerror(errno)); 215 exit(1); 216 } 217 218 if (!debug_flag) { 245 246 if (!debugFlag) { 219 247 if (daemon(0,1) < 0) { 220 248 ERROR("can't daemonize nanoscale: ", strerror(errno)); … … 273 301 274 302 /* Child process. */ 275 if (!debug _flag) {303 if (!debugFlag) { 276 304 /* Disassociate server from */ 277 305 status = daemon(0,1); … … 288 316 } 289 317 if (maxCards > 1) { 290 display Var[11] = dispNum + '0';318 displaySetting[11] = dispNum + '0'; 291 319 } 320 INFO("%s: client %s connecting to display %d", 321 serverPtr->name, inet_ntoa(newaddr.sin_addr), dispNum); 292 322 status = execvp(serverPtr->argv[0], serverPtr->argv); 293 323 ERROR("can't execute \"", serverPtr->argv[0],
Note: See TracChangeset
for help on using the changeset viewer.