void so_listen(int fd) { int r; osenter(); r = listen(fd, 256); osleave(); if(r < 0) oserror(); }
static int audio_set_rate(struct audio_info_struct *ai, ALconfig config) { int dev = alGetDevice(config); ALpv params[1]; /* Make sure the device is OK */ if (dev < 0) { fprintf(stderr,"audio_set_rate : %s\n",alGetErrorString(oserror())); return 1; } params[0].param = AL_OUTPUT_RATE; params[0].value.ll = alDoubleToFixed(ai->rate); if (alSetParams(dev, params,1) < 0) fprintf(stderr,"audio_set_rate : %s\n",alGetErrorString(oserror())); return 0; }
static int set_rate(audio_output_t *ao, ALconfig config) { int dev = alGetDevice(config); ALpv params[1]; /* Make sure the device is OK */ if (dev < 0) { error1("set_rate: %s",alGetErrorString(oserror())); return 1; } params[0].param = AL_OUTPUT_RATE; params[0].value.ll = alDoubleToFixed(ao->rate); if (alSetParams(dev, params,1) < 0) error1("set_rate: %s",alGetErrorString(oserror())); return 0; }
static long lfdread(Chan *c, void *buf, long n, vlong off) { USED(off); /* can't pread on pipes */ n = read((int)(uintptr)c->aux, buf, n); if(n < 0){ iprint("error %d\n", errno); oserror(); } return n; }
int clipwrite(char *buf) { HANDLE h; char *p, *e; Rune16 *rp; int n = strlen(buf); if(!OpenClipboard(window)) { oserror(); return -1; } if(!EmptyClipboard()) { oserror(); CloseClipboard(); return -1; } h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, (n+1)*sizeof(Rune)); if(h == NULL) panic("out of memory"); rp = GlobalLock(h); utftorunes16(rp, buf, n+1); GlobalUnlock(h); SetClipboardData(CF_UNICODETEXT, h); h = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, n+1); if(h == NULL) panic("out of memory"); p = GlobalLock(h); memcpy(p, buf, n); p[n] = 0; GlobalUnlock(h); SetClipboardData(CF_TEXT, h); CloseClipboard(); return n; }
void osproc(Proc *p) { DWORD tid; if(CreateThread(0, 0, tramp, p, 0, &tid) == 0) { oserror(); panic("osproc: %r"); } Sleep(0); }
void osnewproc(Proc *p) { Oproc *op; op = (Oproc*)p->oproc; op->sema = CreateSemaphore(0, 0, 1000, 0); if (op->sema == 0) { oserror(); panic("could not create semaphore: %r"); } }
int fileread(int fd,void *buf,ub4 len,const char *name) { long n; if (len == 0) return error(0,"nil read from %s",name); n = osread(fd,buf,len); if (n == -1) return oserror(0,"cannot read from %s",name); else if (n == 0) return error(0,"eof on reading \ah%u bytes from %s",len,name); else if (n != (long)len) return error(0,"partial read \ah%ld of \ah%u bytes from %s",n,len,name); else return 0; }
static long cmdread(Chan *ch, void *a, long n, vlong offset) { Conv *c; char *p, *cmds; int fd; USED(offset); p = a; switch(TYPE(ch->qid)) { default: error(Eperm); case Qcmd: case Qtopdir: case Qconvdir: return devdirread(ch, a, n, 0, 0, cmdgen); case Qctl: sprint(up->genbuf, "%ld", CONV(ch->qid)); return readstr(offset, p, n, up->genbuf); case Qstatus: c = cmd.conv[CONV(ch->qid)]; cmds = ""; if(c->cmd != nil) cmds = c->cmd->f[1]; snprint(up->genbuf, sizeof(up->genbuf), "cmd/%d %d %s %q %q\n", c->x, c->inuse, c->state, c->dir, cmds); return readstr(offset, p, n, up->genbuf); case Qdata: case Qstderr: fd = 1; if(TYPE(ch->qid) == Qstderr) fd = 2; c = cmd.conv[CONV(ch->qid)]; qlock(&c->l); if(c->fd[fd] == -1){ qunlock(&c->l); return 0; } qunlock(&c->l); osenter(); n = read(c->fd[fd], a, n); osleave(); if(n < 0) oserror(); return n; case Qwait: c = cmd.conv[CONV(ch->qid)]; return qread(c->waitq, a, n); } }
/* * This routine opens the config file and stores the information in the * mounts structure. The mounts structure must be reallocated as * necessary, and also the num_procs structure needs to be reallocated * as we define new mounts. When all of that is done, we fill in the * values in the indomtab structure, those being the number of instances * and the pointer to the mounts structure. */ static void mounts_grab_config_info(void) { FILE *fp; char mount_name[MAXPATHLEN]; char *q; size_t size; int mount_number = 0; int sep = __pmPathSeparator(); snprintf(mypath, sizeof(mypath), "%s%c" "mounts" "%c" "mounts.conf", pmGetConfig("PCP_PMDAS_DIR"), sep, sep); if ((fp = fopen(mypath, "r")) == NULL) { __pmNotifyErr(LOG_ERR, "fopen on %s failed: %s\n", mypath, pmErrStr(-oserror())); if (mounts) { free(mounts); mounts = NULL; mount_number = 0; } goto done; } while (fgets(mount_name, sizeof(mount_name), fp) != NULL) { if (mount_name[0] == '#') continue; /* Remove the newline */ if ((q = strchr(mount_name, '\n')) != NULL) { *q = '\0'; } else { /* This means the line was too long */ __pmNotifyErr(LOG_WARNING, "line %d in the config file too long\n", mount_number+1); } size = (mount_number + 1) * sizeof(pmdaInstid); if ((mounts = realloc(mounts, size)) == NULL) __pmNoMem("process", size, PM_FATAL_ERR); mounts[mount_number].i_name = malloc(strlen(mount_name) + 1); strcpy(mounts[mount_number].i_name, mount_name); mounts[mount_number].i_inst = mount_number; mount_number++; } fclose(fp); done: if (mounts == NULL) __pmNotifyErr(LOG_WARNING, "\"mounts\" instance domain is empty"); indomtab[MOUNTS_INDOM].it_set = mounts; indomtab[MOUNTS_INDOM].it_numinst = mount_number; mount_list = realloc(mount_list, (mount_number)*sizeof(mountinfo)); }
static void copy_fd(int i, int o) { char buf[CPBUFSIZE]; ssize_t n; while( (n=read(i, buf, sizeof(buf))) > 0 ) { while( n > 0 ) { ssize_t n2; if ( (n2 = write(o, buf, n)) > 0 ) { n -= n2; } else { fprintf(stderr, "%s: write failed: %s\n", plld, oserror()); error(1); } } } if ( n < 0 ) { fprintf(stderr, "%s: read failed: %s\n", plld, oserror()); error(1); } }
char* clipread(void) { HANDLE h; uchar *p; if(!OpenClipboard(window)) { oserror(); return strdup(""); } if((h = GetClipboardData(CF_UNICODETEXT))) p = clipreadunicode(h); else if((h = GetClipboardData(CF_TEXT))) p = clipreadutf(h); else { oserror(); p = strdup(""); } CloseClipboard(); return p; }
static int set_channels(audio_output_t *ao, ALconfig config) { int ret; if(ao->channels == 2) ret = alSetChannels(config, AL_STEREO); else ret = alSetChannels(config, AL_MONO); if (ret < 0) error1("set_channels : %s",alGetErrorString(oserror())); return 0; }
static const char * stringErrorReport(void) { #if ( defined(sgi) ) return strerror(oserror()); #elif ( defined(_AIX) ) return strerror(errno); #else if ( errno > 0 && errno < sys_nerr ) return sys_errlist[errno]; else return "Unknown error.\n"; #endif }
int __pmtracexmitPDU(int fd, __pmTracePDU *pdubuf) { int n, len; __pmTracePDUHdr *php = (__pmTracePDUHdr *)pdubuf; #if defined(HAVE_SIGPIPE) SIG_PF user_onpipe; user_onpipe = signal(SIGPIPE, SIG_IGN); if (user_onpipe != SIG_DFL) /* put user handler back */ signal(SIGPIPE, user_onpipe); #endif php->from = (__int32_t)getpid(); #ifdef PMTRACE_DEBUG if (__pmstate & PMTRACE_STATE_PDU) { int j; int jend = (php->len+(int)sizeof(__pmTracePDU)-1)/(int)sizeof(__pmTracePDU); char *p; /* for Purify ... */ p = (char *)pdubuf + php->len; while (p < (char *)pdubuf + jend*sizeof(__pmTracePDU)) *p++ = '~'; /* buffer end */ fprintf(stderr, "[%d]__pmtracexmitPDU: %s fd=%d len=%d", php->from, pdutypestr(php->type), fd, php->len); for (j = 0; j < jend; j++) { if ((j % 8) == 0) fprintf(stderr, "\n%03d: ", j); fprintf(stderr, "%8x ", pdubuf[j]); } putc('\n', stderr); } #endif len = php->len; php->len = htonl(php->len); php->from = htonl(php->from); php->type = htonl(php->type); n = (int)__pmWrite(fd, pdubuf, len); php->len = ntohl(php->len); php->from = ntohl(php->from); php->type = ntohl(php->type); if (n != len) return -oserror(); return n; }
int __pmAFregister(const struct timeval *delta, void *data, void (*func)(int, void *)) { qelt *qp; struct timeval now; struct timeval interval; if (PM_MULTIPLE_THREADS(PM_SCOPE_AF)) return PM_ERR_THREAD; if (!block) AFhold(); if (afid == 0x8000 && !block) /* first time */ AFrearm(); if ((qp = (qelt *)malloc(sizeof(qelt))) == NULL) { return -oserror(); } qp->q_afid = ++afid; qp->q_data = data; qp->q_delta = *delta; qp->q_func = func; __pmtimevalNow(&qp->q_when); tadd(&qp->q_when, &qp->q_delta); enqueue(qp); if (root == qp) { /* we ended up at the head of the list, set itimer */ interval = qp->q_when; __pmtimevalNow(&now); tsub(&interval, &now); if (interval.tv_sec == 0 && interval.tv_usec < MIN_ITIMER_USEC) /* use minimal delay (platform dependent) */ interval.tv_usec = MIN_ITIMER_USEC; #ifdef PCP_DEBUG if (pmDebug & DBG_TRACE_AF) { __pmPrintStamp(stderr, &now); fprintf(stderr, " AFsetitimer for delta "); printdelta(stderr, &interval); fputc('\n', stderr); } #endif AFsetitimer(&interval); } if (!block) AFrelse(); return qp->q_afid; }
static int audio_set_channels(struct audio_info_struct *ai, ALconfig config) { int ret; if(ai->channels == 2) ret = alSetChannels(config, AL_STEREO); else ret = alSetChannels(config, AL_MONO); if (ret < 0) fprintf(stderr,"audio_set_channels : %s\n",alGetErrorString(oserror())); return 0; }
void so_bind(int fd, int su, uchar *addr, ushort port) { int i, one; struct sockaddr_storage sa; struct sockaddr_in6 *sin6; sin6 = (struct sockaddr_in6*)&sa; one = 1; if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0) { oserrstr(up->genbuf, sizeof(up->genbuf)); print("setsockopt: %s", up->genbuf); } if(su) { for(i = 600; i < 1024; i++) { memset(&sa, 0, sizeof(sa)); sin6->sin6_family = AF_INET6; memmove(&sin6->sin6_addr, addr, IPaddrlen); hnputs(&sin6->sin6_port, i); if(bind(fd, (struct sockaddr*)sin6, sizeof(*sin6)) >= 0) return; } oserror(); } memset(&sa, 0, sizeof(sa)); sin6->sin6_family = AF_INET6; memmove(&sin6->sin6_addr, addr, IPaddrlen); hnputs(&sin6->sin6_port, port); if(bind(fd, (struct sockaddr*)sin6, sizeof(*sin6)) < 0) oserror(); }
static boolean filegetprefsspec (bigstring fname, tybrowserspec *fsprefs) { short vnum; long dirid; OSErr ec; ec = FindFolder (kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &vnum, &dirid); if (oserror (ec)) return (false); claymakespec (vnum, dirid, fname, fsprefs); return (true); } /*filegetprefsspec*/
/* Set the sample rate of an audio port. */ void setrate (ALport port, double rate) { int rv; ALpv params; rv = alGetResource(port); params.param = AL_RATE; params.value.ll = alDoubleToFixed(rate); if (alSetParams(rv, ¶ms, 1) < 0) { printf("alSetParams failed: %s\n", alGetErrorString(oserror())); } }
void so_bind(int fd, int su, uchar *addr, ushort port) { int i, one; struct sockaddr sa; struct sockaddr_in *sin; sin = (struct sockaddr_in*)&sa; one = 1; // if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)) < 0) { // oserrstr(up->genbuf, sizeof(up->genbuf)); // print("setsockopt: %s", err); // } if(su) { for(i = 600; i < 1024; i++) { memset(&sa, 0, sizeof(sa)); sin->sin_family = AF_INET; memmove(&sin->sin_addr.s_addr, addr+IPv4off, IPv4addrlen); hnputs(&sin->sin_port, i); if(bind(fd, &sa, sizeof(sa)) >= 0) return; } oserror(); } memset(&sa, 0, sizeof(sa)); sin->sin_family = AF_INET; memmove(&sin->sin_addr.s_addr, addr+IPv4off, IPv4addrlen); hnputs(&sin->sin_port, port); if(bind(fd, &sa, sizeof(sa)) < 0) oserror(); }
static long fswrite(Chan *c, void *va, long n, vlong offset) { int r; UnixFd *ufd; ufd = c->aux; if(ufd->issocket) r = write(ufd->fd, va, n); else r = pwrite(ufd->fd, va, n, offset); if(r < 0) oserror(); return r; }
static int set_format(audio_output_t *ao, ALconfig config) { if(ao->format == MPG123_ENC_FLOAT_32) { if(alSetSampFmt(config, AL_SAMPFMT_FLOAT) < 0) { error1("SetSampFmt: %s", alGetErrorString(oserror())); return -1; } } else { if(alSetSampFmt(config, AL_SAMPFMT_TWOSCOMP) < 0) { error1("SetSampFmt: %s", alGetErrorString(oserror())); return -1; } if(alSetWidth(config, AL_SAMPLE_16) < 0) { error1("SetWidth: %s", alGetErrorString(oserror())); return -1; } } return 0; }
static int setup(char *filename, pmOptions *opts) { FILE *fp; size_t length; int sts = 0, ended = 0; if (filename) fp = fopen(filename, "r"); else fp = fdopen(STDIN_FILENO, "r"); if (!fp) { fprintf(stderr, "%s: cannot open %s for reading configuration\n", pmProgname, filename? filename : "<stdin>"); return -oserror(); } while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) { lineno++; length = strlen(buffer); if (length > 0 && buffer[length-1] == '\n') buffer[--length] = '\0'; /* * Check for special command character (hash) - if found process * the few commands it can represent -> getopts/usage/end. * If we're finished already, we just tack the text on unchanged. * Otherwise, we must deal with regular long options/headers/text. */ if (ended) sts = append_text(opts, buffer, length); else if (buffer[0] == '#') sts = ended = command(opts, buffer + 1); else sts = options(opts, buffer, length); if (sts < 0) break; } fclose(fp); /* if not given a getopt string with short options, just make one */ if (sts >= 0 && !opts->short_options) opts->short_options = build_short_options(opts); return sts; }
/* * Return 1 if hostname corresponds to the current host, 0 if not and < 0 for * an error. */ int __pmIsLocalhost(const char *hostname) { int sts = 0; if (strcasecmp(hostname, "localhost") == 0 || strncmp(hostname, "local:", 6) == 0 || strncmp(hostname, "unix:", 5) == 0) return 1; else { char lhost[MAXHOSTNAMELEN+1]; __pmHostEnt *servInfo1; if (gethostname(lhost, MAXHOSTNAMELEN) < 0) return -oserror(); if ((servInfo1 = __pmGetAddrInfo(lhost)) != NULL) { __pmHostEnt *servInfo2; __pmSockAddr *addr1, *addr2; void *enumIx1, *enumIx2; if ((servInfo2 = __pmGetAddrInfo(hostname)) == NULL) { __pmHostEntFree(servInfo1); return -EHOSTUNREACH; } enumIx1 = NULL; for (addr1 = __pmHostEntGetSockAddr(servInfo1, &enumIx1); addr1 != NULL; addr1 = __pmHostEntGetSockAddr(servInfo1, &enumIx1)) { enumIx2 = NULL; for (addr2 = __pmHostEntGetSockAddr(servInfo2, &enumIx2); addr2 != NULL; addr2 = __pmHostEntGetSockAddr(servInfo2, &enumIx2)) { if (__pmSockAddrCompare(addr1, addr2) == 0) { __pmHostEntFree(servInfo1); __pmHostEntFree(servInfo2); return 1; } } } __pmHostEntFree(servInfo1); __pmHostEntFree(servInfo2); } } return sts; }
/* * Sleep for a specified amount of time */ void __pmtimevalSleep(struct timeval interval) { struct timespec delay; struct timespec left; int sts; delay.tv_sec = interval.tv_sec; delay.tv_nsec = interval.tv_usec * 1000; for (;;) { /* loop to catch early wakeup by nanosleep */ sts = nanosleep(&delay, &left); if (sts == 0 || (sts < 0 && oserror() != EINTR)) break; delay = left; } }
void so_setsockopt(int fd, int opt, int value) { int r; struct linger l; if(opt == SO_LINGER){ l.l_onoff = 1; l.l_linger = (short) value; osenter(); r = setsockopt(fd, SOL_SOCKET, opt, (char *)&l, sizeof(l)); osleave(); }else error(Ebadctl); if(r < 0) oserror(); }
void osinit(void) { Oproc *t; static Proc firstprocCTstore; _setproc(&firstprocCTstore); t = (Oproc*)firstprocCTstore.oproc; assert(t != 0); t->tid = GetCurrentThreadId(); t->sema = CreateSemaphore(0, 0, 1000, 0); if(t->sema == 0) { oserror(); panic("could not create semaphore: %r"); } }
static long fsread(Chan *c, void *va, long n, vlong offset) { int r; UnixFd *ufd; if(c->qid.type & QTDIR) return fsdirread(c, va, n, offset); ufd = c->aux; if(ufd->issocket) r = read(ufd->fd, va, n); else r = pread(ufd->fd, va, n, offset); if(r < 0) oserror(); return r; }
static DWORD WINAPI tramp(LPVOID vp) { Proc *p = (Proc *) vp; Oproc *op = (Oproc*) p->oproc; _setproc(p); op->tid = GetCurrentThreadId(); op->sema = CreateSemaphore(0, 0, 1000, 0); if(op->sema == 0) { oserror(); panic("could not create semaphore: %r"); } (*p->fn)(p->arg); ExitThread(0); return 0; }