void Framework::Close() { m_init = false; _CLOSE(m_render); _CLOSE(m_wnd); _CLOSE(m_input); }
void Framework::Close() { m_init = false; m_render->Shutdown(); _DELETE(m_render); _CLOSE(m_wnd); _CLOSE(m_input); }
int close(int d) { int n; Fdinfo *f; n = -1; f = &_fdinfo[d]; if(d<0 || d>=OPEN_MAX || !(f->flags&FD_ISOPEN)) errno = EBADF; else{ if(f->flags&(FD_BUFFERED|FD_BUFFEREDX)) { if(f->flags&FD_BUFFERED) _closebuf(d); f->flags &= ~FD_BUFFERED; } n = _CLOSE(d); if(n < 0) _syserrno(); _fdinfo[d].flags = 0; _fdinfo[d].oflags = 0; if(_fdinfo[d].name){ free(_fdinfo[d].name); _fdinfo[d].name = 0; } } return n; }
char* getcwd(char *buf, size_t len) { int fd; fd = _OPEN(".", OREAD); if(fd < 0) { errno = EACCES; return 0; } if(_FD2PATH(fd, buf, len) < 0) { errno = EIO; _CLOSE(fd); return 0; } _CLOSE(fd); /* RSC: is this necessary? */ if(buf[0] == '\0') strcpy(buf, "/"); return buf; }
pid_t getppid(void) { char b[20]; int f; memset(b, 0, sizeof(b)); f = _OPEN("/dev/ppid", OREAD); if(f >= 0) { _PREAD(f, b, sizeof(b), 0); _CLOSE(f); } return atol(b); }
time_t time(time_t *tp) { char b[20]; int f; time_t t; memset(b, 0, sizeof(b)); f = _OPEN("/dev/time", OREAD); if(f >= 0) { _PREAD(f, b, sizeof(b), 0); _CLOSE(f); } t = atol(b); if(tp) *tp = t; return t; }
int _IOUNIT(int fd) { int i, cfd; char buf[128], *args[10]; snprint(buf, sizeof buf, "#d/%dctl", fd); cfd = _OPEN(buf, OREAD); if(cfd < 0) return 0; i = _READ(cfd, buf, sizeof buf-1); _CLOSE(cfd); if(i <= 0) return 0; buf[i] = '\0'; if(getfields(buf, args, 10, 1) != 10) return 0; return atoi(args[7]); }
// Open Control connection muroar_t muroar_connect(const char * server, const char * name) { char useraddr[80] = MUROAR_INVALID; char * addr[] = {useraddr, MUROAR_GSOCK, MUROAR_HOST, "::" MUROAR_OBJECT, MUROAR_ABSTRACT, NULL}; const char * home; unsigned char buf[MUROAR_IOBUF]; uint16_t tmpu16; uint16_t pid; muroar_t fh = MUROAR_HANDLE_INVALID; int i; #if !defined(__WIN32) ssize_t len; #endif // Prepare server address: if ( server != NULL && *server == 0 ) server = NULL; if ( server == NULL ) server = getenv("ROAR_SERVER"); #if !defined(__WIN32) if ( server == NULL ) { if ( (len = readlink("/etc/roarserver", useraddr, sizeof(useraddr))) != -1 ) { useraddr[len < (ssize_t)sizeof(useraddr) ? (size_t)len : sizeof(useraddr)-1] = 0; server = useraddr; } } #endif if ( server != NULL ) { // "+default" is an alias to NULL. if ( !strcmp(server, MUROAR_DEFAULT) ) { server = NULL; // "+invalid" always return an invalid handle. } else if ( !strcmp(server, MUROAR_INVALID) ) { #ifdef ECANCELED _SET_ERRNO(ECANCELED); #else _SET_ERRNO(EINVAL); #endif return MUROAR_HANDLE_INVALID; } } // Connect to server: if ( server != NULL ) { if ( (fh = muroar_open_socket(server)) == MUROAR_HANDLE_INVALID ) return MUROAR_HANDLE_INVALID; } else { // build string for ~/.roar home = getenv("HOME"); if ( home != NULL && home[0] == '/' && strlen(home) < (sizeof(useraddr) - 7) ) { snprintf(useraddr, sizeof(useraddr), "%s/.roar", home); useraddr[sizeof(useraddr)-1] = 0; } // go thru list of possible defaults: for (i = 0; fh == MUROAR_HANDLE_INVALID && addr[i] != NULL; i++) { if ( !strcmp(addr[i], MUROAR_INVALID) ) continue; fh = muroar_open_socket(addr[i]); } if ( fh == MUROAR_HANDLE_INVALID ) { _SET_ERRNO(ENOENT); return MUROAR_HANDLE_INVALID; } } // Prepare client name: if ( name == NULL || *name == 0 ) name = _DEFAULT_CLIENT_NAME; // Send IDENTIFY command to server: memset(buf, 0, sizeof(buf)); buf[1] = MUROAR_CMD_IDENTIFY; // Calculate the length for the data part of the package. // Its 5 bytes plus the length of the name string. tmpu16 = strlen(name) + 5; // check if we have space for 5 bytes + length of name + tailing \0 // in the buffer. if ( tmpu16 >= MUROAR_IOBUF ) { _CLOSE(fh); _SET_ERRNO(EINVAL); return MUROAR_HANDLE_INVALID; } buf[8] = (tmpu16 & 0xFF00) >> 8; buf[9] = (tmpu16 & 0x00FF); if ( muroar_write(fh, buf, 10) != 10 ) { _CLOSE(fh); return MUROAR_HANDLE_INVALID; } buf[0] = 1; pid = getpid(); buf[1] = (pid & 0xFF000000UL) >> 24; buf[2] = (pid & 0x00FF0000UL) >> 16; buf[3] = (pid & 0x0000FF00UL) >> 8; buf[4] = (pid & 0x000000FFUL) >> 0; // sizes are already checked. strcpy((char*)&(buf[5]), name); if ( muroar_write(fh, buf, tmpu16) != tmpu16 ) { _CLOSE(fh); return MUROAR_HANDLE_INVALID; } if ( muroar_read(fh, buf, 10) != 10 ) { _CLOSE(fh); return MUROAR_HANDLE_INVALID; } if ( buf[1] != MUROAR_CMD_OK ) { _CLOSE(fh); _SET_ERRNO(EACCES); return MUROAR_HANDLE_INVALID; } // Send AUTH command to server: // We send zero-byte AUTH command // (type=NONE). memset(buf, 0, 10); buf[1] = MUROAR_CMD_AUTH; if ( muroar_write(fh, buf, 10) != 10 ) { _CLOSE(fh); return MUROAR_HANDLE_INVALID; } if ( muroar_read(fh, buf, 10) != 10 ) { _CLOSE(fh); return MUROAR_HANDLE_INVALID; } if ( buf[1] != MUROAR_CMD_OK ) { _CLOSE(fh); _SET_ERRNO(EACCES); return MUROAR_HANDLE_INVALID; } // We now have a working control connection, return it. return fh; }
int rename(const char *from, const char *to) { int n; char *f, *t; Dir *d, nd; if(access(to, 0) >= 0){ if(_REMOVE(to) < 0){ _syserrno(); return -1; } } if((d = _dirstat(to)) != nil){ free(d); errno = EEXIST; return -1; } if((d = _dirstat(from)) == nil){ _syserrno(); return -1; } f = strrchr(from, '/'); t = strrchr(to, '/'); f = f? f+1 : (char *)from; t = t? t+1 : (char *)to; n = 0; if(f-from==t-to && strncmp(from, to, f-from)==0){ /* from and to are in same directory (we miss some cases) */ _nulldir(&nd); nd.name = t; if(_dirwstat(from, &nd) < 0){ _syserrno(); n = -1; } }else{ /* different directories: have to copy */ int ffd, tfd; char buf[8192]; tfd = -1; if((ffd = _OPEN(from, 0)) < 0 || (tfd = _CREATE(to, 1, d->mode)) < 0){ _CLOSE(ffd); _syserrno(); n = -1; } while(n>=0 && (n = _READ(ffd, buf, 8192)) > 0) if(_WRITE(tfd, buf, n) != n){ _syserrno(); n = -1; } _CLOSE(ffd); _CLOSE(tfd); if(n>0) n = 0; if(n == 0) { if(_REMOVE(from) < 0){ _syserrno(); return -1; } } } free(d); return n; }
static int readprocfdinit(void) { /* construct info from /proc/$pid/fd */ char buf[8192]; Fdinfo *fi; int fd, pfd, pid, n, tot, m; char *s, *nexts; memset(buf, 0, sizeof buf); pfd = _OPEN("#c/pid", 0); if(pfd < 0) return -1; if(_PREAD(pfd, buf, 100, 0) < 0){ _CLOSE(pfd); return -1; } _CLOSE(pfd); pid = strtoul(buf, 0, 10); strcpy(buf, "#p/"); _ultoa(buf+3, pid); strcat(buf, "/fd"); pfd = _OPEN(buf, 0); if(pfd < 0) return -1; memset(buf, 0, sizeof buf); tot = 0; for(;;){ n = _PREAD(pfd, buf+tot, sizeof buf-tot, tot); if(n <= 0) break; tot += n; } _CLOSE(pfd); if(n < 0) return -1; buf[sizeof buf-1] = '\0'; s = strchr(buf, '\n'); /* skip current directory */ if(s == 0) return -1; s++; m = 0; for(; s && *s; s=nexts){ nexts = strchr(s, '\n'); if(nexts) *nexts++ = '\0'; errno = 0; fd = strtoul(s, &s, 10); if(errno != 0) return -1; if(fd >= OPEN_MAX) continue; if(fd == pfd) continue; fi = &_fdinfo[fd]; fi->flags = FD_ISOPEN; while(*s == ' ' || *s == '\t') s++; if(*s == 'r'){ m |= 1; s++; } if(*s == 'w'){ m |= 2; } if(m==1) fi->oflags = O_RDONLY; else if(m==2) fi->oflags = O_WRONLY; else fi->oflags = O_RDWR; if(strlen(s) >= 9 && strcmp(s+strlen(s)-9, "/dev/cons") == 0) fi->flags |= FD_ISTTY; } return 0; }
int execve(const char *name, const char *argv[], const char *envp[]) { int n, f, i; char **e, *ss, *se; Fdinfo *fi; unsigned long flags; char nam[256+5]; char buf[1000]; _RFORK(RFCENVG); /* * To pass _fdinfo[] across exec, put lines like * fd flags oflags * in $_fdinfo (for open fd's) */ f = _CREATE("#e/_fdinfo", OWRITE, 0666); ss = buf; for(n = 0; n<OPEN_MAX; n++){ fi = &_fdinfo[n]; flags = fi->flags; if(flags&FD_CLOEXEC){ _CLOSE(n); fi->flags = 0; fi->oflags = 0; }else if(flags&FD_ISOPEN){ ss = _ultoa(ss, n); *ss++ = ' '; ss = _ultoa(ss, flags); *ss++ = ' '; ss = _ultoa(ss, fi->oflags); *ss++ = '\n'; if(ss-buf < sizeof(buf)-50){ _WRITE(f, buf, ss-buf); ss = buf; } } } if(ss > buf) _WRITE(f, buf, ss-buf); _CLOSE(f); /* * To pass _sighdlr[] across exec, set $_sighdlr * to list of blank separated fd's that have * SIG_IGN (the rest will be SIG_DFL). * We write the variable, even if no signals * are ignored, in case the current value of the * variable ignored some. */ f = _CREATE("#e/_sighdlr", OWRITE, 0666); if(f >= 0){ ss = buf; for(i = 0; i <=MAXSIG && ss < &buf[sizeof(buf)]-5; i++) { if(_sighdlr[i] == SIG_IGN) { ss = _ultoa(ss, i); *ss++ = ' '; } } _WRITE(f, buf, ss-buf); _CLOSE(f); } if(envp){ strcpy(nam, "#e/"); for(e = (char **)envp; (ss = *e); e++) { se = strchr(ss, '='); if(!se || ss==se) continue; /* what is name? value? */ n = se-ss; if(n >= sizeof(nam)-3) n = sizeof(nam)-3-1; memcpy(nam+3, ss, n); nam[3+n] = 0; f = _CREATE(nam, OWRITE, 0666); if(f < 0) continue; se++; /* past = */ n = strlen(se); /* temporarily decode nulls (see _envsetup()) */ for(i=0; i < n; i++) if(se[i] == 1) se[i] = 0; _WRITE(f, se, n); /* put nulls back */ for(i=0; i < n; i++) if(se[i] == 0) se[i] = 1; _CLOSE(f); } } n = _EXEC(name, argv); _syserrno(); return n; }
void c_server::init_server() { flag_end_server = false; state123 = 1;//RUN printf("initializing server\n"); sockaddr_in server; //used in this function only to initiate socket memset((uchar*)&server, 0, sizeof(server)); #ifdef __linux__ /*Fill in server's sockaddr_in*/ #define SOCKADDR struct sockaddr #else WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); if (WSAStartup(wVersionRequested, &wsaData) != 0) { cout << "c_server::init_server() WSAStartup() error" << endl; getchar(); return; } #endif /*Fill in server's sockaddr_in*/ server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY;//inet_addr(tcp_id); server.sin_port = htons(PORT_NUM); memset((char *)&anyclient, 0, sizeof(anyclient)); anyclient.sin_family = AF_INET; anyclient.sin_addr.s_addr = INADDR_ANY; anyclient.sin_port = htons(0000);//any client port! int slen = sizeof(anyclient); /*Create socket */ server_socket = socket(AF_INET, SOCK_DGRAM, 0); if (server_socket == -1) { cout << "Error in c_server::init_server(): socket failed"; getchar(); return; } //bind(server_socket, (SOCKADDR*)&server_address, sizeof(SOCKADDR)); if (_BIND(server_socket, (SOCKADDR *)&server, sizeof(server)) == -1) { cout<<"Fail bind()"; getchar(); _CLOSE(server_socket); return; } int buffsize = 24 * UDP1_BUFFER_LONG; //Increasing the socket buffer: if (setsockopt(server_socket, SOL_SOCKET, SO_RCVBUF, (char*)&buffsize, sizeof(int)) == -1) { cout << "Warning at c_server::init_server() could not increase receiver socket size" << endl; getchar(); } else cout << "Increased server socket bufer size" << endl; //the select side! //fd_set select_fd_set;//to be initiated! //struct timeval select_timeval; select_timeval.tv_sec = 0; select_timeval.tv_usec = TIMEOUT_US; FD_ZERO(&select_fd_set0); FD_SET(server_socket, &select_fd_set0); //BLOCKING/NON BLOCKING: #ifdef NONBLOCKING #ifdef __linux__ int opt = 1; ioctl(server_socket, FIONBIO, &opt); #else unsigned long opt; ioctlsocket(server_socket, FIONBIO, &opt); #endif #endif }
void _envsetup(void) { int dfd, fdinited, n, nd, m, i, j, f, nohandle, psize, cnt; char *ps, *p; char **pp; char name[NAME_MAX+5]; Dir *d9, *d9a; static char **emptyenvp = 0; environ = emptyenvp; /* pessimism */ nohandle = 0; fdinited = 0; cnt = 0; strcpy(name, "#e"); dfd = _OPEN(name, 0); if(dfd < 0) return; name[2] = '/'; ps = p = malloc(Envhunk); if(p == 0) return; psize = Envhunk; nd = _dirreadall(dfd, &d9a); _CLOSE(dfd); for(j=0; j<nd; j++) { d9 = &d9a[j]; n = strlen(d9->name); if(n >= sizeof name - 4) continue; /* shouldn't be possible */ m = d9->length; i = p - ps; if(i+n+1+m+1 > psize) { psize += (n+m+2 < Envhunk)? Envhunk : n+m+2; ps = realloc(ps, psize); if (ps == 0) { free(d9a); return; } p = ps + i; } memcpy(p, d9->name, n); p[n] = '='; strcpy(name+3, d9->name); f = _OPEN(name, O_RDONLY); if(f < 0 || _READ(f, p+n+1, m) != m) m = 0; _CLOSE(f); if(p[n+m] == 0) m--; for(i=0; i<m; i++) if(p[n+1+i] == 0) p[n+1+i] = 1; p[n+1+m] = 0; if(strcmp(d9->name, "_fdinfo") == 0) { _fdinit(p+n+1, p+n+1+m); fdinited = 1; } else if(strcmp(d9->name, "_sighdlr") == 0) sigsetup(p+n+1, p+n+1+m); else if(strcmp(d9->name, "nohandle") == 0) nohandle = 1; p += n+m+2; cnt++; } free(d9a); if(!fdinited) _fdinit(0, 0); pp = malloc((1+cnt)*sizeof(char *)); if (pp == 0) return; environ = pp; p = ps; for(i = 0; i < cnt; i++) { *pp++ = p; p = memchr(p, 0, ps+psize-p); if (!p) break; p++; } *pp = 0; if(!nohandle) _NOTIFY(_notehandler); }
int execve(const char *name, const char *argv[], const char *envp[]) { int n, f, i; char **e, *ss, *se; Fdinfo *fi; unsigned long flags; char buf[1024]; _RFORK(RFCENVG); /* * To pass _fdinfo[] across exec, put lines like * fd flags oflags * in $_fdinfo (for open fd's) */ f = _CREATE("/env/_fdinfo", OWRITE, 0666); ss = buf; for(i = 0; i<OPEN_MAX; i++){ if(i == f) continue; fi = &_fdinfo[i]; flags = fi->flags; if(flags&FD_CLOEXEC){ _CLOSE(i); fi->flags = 0; fi->oflags = 0; }else if(flags&FD_ISOPEN){ if(f < 0) continue; ss = _ultoa(ss, i); *ss++ = ' '; ss = _ultoa(ss, flags); *ss++ = ' '; ss = _ultoa(ss, fi->oflags); *ss++ = '\n'; n = ss-buf; if(n > sizeof(buf)-50){ if(_WRITE(f, buf, n) != n) break; ss = buf; } } } if(f >= 0){ if(ss > buf) _WRITE(f, buf, ss-buf); _CLOSE(f); } /* * To pass _sighdlr[] across exec, set $_sighdlr * to list of blank separated fd's that have * SIG_IGN (the rest will be SIG_DFL). * We write the variable, even if no signals * are ignored, in case the current value of the * variable ignored some. */ f = _CREATE("/env/_sighdlr", OWRITE, 0666); if(f >= 0){ ss = buf; for(i = 0; i <=MAXSIG; i++) { if(_sighdlr[i] == SIG_IGN) { ss = _ultoa(ss, i); *ss++ = ' '; n = ss-buf; if(n > sizeof(buf)-20){ if(_WRITE(f, buf, n) != n) break; ss = buf; } } } if(ss > buf) _WRITE(f, buf, ss-buf); _CLOSE(f); } if(envp){ for(e = envp; (ss = *e); e++) { se = strchr(ss, '='); if(!se || ss==se) continue; /* what is name? value? */ n = se-ss; if(n >= sizeof(buf)-5) continue; /* name too long */ strcpy(buf, "/env/"); memcpy(buf+5, ss, n); buf[5+n] = 0; f = _CREATE(buf, OWRITE, 0666); if(f < 0) continue; ss = ++se; /* past = */ se += strlen(ss); while((n = (se - ss)) > 0){ if(n > sizeof(buf)) n = sizeof(buf); /* decode nulls (see _envsetup()) */ for(i=0; i<n; i++) if((buf[i] = ss[i]) == 1) buf[i] = 0; if(_WRITE(f, buf, n) != n) break; ss += n; } _CLOSE(f); } } n = _EXEC(name, argv); _syserrno(); return n; }