summaryrefslogtreecommitdiffstats
path: root/file.c
diff options
context:
space:
mode:
authornicm <nicm>2019-12-16 15:48:50 +0000
committernicm <nicm>2019-12-16 15:48:50 +0000
commiteaa58d28dc7da9b2ef0d77f4c8e85aab55b71935 (patch)
treec6e7cf718c7a5d8f7348c2e178a7114c5a41c030 /file.c
parent21f9b39f060006fe769034ac2bb9b71d0a910f80 (diff)
Instead of using large buffers in imsgs, add the data or path onto the end.
Diffstat (limited to 'file.c')
-rw-r--r--file.c61
1 files changed, 37 insertions, 24 deletions
diff --git a/file.c b/file.c
index f0d622bc..7e1f1879 100644
--- a/file.c
+++ b/file.c
@@ -17,9 +17,11 @@
*/
#include <sys/types.h>
+#include <sys/queue.h>
#include <errno.h>
#include <fcntl.h>
+#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -147,7 +149,6 @@ file_vprint(struct client *c, const char *fmt, va_list ap)
msg.stream = 1;
msg.fd = STDOUT_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add_vprintf(cf->buffer, fmt, ap);
@@ -174,7 +175,6 @@ file_print_buffer(struct client *c, void *data, size_t size)
msg.stream = 1;
msg.fd = STDOUT_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add(cf->buffer, data, size);
@@ -204,7 +204,6 @@ file_error(struct client *c, const char *fmt, ...)
msg.stream = 2;
msg.fd = STDERR_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add_vprintf(cf->buffer, fmt, ap);
@@ -220,7 +219,8 @@ file_write(struct client *c, const char *path, int flags, const void *bdata,
{
struct client_file *cf;
FILE *f;
- struct msg_write_open msg;
+ struct msg_write_open *msg;
+ size_t msglen;
int fd = -1;
const char *mode;
@@ -261,17 +261,22 @@ file_write(struct client *c, const char *path, int flags, const void *bdata,
skip:
evbuffer_add(cf->buffer, bdata, bsize);
- msg.stream = cf->stream;
- msg.fd = fd;
- msg.flags = flags;
- if (strlcpy(msg.path, cf->path, sizeof msg.path) >= sizeof msg.path) {
+ msglen = strlen(cf->path) + 1 + sizeof *msg;
+ if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
cf->error = E2BIG;
goto done;
}
- if (proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg) != 0) {
+ msg = xmalloc(msglen);
+ msg->stream = cf->stream;
+ msg->fd = fd;
+ msg->flags = flags;
+ memcpy(msg + 1, cf->path, msglen - sizeof *msg);
+ if (proc_send(c->peer, MSG_WRITE_OPEN, -1, msg, msglen) != 0) {
+ free(msg);
cf->error = EINVAL;
goto done;
}
+ free(msg);
return;
done:
@@ -283,10 +288,10 @@ file_read(struct client *c, const char *path, client_file_cb cb, void *cbdata)
{
struct client_file *cf;
FILE *f;
- struct msg_read_open msg;
+ struct msg_read_open *msg;
+ size_t msglen, size;
int fd = -1;
char buffer[BUFSIZ];
- size_t size;
if (strcmp(path, "-") == 0) {
cf = file_create(c, file_next_stream++, cb, cbdata);
@@ -327,16 +332,21 @@ file_read(struct client *c, const char *path, client_file_cb cb, void *cbdata)
}
skip:
- msg.stream = cf->stream;
- msg.fd = fd;
- if (strlcpy(msg.path, cf->path, sizeof msg.path) >= sizeof msg.path) {
+ msglen = strlen(cf->path) + 1 + sizeof *msg;
+ if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
cf->error = E2BIG;
goto done;
}
- if (proc_send(c->peer, MSG_READ_OPEN, -1, &msg, sizeof msg) != 0) {
+ msg = xmalloc(msglen);
+ msg->stream = cf->stream;
+ msg->fd = fd;
+ memcpy(msg + 1, cf->path, msglen - sizeof *msg);
+ if (proc_send(c->peer, MSG_READ_OPEN, -1, msg, msglen) != 0) {
+ free(msg);
cf->error = EINVAL;
goto done;
}
+ free(msg);
return;
done:
@@ -358,20 +368,22 @@ void
file_push(struct client_file *cf)
{
struct client *c = cf->c;
- struct msg_write_data msg;
+ struct msg_write_data *msg;
+ size_t msglen, sent, left;
struct msg_write_close close;
- size_t sent, left;
+ msg = xmalloc(sizeof *msg);
left = EVBUFFER_LENGTH(cf->buffer);
while (left != 0) {
sent = left;
- if (sent > sizeof msg.data)
- sent = sizeof msg.data;
- memcpy(msg.data, EVBUFFER_DATA(cf->buffer), sent);
- msg.size = sent;
-
- msg.stream = cf->stream;
- if (proc_send(c->peer, MSG_WRITE, -1, &msg, sizeof msg) != 0)
+ if (sent > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
+ sent = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
+
+ msglen = (sizeof *msg) + sent;
+ msg = xrealloc(msg, msglen);
+ msg->stream = cf->stream;
+ memcpy(msg + 1, EVBUFFER_DATA(cf->buffer), sent);
+ if (proc_send(c->peer, MSG_WRITE, -1, msg, msglen) != 0)
break;
evbuffer_drain(cf->buffer, sent);
@@ -387,4 +399,5 @@ file_push(struct client_file *cf)
proc_send(c->peer, MSG_WRITE_CLOSE, -1, &close, sizeof close);
file_fire_done(cf);
}
+ free(msg);
}