static CFIndex fileRead(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLength, CFStreamError *errorCode, Boolean *atEOF, void *info) { _CFFileStreamContext *ctxt = (_CFFileStreamContext *)info; CFIndex result; result = fdRead(ctxt->fd, buffer, bufferLength, errorCode, atEOF); #ifdef REAL_FILE_SCHEDULING if (__CFBitIsSet(ctxt->flags, SCHEDULE_AFTER_READ)) { __CFBitClear(ctxt->flags, SCHEDULE_AFTER_READ); if (!*atEOF && ctxt->rlInfo.cffd) { struct stat statbuf; int ret = fstat(ctxt->fd, &statbuf); if (0 <= ret && (S_IFREG == (statbuf.st_mode & S_IFMT))) { off_t offset = lseek(ctxt->fd, 0, SEEK_CUR); if (statbuf.st_size == offset) { _CFFileDescriptorInduceFakeReadCallBack(ctxt->rlInfo.cffd); } } } if (ctxt->rlInfo.cffd) { CFFileDescriptorEnableCallBacks(ctxt->rlInfo.cffd, kCFFileDescriptorReadCallBack); } } #else if (*atEOF) __CFBitSet(ctxt->flags, AT_EOF); if (ctxt->scheduled > 0 && !*atEOF) { CFReadStreamSignalEvent(stream, kCFStreamEventHasBytesAvailable, NULL); } #endif return result; }
long long test(char** tname) { int i; int j; int t; int n; int iter; pLxSession lxs; pFile fd; char buf[256]; *tname = "mtlexer-20 NODISCARD flag test"; mssInitialize("system", "", "", 0, "test"); iter = 60000; for(i=0;i<iter;i++) { fd = fdOpen("tests/test_mtlexer_20.txt", O_RDONLY, 0600); assert(fd != NULL); lxs = mlxOpenSession(fd, MLX_F_EOL | MLX_F_EOF | MLX_F_NODISCARD); assert(lxs != NULL); for(j=1;j<=3;j++) { t = mlxNextToken(lxs); assert(t == MLX_TOK_KEYWORD); t = mlxNextToken(lxs); assert(t == MLX_TOK_COLON); t = mlxNextToken(lxs); assert(t == MLX_TOK_KEYWORD); t = mlxNextToken(lxs); assert(t == MLX_TOK_EOL); } t = mlxNextToken(lxs); assert(t == MLX_TOK_EOL); mlxCloseSession(lxs); n = fdRead(fd, buf, sizeof(buf) - 1, 0, 0); assert(n >= 0); buf[n] = '\0'; assert(strcmp(buf, "This is some text.\n") == 0); fdClose(fd, 0); } return iter; }
/* Extract the file */ static size_t RPMCopy(install_info *info, const char *path, const char *dest, const char *current_option_name, xmlNodePtr node, int (*update)(install_info *info, const char *path, size_t progress, size_t size, const char *current)) { FD_t fdi; Header hd; size_t size; int_32 type, c; int rc, isSource; void *p; const char *reloc = xmlGetProp(node, "relocate"); const char *autorm = xmlGetProp(node, "autoremove"); const char *depsoff = xmlGetProp(node, "nodeps"); int relocate = (reloc && !strcasecmp(reloc, "true")); int autoremove = (autorm && !strcasecmp(autorm, "true")); int nodeps = (depsoff && !strcasecmp(depsoff, "true")); fdi = fdOpen(path, O_RDONLY, 0644); rc = rpmReadPackageHeader(fdi, &hd, &isSource, NULL, NULL); if ( rc ) { log_warning(_("RPM error: %s"), rpmErrorString()); return 0; } size = 0; if ( rpm_access && ! force_manual ) { /* We can call RPM directly */ char cmd[300]; FILE *fp; float percent = 0.0; double bytes_copied = 0.0; double previous_bytes = 0.0; char *name = "", *version = "", *release = ""; char *options = (char *) malloc(PATH_MAX); options[0] = '\0'; headerGetEntry(hd, RPMTAG_SIZE, &type, &p, &c); if(type==RPM_INT32_TYPE){ size = *(int_32*) p; } headerGetEntry(hd, RPMTAG_RELEASE, &type, &p, &c); if(type==RPM_STRING_TYPE){ release = (char *) p; } headerGetEntry(hd, RPMTAG_NAME, &type, &p, &c); if(type==RPM_STRING_TYPE){ name = (char*)p; } headerGetEntry(hd, RPMTAG_VERSION, &type, &p, &c); if(type==RPM_STRING_TYPE){ version = (char*)p; } fdClose(fdi); if (relocate) { /* force relocating RPM to install directory */ sprintf(options, " --relocate /=%s --badreloc ", dest); } if (nodeps) { strcat(options, " --nodeps "); } snprintf(cmd,sizeof(cmd),"rpm -U --percent --root %s %s %s", rpm_root, options, path); fp = popen(cmd, "r"); while ( percent < 100.0 ) { if(!fp || feof(fp)){ pclose(fp); free(options); log_warning(_("Unable to install RPM file: '%s'"), path); return 0; } fscanf(fp,"%s", cmd); if(strcmp(cmd,"%%")){ pclose(fp); free(options); log_warning(_("Unable to install RPM file: '%s'"), path); return 0; } fscanf(fp,"%f", &percent); /* calculate the bytes installed in this pass of the loop */ bytes_copied = (percent/100.0)*size - previous_bytes; previous_bytes += bytes_copied; info->installed_bytes += bytes_copied; if ( ! update(info, path, (percent/100.0)*size, size, current_option_name) ) break; } pclose(fp); free (options); /* Log the RPM installation */ add_rpm_entry(info, current_option, name, version, atoi(release), autoremove); } else { /* Manually install the RPM file */ gzFile gzdi = NULL; BZFILE *bzdi = NULL; FILE *fd = NULL; unsigned char magic[2]; stream *cpio; if(headerIsEntry(hd, RPMTAG_PREIN)){ headerGetEntry(hd, RPMTAG_PREIN, &type, &p, &c); if(type==RPM_STRING_TYPE) run_script(info, (char*)p, 1, 1); } /* Identify the type of compression for the archive */ if ( fdRead(fdi, magic, 2) < 2 ) { return 0; } lseek(fdFileno(fdi), -2L, SEEK_CUR); if ( magic[0]==037 && magic[1]==0213 ) { gzdi = gzdopen(fdFileno(fdi), "r"); /* XXX gzdi == fdi */ } else if ( magic[0]=='B' && magic[1]=='Z' ) { bzdi = BZDOPEN(fdFileno(fdi), "r"); } else { /* Assume not compressed */ fd = fdopen(fdFileno(fdi), "r"); } cpio = file_fdopen(info, path, fd, gzdi, bzdi, "r"); /* if relocate="true", copy the files into dest instead of rpm_root */ if (relocate) { size = copy_cpio_stream(info, cpio, dest, current_option_name, node, update); } else { size = copy_cpio_stream(info, cpio, rpm_root, current_option_name, node, update); } if(headerIsEntry(hd, RPMTAG_POSTIN)){ headerGetEntry(hd, RPMTAG_POSTIN, &type, &p, &c); if(type==RPM_STRING_TYPE) run_script(info, (char*)p, 1, 1); } /* Append the uninstall scripts to the uninstall */ if(headerIsEntry(hd, RPMTAG_PREUN)){ headerGetEntry(hd, RPMTAG_PREUN, &type, &p, &c); if(type==RPM_STRING_TYPE) add_script_entry(info, current_option, (char*)p, 0); } if(headerIsEntry(hd, RPMTAG_POSTUN)){ headerGetEntry(hd, RPMTAG_POSTUN, &type, &p, &c); if(type==RPM_STRING_TYPE) add_script_entry(info, current_option, (char*)p, 1); } fdClose(fdi); } return size; }
struct gretTap *gretTapConnect(char *addr, int position, unsigned int typeMask) { struct sockaddr_in adr_srvr; /* AF_INET */ struct hostent *hp; struct gretTap *retval; int numrec, firstret[2]; unsigned int outbuf[2]; int numsent; gretTapClientError = GTC_NOERROR; retval = (struct gretTap *) calloc(1, sizeof(struct gretTap)); retval->connectionMutex = epicsMutexCreate(); if (!retval->connectionMutex) { gretTapClientError = GTC_M_CREATE; free(retval); return 0; } memset(&adr_srvr,0,sizeof adr_srvr); adr_srvr.sin_family = AF_INET; adr_srvr.sin_port = htons(GRETTAP_PORT); hp = gethostbyname(addr); if (!hp) { gretTapClientError = GTC_HOSTNAME; free(retval); return 0; } adr_srvr.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *) (hp->h_addr_list[0])))); if ( adr_srvr.sin_addr.s_addr == INADDR_NONE ) { gretTapClientError = GTC_INADDR; free(retval); return 0; } retval->inSock = socket(AF_INET, SOCK_STREAM, 0); if (retval->inSock == -1) { gretTapClientError = GTC_INSOCK; free(retval); return 0; } if (connect(retval->inSock, (struct sockaddr *)&adr_srvr, sizeof(adr_srvr)) < 0) { gretTapClientError = GTC_TAPCONN; close(retval->inSock); free(retval); return 0; } /* send two ints: type and position */ outbuf[0] = typeMask; outbuf[1] = position; numsent = fdWrite(retval->inSock, outbuf, 2 * sizeof(unsigned int)); if (numsent != 2 * sizeof(unsigned int)){ gretTapClientError = GTC_INITWRITE; close(retval->inSock); free(retval); return 0; } numrec = fdRead(retval->inSock, firstret, TAP_HEADER_LEN); if (numrec != TAP_HEADER_LEN) { gretTapClientError = GTC_INITREAD; close(retval->inSock); free(retval); return 0; } if (firstret[0] != TAP_ACK) { switch (firstret[0]) { case TAP_NOT_FOUND: gretTapClientError = GTC_NOT_FOUND; break; case TAP_NOT_RUNNING: gretTapClientError = GTC_NOT_RUNNING; break; default: gretTapClientError = GTC_UNKNOWN; } close(retval->inSock); free(retval); return 0; } return (retval); }
struct GEBData *gretTapData(struct gretTap *tap, int nreq, float timeout) { int numrec, i; int outbuf[2]; int firstret[2] = {0,0}; struct GEBData *retval=0, *inbuf = 0; gretTapClientError = GTC_NOERROR; if (tap->inSock == 0) { gretTapClientError = GTC_CLOSED; return 0; } outbuf[0] = nreq; if (timeout < 0) { outbuf[1] = 0; } else { outbuf[1] = (int)(timeout * 1000.0); } if (fdWrite(tap->inSock, outbuf, TAP_HEADER_LEN) != TAP_HEADER_LEN) { gretTapClientError = GTC_HEADER_WRITE; close(tap->inSock); tap->inSock = 0; return 0; } numrec = fdRead(tap->inSock, firstret, TAP_HEADER_LEN); if (numrec != TAP_HEADER_LEN) { if (numrec) { gretTapClientError = GTC_HEADER_READ; } else { gretTapClientError = GTC_CLOSED; } close(tap->inSock); tap->inSock = 0; return 0; } if (firstret[0] == TAP_TIMEOUT) { gretTapClientError = GTC_TIMEOUT; } if (firstret[0] == TAP_NOT_RUNNING) { gretTapClientError = GTC_NOT_RUNNING; } if (firstret[1] == 0) { return 0; } for (i = 0; i < firstret[1]; i++) { if (!retval) { inbuf = (struct GEBData *) calloc(1, sizeof(struct GEBData)); retval = inbuf; } else { inbuf->next = (struct GEBData *) calloc(1, sizeof(struct GEBData)); inbuf = inbuf->next; } numrec = fdRead(tap->inSock, inbuf, GEB_HEADER_BYTES); if (numrec != GEB_HEADER_BYTES) { gretTapClientError = GTC_GEB_HEADER; gretTapDataFree(retval); close(tap->inSock); tap->inSock = 0; } #if(1) if (inbuf == NULL) { printf("inbuf=0x%p, return GTC_LEN_0\n",inbuf); fflush(stdout); gretTapClientError = GTC_LEN_0; inbuf->next = (struct GEBData *) calloc(1, sizeof(struct GEBData)); inbuf = inbuf->next; continue; } #endif if (inbuf->length == 0) { gretTapClientError = GTC_LEN_0; inbuf->next = (struct GEBData *) calloc(1, sizeof(struct GEBData)); inbuf = inbuf->next; continue; } inbuf->payload = calloc(1, inbuf->length); if (!inbuf->payload) { /* if length rediculous this could happen ? */ gretTapClientError = GTC_LEN_HUGE; inbuf->next = (struct GEBData *) calloc(1, sizeof(struct GEBData)); inbuf = inbuf->next; continue; } numrec = fdRead(tap->inSock, inbuf->payload, inbuf->length); if (numrec != inbuf->length) { gretTapClientError = GTC_DATA_READ; gretTapDataFree(retval); close(tap->inSock); tap->inSock = 0; } } return retval; }
/*** nht_internal_PUT - implements the PUT HTTP method. Set content_buf to *** data to write, otherwise it will be read from the connection if content_buf *** is NULL. ***/ int nht_internal_PUT(pNhtSessionData nsess, pFile conn, pStruct url_inf, int size, char* content_buf) { pObject target_obj; char sbuf[160]; int rcnt,wcnt; int type,i,v; pStruct sub_inf; int already_exist=0; /** See if the object already exists. **/ target_obj = objOpen(nsess->ObjSess, url_inf->StrVal, O_RDONLY, 0600, "text/html"); if (target_obj) { objClose(target_obj); already_exist = 1; } /** Ok, open the object here. **/ target_obj = objOpen(nsess->ObjSess, url_inf->StrVal, O_WRONLY | O_CREAT | O_TRUNC, 0600, "text/html"); if (!target_obj) { sprintf(sbuf,"HTTP/1.0 404 Not Found\r\n" "Server: Centrallix/1.0\r\n" "\r\n" "<H1>404 Not Found</H1><HR><PRE>\r\n"); fdWrite(conn,sbuf,strlen(sbuf),0,0); mssPrintError(conn); netCloseTCP(conn,1000,0); thExit(); } /** OK, we're ready. Send the 100 Continue message. **/ /*sprintf(sbuf,"HTTP/1.1 100 Continue\r\n" "Server: Centrallix/1.0\r\n" "\r\n"); fdWrite(conn,sbuf,strlen(sbuf),0,0);*/ /** If size specified, set the size. **/ if (size != -1) objSetAttrValue(target_obj, "size", &size); /** Set any attributes specified in the url inf **/ for(i=0;i<url_inf->nSubInf;i++) { sub_inf = url_inf->SubInf[i]; type = objGetAttrType(target_obj, sub_inf->Name); if (type == DATA_T_INTEGER) { v = strtoi(sub_inf->StrVal,NULL,10); objSetAttrValue(target_obj, sub_inf->Name, &v); } else if (type == DATA_T_STRING) { objSetAttrValue(target_obj, sub_inf->Name, &(sub_inf->StrVal)); } } /** If content_buf, write that else write from the connection. **/ if (content_buf) { while(size != 0) { rcnt = (size>1024)?1024:size; objWrite(target_obj, content_buf, rcnt, 0,0); size -= rcnt; content_buf += rcnt; } } else { /** Ok, read from the connection, either until size bytes or until EOF. **/ while(size != 0 && (rcnt=fdRead(conn,sbuf,160,0,0)) > 0) { if (size > 0) { size -= rcnt; if (size < 0) { rcnt += size; size = 0; } } if (objWrite(target_obj, sbuf, rcnt, 0,0) < 0) break; } } /** Close the object. **/ objClose(target_obj); /** Ok, issue the HTTP header for this one. **/ if (nsess->IsNewCookie) { if (already_exist) { sprintf(sbuf,"HTTP/1.0 200 OK\r\n" "Server: Centrallix/1.0\r\n" "Set-Cookie: %s\r\n" "\r\n" "%s\r\n", nsess->Cookie, url_inf->StrVal); } else { sprintf(sbuf,"HTTP/1.0 201 Created\r\n" "Server: Centrallix/1.0\r\n" "Set-Cookie: %s\r\n" "\r\n" "%s\r\n", nsess->Cookie, url_inf->StrVal); } nsess->IsNewCookie = 0; } else { if (already_exist) { sprintf(sbuf,"HTTP/1.0 200 OK\r\n" "Server: Centrallix/1.0\r\n" "\r\n" "%s\r\n", url_inf->StrVal); } else { sprintf(sbuf,"HTTP/1.0 201 Created\r\n" "Server: Centrallix/1.0\r\n" "\r\n" "%s\r\n", url_inf->StrVal); } } fdWrite(conn,sbuf,strlen(sbuf),0,0); return 0; }
/*** shlRead - Read data from the shell's stdout ***/ int shlRead(void* inf_v, char* buffer, int maxcnt, int offset, int flags, pObjTrxTree* oxt) { pShlData inf = SHL(inf_v); int i=-1; int waitret; int retval; if(SHELL_DEBUG & SHELL_DEBUG_IO) printf("%s -- %p, %p, %i, %i, %i, %p\n",__FUNCTION__,inf_v,buffer,maxcnt,offset,flags,oxt); /** launch the program if it's not running already **/ if(inf->shell_pid == -1) if(shl_internal_Launch(inf) < 0) return -1; /** can't seek backwards on a stream :) **/ if(flags & FD_U_SEEK) { if(offset>inf->curRead) { /** scroll forward to point in the stream to read from **/ while(offset>inf->curRead) { char buf[1024]; int i; int readlen = offset-inf->curRead>1024?1024:offset-inf->curRead; /** call shlRead instead of fdRead directly, as it has extra protection and this ** will only ever go to one level of recursion **/ i=shlRead(inf,buf,readlen,0,flags & ~FD_U_SEEK,oxt); if(i==-1) return -1; inf->curRead+=i; } } /** this'll also catch if we scroll too far forward... **/ if(offset<inf->curRead) return -1; } while(i < 0) { i=fdRead(inf->shell_fd,buffer,maxcnt,0,flags & ~FD_U_SEEK); if(i < 0) { /** if we get EIO, set the done flag **/ if(errno == EIO) { inf->done = 1; return -1; } /** user doesn't want us to block **/ if(flags & FD_U_NOBLOCK) return -1; /** if there is no more child process, that's probably why we can't read :) **/ if(inf->shell_pid==0) return -1; shl_internal_UpdateStatus(inf); if(inf->shell_pid>0) { /** child is alive, it just doesn't have any data yet -- wait for some **/ thSleep(200); } else { /** child just died -- retry the read **/ } } } inf->curRead+=i; return i; }
void start(void* v) { char enc_pass[64]; char salt[MSS_SALT_SIZE + 1]; char passwd_line[128]; XString passwd_contents; pFile passwd_file; char buf[256]; int len; int offset; int pos; int uname_len; int found_user; size_t found_len; char* nlptr; char* ptr; cxssInitialize(); /** No file specified? **/ if (!CXPASSWD.PasswdFile[0]) { puts("no passwd file specified (use option -f)."); exit(1); } /** User asked us to read password from stdin? **/ if (CXPASSWD.ReadStdin) { fgets(CXPASSWD.Password, sizeof(CXPASSWD.Password), stdin); if (strchr(CXPASSWD.Password, '\n')) { *strchr(CXPASSWD.Password, '\n') = '\0'; } } /** Now get username and/or password if not supplied on command line **/ if (!CXPASSWD.UserName[0]) { ptr = readline("Username: "******"Password: "******"could not generate random bytes for password salt."); exit(1); } /** Generate encrypted credential **/ if (mssGenCred(salt, MSS_SALT_SIZE, CXPASSWD.Password, enc_pass, sizeof(enc_pass)) < 0) { puts("could not generate encrypted password."); exit(1); } /** Generate our password file line **/ snprintf(passwd_line, sizeof(passwd_line), "%s:%s\n", CXPASSWD.UserName, enc_pass); /** Open the password file **/ passwd_file = fdOpen(CXPASSWD.PasswdFile, O_RDWR | O_CREAT, 0600); if (!passwd_file) { puts("could not open passwd file."); exit(1); } /** Read it into the xstring **/ xsInit(&passwd_contents); while((len = fdRead(passwd_file, buf, sizeof(buf), 0, 0)) > 0) { xsConcatenate(&passwd_contents, buf, len); } fdClose(passwd_file, 0); /** Do we already have this user? **/ uname_len = strlen(CXPASSWD.UserName); offset = 0; found_user = -1; while((pos = xsFind(&passwd_contents, CXPASSWD.UserName, uname_len, offset)) >= 0) { if ((pos == 0 || xsString(&passwd_contents)[pos-1] == '\n') && xsString(&passwd_contents)[pos+uname_len] == ':') { /** Found it **/ found_user = pos; found_len = strlen(xsString(&passwd_contents)+pos); if ((nlptr = strchr(xsString(&passwd_contents)+pos, '\n')) != NULL) found_len = (nlptr - (xsString(&passwd_contents)+pos)) + 1; break; } offset = pos+1; } /** Replace if found, otherwise add the new user **/ if (found_user >= 0) xsSubst(&passwd_contents, found_user, found_len, passwd_line, strlen(passwd_line)); else xsConcatenate(&passwd_contents, passwd_line, strlen(passwd_line)); /** Rewrite the entire file **/ passwd_file = fdOpen(CXPASSWD.PasswdFile, O_RDWR | O_CREAT | O_TRUNC, 0600); if (!passwd_file) { puts("could not open password file to write to it."); exit(1); } if (fdWrite(passwd_file, xsString(&passwd_contents), strlen(xsString(&passwd_contents)), 0, FD_U_SEEK | FD_U_PACKET) != strlen(xsString(&passwd_contents))) { puts("could not re-write new password file."); exit(1); } /** Close the file **/ fdClose(passwd_file, 0); exit(0); }