| 1 |
gregl |
3.1 |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ SGI"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Holodeck display process communication |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "rholo.h" |
| 12 |
|
|
#include "rhdisp.h" |
| 13 |
gregl |
3.2 |
#include <sys/uio.h> |
| 14 |
gregl |
3.1 |
|
| 15 |
gregl |
3.2 |
#ifndef HDSUF |
| 16 |
gregl |
3.16 |
#define HDSUF ".hdi" |
| 17 |
gregl |
3.2 |
#endif |
| 18 |
gregl |
3.1 |
|
| 19 |
gregl |
3.2 |
static int inp_flags; |
| 20 |
|
|
static int dpd[3]; |
| 21 |
gregl |
3.5 |
static FILE *dpout; |
| 22 |
gregl |
3.2 |
|
| 23 |
|
|
|
| 24 |
|
|
disp_open(dname) /* open the named display driver */ |
| 25 |
gregl |
3.1 |
char *dname; |
| 26 |
|
|
{ |
| 27 |
gregl |
3.11 |
char dpath[128], fd0[8], fd1[8], *cmd[5]; |
| 28 |
gregl |
3.2 |
int i; |
| 29 |
gregl |
3.11 |
/* get full display program name */ |
| 30 |
gregl |
3.2 |
#ifdef DEVPATH |
| 31 |
|
|
sprintf(dpath, "%s/%s%s", DEVPATH, dname, HDSUF); |
| 32 |
|
|
#else |
| 33 |
|
|
sprintf(dpath, "dev/%s%s", dname, HDSUF); |
| 34 |
|
|
#endif |
| 35 |
gregl |
3.11 |
/* dup stdin and stdout */ |
| 36 |
|
|
if (readinp) |
| 37 |
|
|
sprintf(fd0, "%d", dup(0)); |
| 38 |
|
|
else |
| 39 |
|
|
strcpy(fd0, "-1"); |
| 40 |
|
|
sprintf(fd1, "%d", dup(1)); |
| 41 |
|
|
/* start the display process */ |
| 42 |
|
|
cmd[0] = dpath; |
| 43 |
|
|
cmd[1] = froot; cmd[2] = fd1; cmd[3] = fd0; |
| 44 |
|
|
cmd[4] = NULL; |
| 45 |
|
|
i = open_process(dpd, cmd); |
| 46 |
gregl |
3.6 |
if (i <= 0) |
| 47 |
gregl |
3.2 |
error(USER, "cannot start display process"); |
| 48 |
gregl |
3.11 |
if ((dpout = fdopen(dpd[1], "w")) == NULL) |
| 49 |
gregl |
3.5 |
error(SYSTEM, "cannot associate FILE with display pipe"); |
| 50 |
gregl |
3.11 |
dpd[1] = -1; /* causes ignored error in close_process() */ |
| 51 |
gregl |
3.2 |
inp_flags = 0; |
| 52 |
gregl |
3.11 |
/* close dup'ed stdin and stdout */ |
| 53 |
|
|
if (readinp) |
| 54 |
|
|
close(atoi(fd0)); |
| 55 |
|
|
close(atoi(fd1)); |
| 56 |
gregl |
3.2 |
/* write out hologram grids */ |
| 57 |
|
|
for (i = 0; hdlist[i] != NULL; i++) |
| 58 |
|
|
disp_result(DS_ADDHOLO, sizeof(HDGRID), (char *)hdlist[i]); |
| 59 |
gregl |
3.5 |
disp_flush(); |
| 60 |
gregl |
3.1 |
} |
| 61 |
|
|
|
| 62 |
|
|
|
| 63 |
gregl |
3.2 |
disp_packet(p) /* display a packet */ |
| 64 |
gregl |
3.4 |
register PACKHEAD *p; |
| 65 |
gregl |
3.2 |
{ |
| 66 |
gregl |
3.4 |
disp_result(DS_BUNDLE, packsiz(p->nr), (char *)p); |
| 67 |
gregl |
3.2 |
} |
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
gregl |
3.1 |
disp_check(block) /* check display process */ |
| 71 |
|
|
int block; |
| 72 |
|
|
{ |
| 73 |
gregl |
3.2 |
MSGHEAD msg; |
| 74 |
|
|
int n; |
| 75 |
|
|
char *buf = NULL; |
| 76 |
|
|
|
| 77 |
gregl |
3.6 |
if (dpout == NULL) |
| 78 |
gregl |
3.2 |
return(-1); |
| 79 |
gregl |
3.5 |
/* flush display output */ |
| 80 |
|
|
disp_flush(); |
| 81 |
gregl |
3.3 |
/* check read blocking */ |
| 82 |
gregl |
3.2 |
if (block != (inp_flags == 0)) { |
| 83 |
|
|
inp_flags = block ? 0 : FNONBLK; |
| 84 |
|
|
if (fcntl(dpd[0], F_SETFL, inp_flags) < 0) |
| 85 |
|
|
goto fcntlerr; |
| 86 |
|
|
} |
| 87 |
|
|
/* read message header */ |
| 88 |
|
|
n = read(dpd[0], (char *)&msg, sizeof(MSGHEAD)); |
| 89 |
|
|
if (n != sizeof(MSGHEAD)) { |
| 90 |
gregl |
3.10 |
if (n >= 0) { |
| 91 |
|
|
dpout = NULL; |
| 92 |
gregl |
3.2 |
error(USER, "display process died"); |
| 93 |
gregl |
3.10 |
} |
| 94 |
gregl |
3.2 |
if (errno != EAGAIN & errno != EINTR) |
| 95 |
|
|
goto readerr; |
| 96 |
gregl |
3.3 |
return(2); /* acceptable failure */ |
| 97 |
gregl |
3.2 |
} |
| 98 |
|
|
if (msg.nbytes) { /* get the message body */ |
| 99 |
|
|
buf = (char *)malloc(msg.nbytes); |
| 100 |
|
|
if (buf == NULL) |
| 101 |
|
|
error(SYSTEM, "out of memory in disp_check"); |
| 102 |
|
|
if (fcntl(dpd[0], F_SETFL, inp_flags=0) < 0) |
| 103 |
|
|
goto fcntlerr; |
| 104 |
|
|
if (readbuf(dpd[0], buf, msg.nbytes) != msg.nbytes) |
| 105 |
|
|
goto readerr; |
| 106 |
|
|
} |
| 107 |
|
|
switch (msg.type) { /* take appropriate action */ |
| 108 |
gregl |
3.12 |
case DR_BUNDLE: /* new bundle to calculate */ |
| 109 |
gregl |
3.2 |
if (msg.nbytes != sizeof(PACKHEAD)) |
| 110 |
|
|
error(INTERNAL, "bad DR_BUNDLE from display process"); |
| 111 |
|
|
bundle_set(BS_ADD, (PACKHEAD *)buf, 1); |
| 112 |
|
|
break; |
| 113 |
gregl |
3.12 |
case DR_NEWSET: /* new calculation set */ |
| 114 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
| 115 |
|
|
error(INTERNAL, "bad DR_NEWSET from display process"); |
| 116 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
| 117 |
|
|
bundle_set(BS_NEW, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
| 118 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
| 119 |
gregl |
3.5 |
disp_flush(); |
| 120 |
gregl |
3.2 |
break; |
| 121 |
gregl |
3.12 |
case DR_ADDSET: /* add to calculation set */ |
| 122 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
| 123 |
|
|
error(INTERNAL, "bad DR_ADDSET from display process"); |
| 124 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
| 125 |
|
|
bundle_set(BS_ADD, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
| 126 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
| 127 |
gregl |
3.15 |
disp_flush(); |
| 128 |
gregl |
3.2 |
break; |
| 129 |
gregl |
3.12 |
case DR_ADJSET: /* adjust calculation set members */ |
| 130 |
gregl |
3.9 |
if (msg.nbytes % sizeof(PACKHEAD)) |
| 131 |
|
|
error(INTERNAL, "bad DR_ADJSET from display process"); |
| 132 |
|
|
disp_result(DS_STARTIMM, 0, NULL); |
| 133 |
|
|
bundle_set(BS_ADJ, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
| 134 |
|
|
disp_result(DS_ENDIMM, 0, NULL); |
| 135 |
|
|
disp_flush(); |
| 136 |
|
|
break; |
| 137 |
gregl |
3.12 |
case DR_DELSET: /* delete from calculation set */ |
| 138 |
gregl |
3.2 |
if (msg.nbytes % sizeof(PACKHEAD)) |
| 139 |
|
|
error(INTERNAL, "bad DR_DELSET from display process"); |
| 140 |
|
|
bundle_set(BS_DEL, (PACKHEAD *)buf, msg.nbytes/sizeof(PACKHEAD)); |
| 141 |
|
|
break; |
| 142 |
gregl |
3.12 |
case DR_ATTEN: /* block for priority request */ |
| 143 |
gregl |
3.2 |
if (msg.nbytes) |
| 144 |
|
|
error(INTERNAL, "bad DR_ATTEN from display process"); |
| 145 |
|
|
/* send acknowledgement */ |
| 146 |
|
|
disp_result(DS_ACKNOW, 0, NULL); |
| 147 |
gregl |
3.3 |
return(disp_check(1)); /* block on following request */ |
| 148 |
gregl |
3.13 |
case DR_KILL: /* kill computation process(es) */ |
| 149 |
|
|
if (msg.nbytes) |
| 150 |
|
|
error(INTERNAL, "bad DR_KILL from display process"); |
| 151 |
gregl |
3.14 |
if (nprocs > 0) |
| 152 |
|
|
done_rtrace(); |
| 153 |
|
|
else |
| 154 |
gregl |
3.13 |
error(WARNING, "no rtrace process to kill"); |
| 155 |
|
|
break; |
| 156 |
|
|
case DR_RESTART: /* restart computation process(es) */ |
| 157 |
|
|
if (msg.nbytes) |
| 158 |
|
|
error(INTERNAL, "bad DR_RESTART from display process"); |
| 159 |
gregl |
3.14 |
if (ncprocs > nprocs) |
| 160 |
|
|
new_rtrace(); |
| 161 |
|
|
else if (nprocs > 0) |
| 162 |
gregl |
3.13 |
error(WARNING, "rtrace already runnning"); |
| 163 |
|
|
else |
| 164 |
|
|
error(WARNING, "holodeck not open for writing"); |
| 165 |
|
|
break; |
| 166 |
|
|
case DR_CLOBBER: /* clobber holodeck */ |
| 167 |
|
|
if (msg.nbytes) |
| 168 |
|
|
error(INTERNAL, "bad DR_CLOBBER from display process"); |
| 169 |
|
|
if (!force || !ncprocs) |
| 170 |
|
|
error(WARNING, "request to clobber holodeck denied"); |
| 171 |
|
|
else { |
| 172 |
|
|
error(WARNING, "clobbering holodeck contents"); |
| 173 |
|
|
hdclobber(NULL); |
| 174 |
|
|
} |
| 175 |
|
|
break; |
| 176 |
gregl |
3.12 |
case DR_SHUTDOWN: /* shut down program */ |
| 177 |
gregl |
3.2 |
if (msg.nbytes) |
| 178 |
|
|
error(INTERNAL, "bad DR_SHUTDOWN from display process"); |
| 179 |
|
|
return(0); /* zero return signals shutdown */ |
| 180 |
gregl |
3.12 |
case DR_NOOP: /* do nothing */ |
| 181 |
gregl |
3.7 |
break; |
| 182 |
gregl |
3.2 |
default: |
| 183 |
|
|
error(INTERNAL, "unrecognized request from display process"); |
| 184 |
|
|
} |
| 185 |
gregl |
3.3 |
if (msg.nbytes) /* clean up */ |
| 186 |
gregl |
3.2 |
free(buf); |
| 187 |
|
|
return(1); /* normal return value */ |
| 188 |
|
|
fcntlerr: |
| 189 |
|
|
error(SYSTEM, "cannot change display blocking mode"); |
| 190 |
|
|
readerr: |
| 191 |
|
|
error(SYSTEM, "error reading from display process"); |
| 192 |
gregl |
3.1 |
} |
| 193 |
|
|
|
| 194 |
|
|
|
| 195 |
gregl |
3.2 |
int |
| 196 |
|
|
disp_close() /* close our display process */ |
| 197 |
gregl |
3.1 |
{ |
| 198 |
gregl |
3.2 |
int rval; |
| 199 |
|
|
|
| 200 |
gregl |
3.6 |
if (dpout == NULL) |
| 201 |
gregl |
3.2 |
return(-1); |
| 202 |
|
|
disp_result(DS_SHUTDOWN, 0, NULL); |
| 203 |
gregl |
3.5 |
fclose(dpout); |
| 204 |
|
|
dpout = NULL; |
| 205 |
gregl |
3.2 |
return(close_process(dpd)); |
| 206 |
|
|
} |
| 207 |
|
|
|
| 208 |
|
|
|
| 209 |
gregl |
3.5 |
disp_result(type, nbytes, p) /* queue result message to display process */ |
| 210 |
gregl |
3.2 |
int type, nbytes; |
| 211 |
|
|
char *p; |
| 212 |
|
|
{ |
| 213 |
|
|
MSGHEAD msg; |
| 214 |
|
|
|
| 215 |
gregl |
3.6 |
if (dpout == NULL) |
| 216 |
|
|
return; |
| 217 |
gregl |
3.2 |
msg.type = type; |
| 218 |
|
|
msg.nbytes = nbytes; |
| 219 |
gregl |
3.5 |
fwrite((char *)&msg, sizeof(MSGHEAD), 1, dpout); |
| 220 |
|
|
if (nbytes > 0) |
| 221 |
|
|
fwrite(p, 1, nbytes, dpout); |
| 222 |
|
|
} |
| 223 |
|
|
|
| 224 |
|
|
|
| 225 |
|
|
disp_flush() /* flush output to display */ |
| 226 |
|
|
{ |
| 227 |
|
|
if (fflush(dpout) < 0) |
| 228 |
|
|
error(SYSTEM, "error writing to the display process"); |
| 229 |
gregl |
3.1 |
} |