static int _cfg_line_dump(ACL_FILE_HANDLE filefd, const ACL_CFG_LINE *cfg_line, const char *delimiter) { char myname[] = "_cfg_line_dump"; char *pbuf, *ptr; int dlen = 0, i, j, n; char tbuf[256]; n = cfg_line->ncount; if (delimiter != NULL) j = (int) strlen(delimiter); else j = 0; if (cfg_line->value != NULL) { for (i = 0; i < n; i++) { if (cfg_line->value[i] == NULL) break; dlen += (int) strlen(cfg_line->value[i]) + j; } dlen += 2; /* for '\n' and '\0' */ pbuf = (char *) acl_mycalloc(1, dlen); ptr = pbuf; for (i = 0; i < n; i++) { if (cfg_line->value[i] == NULL) break; if (i < n -1) sprintf(ptr, "%s%s", cfg_line->value[i], delimiter); else sprintf(ptr, "%s", cfg_line->value[i]); ptr = ptr + strlen(ptr); } ptr = ptr + strlen(ptr); strcat(ptr, "\n\0"); i = acl_file_write(filefd, pbuf, strlen(pbuf), 0, NULL, NULL); if (i <= 0) { printf("%s: can't write pbuf, error=%s\n", myname, acl_last_strerror(tbuf, sizeof(tbuf))); acl_myfree(pbuf); return (-1); } } else if (cfg_line->pdata != NULL) { dlen = (int) strlen(cfg_line->pdata) + 2; pbuf = (char *) acl_mycalloc(1, dlen); if (pbuf == NULL) return (-1); sprintf(pbuf, "%s\n", cfg_line->pdata); i = acl_file_write(filefd, pbuf, strlen(pbuf), 0, NULL, NULL); if (i <= 0) return (-1); } return (0); }
static void test(void) { ACL_FILE_HANDLE fds[2]; char buf[1024]; int ret; if (acl_pipe(fds) < 0) { printf("acl_pipe error(%s)\n", acl_last_serror()); return; } sprintf(buf, "hello client"); ret = acl_file_write(fds[0], buf, strlen(buf), 0, 0); if (ret == ACL_VSTREAM_EOF) { printf("write to client error(%s)\n", acl_last_serror()); acl_pipe_close(fds); return; } printf(">>>server: write to client ok\n"); ret = acl_file_read(fds[1], buf, sizeof(buf), 0, 0); if (ret == ACL_VSTREAM_EOF) { printf(">>>client: read from server error(%s)\n", acl_last_serror()); acl_pipe_close(fds); return; } buf[ret] = 0; printf(">>>client: read from server ok(%s)\n", buf); sprintf(buf, "hello server"); ret = acl_file_write(fds[1], buf, strlen(buf), 0, 0); if (ret == ACL_VSTREAM_EOF) { printf("write to server error(%s)\n", acl_last_serror()); acl_pipe_close(fds); return; } printf(">>>client: write to server ok\n"); ret = acl_file_read(fds[0], buf, sizeof(buf), 0, 0); if (ret == ACL_VSTREAM_EOF) { printf(">>>server: read from client error(%s)\n", acl_last_serror()); acl_pipe_close(fds); return; } printf(">>>server: read from client ok(%s)\n", buf); acl_pipe_close(fds); }
void tls_prng_exch_update(TLS_PRNG_SRC *eh) { const char *myname = "tls_prng_exch_update"; unsigned char buffer[TLS_PRNG_EXCH_SIZE]; ssize_t count; /* * Update the PRNG exchange file. Since other processes may have added * entropy, we use a read-stir-write cycle. */ if (acl_myflock(eh->fd.file, ACL_INTERNAL_LOCK, ACL_MYFLOCK_OP_EXCLUSIVE) != 0) acl_msg_fatal("%s: cannot lock PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); if (acl_lseek(eh->fd.file, 0, SEEK_SET) < 0) acl_msg_fatal("%s: cannot seek PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); if ((count = acl_file_read(eh->fd.file, buffer, sizeof(buffer), 0, NULL)) < 0) acl_msg_fatal("%s: cannot read PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); if (count > 0) RAND_seed(buffer, count); RAND_bytes(buffer, sizeof(buffer)); if (acl_lseek(eh->fd.file, 0, SEEK_SET) < 0) acl_msg_fatal("%s: cannot seek PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); if (acl_file_write(eh->fd.file, buffer, sizeof(buffer), 0, NULL) != sizeof(buffer)) acl_msg_fatal("%s: cannot write PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); if (acl_myflock(eh->fd.file, ACL_INTERNAL_LOCK, ACL_MYFLOCK_OP_NONE) != 0) acl_msg_fatal("%s: cannot unlock PRNG exchange file %s: %s", myname, eh->name, acl_last_serror()); }
static void test_file(ACL_FILE_HANDLE fd, ssize_t max) { ssize_t i; char buf[1]; buf[0] = 'X'; for (i = 0; i < max; i++) { if (acl_file_write(fd, buf, 1, 0, NULL, NULL) != 1) { printf("write error: %s\r\n", acl_last_serror()); return; } } }
static int vstring_extend(ACL_VBUF *bp, ssize_t incr) { const char *myname = "vstring_extend"; ssize_t used = (ssize_t) (bp->ptr - bp->data), new_len; ACL_VSTRING *vp = (ACL_VSTRING *) bp; if (vp->maxlen > 0 && (ssize_t) ACL_VSTRING_LEN(vp) >= vp->maxlen) { ACL_VSTRING_AT_OFFSET(vp, vp->maxlen - 1); ACL_VSTRING_TERMINATE(vp); acl_msg_warn("%s(%d), %s: overflow maxlen: %ld, %ld", __FILE__, __LINE__, myname, (long) vp->maxlen, (long) ACL_VSTRING_LEN(vp)); bp->flags |= ACL_VBUF_FLAG_EOF; return ACL_VBUF_EOF; } #ifdef ACL_WINDOWS if (bp->fd == ACL_FILE_INVALID && (bp->flags & ACL_VBUF_FLAG_FIXED)) #else if (bp->fd < 0 && (bp->flags & ACL_VBUF_FLAG_FIXED)) #endif { acl_msg_warn("%s(%d), %s: can't extend fixed buffer", __FILE__, __LINE__, myname); return ACL_VBUF_EOF; } /* * Note: vp->vbuf.len is the current buffer size (both on entry and on * exit of this routine). We round up the increment size to the buffer * size to avoid silly little buffer increments. With really large * strings we might want to abandon the length doubling strategy, and * go to fixed increments. */ #ifdef INCR_NO_DOUBLE /* below come from redis-server/sds.c/sdsMakeRoomFor, which can * avoid memory double growing too large --- 2015.2.2, zsx */ new_len = bp->len + incr; if (new_len < MAX_PREALLOC) new_len *= 2; else new_len += MAX_PREALLOC; #else new_len = bp->len + (bp->len > incr ? bp->len : incr); #endif if (vp->maxlen > 0 && new_len > vp->maxlen) new_len = vp->maxlen; if (vp->slice) bp->data = (unsigned char *) acl_slice_pool_realloc( __FILE__, __LINE__, vp->slice, bp->data, new_len); else if (vp->dbuf) { const unsigned char *data = bp->data; bp->data = (unsigned char *) acl_dbuf_pool_alloc( vp->dbuf, new_len); memcpy(bp->data, data, used); acl_dbuf_pool_free(vp->dbuf, data); } else if (bp->fd != ACL_FILE_INVALID) { #ifdef ACL_UNIX acl_off_t off = new_len - 1; if (acl_lseek(bp->fd, off, SEEK_SET) != (acl_off_t) off) acl_msg_fatal("lseek failed: %s, off: %lld", acl_last_serror(), off); if (acl_file_write(bp->fd, "\0", 1, 0, NULL, NULL) == ACL_VSTREAM_EOF) { acl_msg_fatal("write error: %s", acl_last_serror()); } #endif } else bp->data = (unsigned char *) acl_myrealloc(bp->data, new_len); bp->len = new_len; bp->ptr = bp->data + used; bp->cnt = bp->len - used; return 0; }