Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9b73e8a417c8
Choose a base ref
...
head repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4f503293b3a3
Choose a head ref
  • 13 commits
  • 81 files changed
  • 1 contributor

Commits on Aug 5, 2014

  1. tools/update-ccan.sh: Fixes, and make it a bit friendlier.

    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    a04c810 View commit details
  2. ccan: update (esp. ccan/io fixes).

    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    c0fd932 View commit details
  3. pending: remove unused prev_txhashes field.

    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    61e3e98 View commit details
  4. blockfile: load with an io_loop.

    More natural, requires less intrusion into io_packet_read.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    7d262b5 View commit details
  5. log_io: preserve errno.

    This is generally a good thing in logging functions.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    4127884 View commit details
  6. peer: make alloc_peer set up peer->log and peer->you.

    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    1e3b600 View commit details
  7. packet_io: use fd -> log map.

    Doesn't require us seeing inside struct io_plan, which is going away.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    be09000 View commit details
  8. json_rpc: process multiple inputs where available.

    0f45401 said we would overwrite output,
    but we don't.  So partial revert.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    35d3ce9 View commit details
  9. dns: hand address we're connecting to through to init.

    This means we record the address we called connect() on, not where we
    ended up (with localhost IPv6 and IPv4, may be different).
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    31a1615 View commit details
  10. peer: simplify setup code.

    Don't create a peer until we're actually connected; use a peer_connecting
    structure until then.  This puts peer creation and initialization into
    a single place.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    7bef352 View commit details
  11. ccan: update to latest ccan (ccan/io rewrite).

    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    6c1a23c View commit details
  12. Update to match new ccan/io version.

    Particular changes:
    1) io_* now take the conn as first arg.
    2) io_plan * is returned intead of io_plan.
    3) io_new_conn needs a callback, rather than an inline call.
    4) io_break() is a separate function, you still need a plan.
    5) io_duplex() just creates two plans, not two conns.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    81294e0 View commit details
  13. protocol_net: actually pad protocol_pkt_welcome to 32 bits.

    There was 2 bytes of undocumented padding.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Aug 5, 2014
    Copy the full SHA
    4f50329 View commit details
Showing with 1,866 additions and 2,121 deletions.
  1. +58 −48 blockfile.c
  2. +1 −1 ccan/README
  3. +21 −26 ccan/ccan/io/_info
  4. +50 −58 ccan/ccan/io/backend.h
  5. +343 −350 ccan/ccan/io/io.c
  6. +363 −262 ccan/ccan/io/io.h
  7. +48 −104 ccan/ccan/io/io_plan.h
  8. +133 −276 ccan/ccan/io/poll.c
  9. +0 −8 ccan/ccan/io/test/run-01-start-finish-DEBUG.c
  10. +2 −0 ccan/ccan/io/test/run-01-start-finish-debug.c
  11. +14 −7 ccan/ccan/io/test/run-01-start-finish.c
  12. +0 −8 ccan/ccan/io/test/run-02-read-DEBUG.c
  13. +2 −0 ccan/ccan/io/test/run-02-read-debug.c
  14. +12 −8 ccan/ccan/io/test/run-02-read.c
  15. +0 −8 ccan/ccan/io/test/run-03-readpartial-DEBUG.c
  16. +2 −0 ccan/ccan/io/test/run-03-readpartial-debug.c
  17. +15 −10 ccan/ccan/io/test/run-03-readpartial.c
  18. +0 −8 ccan/ccan/io/test/run-04-writepartial-DEBUG.c
  19. +2 −0 ccan/ccan/io/test/run-04-writepartial-debug.c
  20. +14 −8 ccan/ccan/io/test/run-04-writepartial.c
  21. +0 −8 ccan/ccan/io/test/run-05-write-DEBUG.c
  22. +2 −0 ccan/ccan/io/test/run-05-write-debug.c
  23. +12 −8 ccan/ccan/io/test/run-05-write.c
  24. +0 −8 ccan/ccan/io/test/run-06-idle-DEBUG.c
  25. +23 −14 ccan/ccan/io/test/run-06-idle.c
  26. +0 −8 ccan/ccan/io/test/run-07-break-DEBUG.c
  27. +2 −0 ccan/ccan/io/test/run-07-break-debug.c
  28. +16 −11 ccan/ccan/io/test/run-07-break.c
  29. +0 −7 ccan/ccan/io/test/run-08-hangup-on-idle-DEBUG.c
  30. +27 −17 ccan/ccan/io/test/run-08-hangup-on-idle.c
  31. +0 −7 ccan/ccan/io/test/run-08-read-after-hangup-DEBUG.c
  32. +17 −7 ccan/ccan/io/test/run-08-read-after-hangup.c
  33. +0 −8 ccan/ccan/io/test/run-09-connect-DEBUG.c
  34. +2 −0 ccan/ccan/io/test/run-09-connect-debug.c
  35. +26 −12 ccan/ccan/io/test/run-09-connect.c
  36. +0 −12 ccan/ccan/io/test/run-10-many-DEBUG.c
  37. +23 −20 ccan/ccan/io/test/run-10-many.c
  38. +0 −8 ccan/ccan/io/test/run-12-bidir-DEBUG.c
  39. +2 −0 ccan/ccan/io/test/run-12-bidir-debug.c
  40. +25 −13 ccan/ccan/io/test/run-12-bidir.c
  41. +0 −8 ccan/ccan/io/test/run-13-all-idle-DEBUG.c
  42. +7 −2 ccan/ccan/io/test/run-13-all-idle.c
  43. +0 −8 ccan/ccan/io/test/run-14-duplex-both-read-DEBUG.c
  44. +2 −0 ccan/ccan/io/test/run-14-duplex-both-read-debug.c
  45. +23 −17 ccan/ccan/io/test/run-14-duplex-both-read.c
  46. +0 −8 ccan/ccan/io/test/run-15-timeout-DEBUG.c
  47. +45 −30 ccan/ccan/io/test/run-15-timeout.c
  48. +2 −0 ccan/ccan/io/test/run-16-duplex-test-debug.c
  49. +17 −22 ccan/ccan/io/test/run-16-duplex-test.c
  50. +0 −8 ccan/ccan/io/test/run-17-homemade-io-DEBUG.c
  51. +2 −0 ccan/ccan/io/test/run-17-homemade-io-debug.c
  52. +32 −30 ccan/ccan/io/test/run-17-homemade-io.c
  53. +0 −8 ccan/ccan/io/test/run-18-errno-DEBUG.c
  54. +2 −0 ccan/ccan/io/test/run-18-errno-debug.c
  55. +16 −10 ccan/ccan/io/test/run-18-errno.c
  56. +0 −8 ccan/ccan/io/test/run-19-always-DEBUG.c
  57. +2 −0 ccan/ccan/io/test/run-19-always-debug.c
  58. +15 −9 ccan/ccan/io/test/run-19-always.c
  59. +0 −240 ccan/ccan/io/test/run-set_alloc.c
  60. +9 −2 ccan/ccan/opt/helpers.c
  61. +28 −1 ccan/ccan/opt/test/run-helpers.c
  62. +13 −0 ccan/tools/configurator/configurator.c
  63. +31 −18 dns.c
  64. +4 −2 dns.h
  65. +38 −28 generating.c
  66. +29 −46 jsonrpc.c
  67. +0 −3 jsonrpc.h
  68. +3 −0 log.c
  69. +60 −62 packet_io.c
  70. +18 −18 packet_io.h
  71. +135 −131 peer.c
  72. +10 −2 peer.h
  73. +0 −1 pending.h
  74. +18 −14 pettycoin.c
  75. +1 −1 protocol_net.h
  76. +2 −1 state.c
  77. +6 −2 state.h
  78. +26 −18 test/run-01-jsonrpc.c
  79. +1 −1 test/standalone/run_local.sh
  80. +11 −3 tools/update-ccan.sh
  81. +1 −1 welcome.c
106 changes: 58 additions & 48 deletions blockfile.c
Original file line number Diff line number Diff line change
@@ -60,11 +60,50 @@ static bool load_tx_in_block(struct state *state,
return e == PROTOCOL_ECODE_NONE;
}

struct load_state {
struct state *state;
off_t processed;
struct protocol_net_hdr *pkt;
};

static struct io_plan *load_packet(struct io_conn *conn, struct load_state *ls)
{
switch (le32_to_cpu(ls->pkt->type)) {
case PROTOCOL_PKT_BLOCK:
if (!load_block(ls->state, ls->pkt)) {
log_unusual(ls->state->log,
"blockfile partial block");
return io_close(conn);
}
break;
case PROTOCOL_PKT_TX_IN_BLOCK:
if (!load_tx_in_block(ls->state, (const void *)ls->pkt)) {
log_unusual(ls->state->log,
"blockfile partial transaction");
return io_close(conn);
}
break;
default:
log_unusual(ls->state->log, "blockfile unknown type %u",
le32_to_cpu(ls->pkt->type));
return io_close(conn);
}

ls->processed = lseek(io_conn_fd(conn), 0, SEEK_CUR);
return io_read_packet(conn, &ls->pkt, load_packet, ls);
}

static struct io_plan *setup_load_conn(struct io_conn *conn,
struct load_state *ls)
{
return io_read_packet(conn, &ls->pkt, load_packet, ls);
}

void load_blocks(struct state *state)
{
off_t off = 0;
struct stat st;
int fd;
struct load_state ls;
off_t len;

fd = open("blockfile", O_RDWR|O_CREAT, 0600);
if (fd < 0)
@@ -73,55 +112,26 @@ void load_blocks(struct state *state)
/* Prevent us saving blocks as we're reading them. */
state->blockfd = -1;

for (;;) {
struct protocol_net_hdr *pkt;
struct io_plan plan;
int ret;

plan = io_read_packet_(&pkt, (void *)1, NULL);
while ((ret = plan.io(fd, &plan)) != 1) {
if (ret == -1) {
/* Did we do a partial read? */
if (lseek(fd, 0, SEEK_CUR) != off) {
log_unusual(state->log,
"blockfile partial read");
goto truncate;
}
goto out;
}
}
ls.state = state;
ls.processed = 0;
io_new_conn(state, fd, setup_load_conn, &ls);

switch (le32_to_cpu(pkt->type)) {
case PROTOCOL_PKT_BLOCK:
if (!load_block(state, pkt)) {
log_unusual(state->log,
"blockfile partial block");
goto truncate;
}
break;
case PROTOCOL_PKT_TX_IN_BLOCK:
if (!load_tx_in_block(state, (const void *)pkt)) {
log_unusual(state->log,
"blockfile partial transaction");
goto truncate;
}
break;
default:
log_unusual(state->log, "blockfile unknown type %u",
le32_to_cpu(pkt->type));
goto truncate;
}
off += le32_to_cpu(pkt->len);
}
/* When it reads 0 bytes, it will close, so dup fd. */
fd = dup(fd);

truncate:
fstat(fd, &st);
log_unusual(state->log, "Truncating blockfile from %llu to %llu",
(long long)st.st_size, (long long)off);
ftruncate(fd, off);
lseek(fd, SEEK_SET, off);
/* Process them all. */
io_loop(NULL, NULL);

len = lseek(fd, 0, SEEK_END);
/* If we didn't process the entire file, truncate it. */
if (len != ls.processed) {
log_unusual(state->log,
"Truncating blockfile from %llu to %llu",
(long long)len, (long long)ls.processed);
ftruncate(fd, ls.processed);
lseek(fd, SEEK_SET, ls.processed);
}

out:
/* Now we can save more. */
state->blockfd = fd;

2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-1886-gc2b2d22
CCAN version: init-1898-g0fbc790
47 changes: 21 additions & 26 deletions ccan/ccan/io/_info
Original file line number Diff line number Diff line change
@@ -10,12 +10,6 @@
* (eg. read, write). It is also possible to write custom I/O
* plans.
*
* When compiled with DEBUG, control flow is changed so that rather
* than returning to the main io_loop(), plans are executed sequentially
* providing a backtrace showing what has occurred on that connection.
* Which connection(s) do this depends on the user-specified io_debug
* function.
*
* Example:
* // Given tr A-Z a-z outputs tr a-z a-z
* #include <ccan/io/io.h>
@@ -41,7 +35,7 @@
* io_wake(b);
* }
*
* static struct io_plan read_in(struct io_conn *c, struct buffer *b)
* static struct io_plan *read_in(struct io_conn *c, struct buffer *b)
* {
* // Add what we just read.
* b->end += b->rlen;
@@ -55,35 +49,33 @@
* if (b->start == b->end)
* b->start = b->end = 0;
*
* // Read in some of the rest.
* b->rlen = sizeof(b->buf) - b->end;
*
* // No room? Wait for writer
* if (b->rlen == 0)
* return io_wait(b, read_in, b);
* if (b->end == sizeof(b->buf))
* return io_wait(c, b, read_in, b);
*
* return io_read_partial(b->buf + b->end, &b->rlen, read_in, b);
* return io_read_partial(c, b->buf + b->end, sizeof(b->buf) - b->end,
* &b->rlen, read_in, b);
* }
*
* static struct io_plan write_out(struct io_conn *c, struct buffer *b)
* static struct io_plan *write_out(struct io_conn *c, struct buffer *b)
* {
* // Remove what we just wrote.
* b->start += b->wlen;
* assert(b->start <= sizeof(b->buf));
*
* // If we wrote somthing, wake writer.
* // If we wrote something, wake writer.
* if (b->wlen != 0)
* io_wake(b);
*
* b->wlen = b->end - b->start;
* // Nothing to write? Wait for reader.
* if (b->wlen == 0) {
* if (b->end == b->start) {
* if (b->finished)
* return io_close();
* return io_wait(b, write_out, b);
* return io_close(c);
* return io_wait(c, b, write_out, b);
* }
*
* return io_write_partial(b->buf + b->start, &b->wlen, write_out, b);
* return io_write_partial(c, b->buf + b->start, b->end - b->start,
* &b->wlen, write_out, b);
* }
*
* // Feed a program our stdin, gather its stdout, print that at end.
@@ -117,16 +109,16 @@
*
* // Read from stdin, write to child.
* memset(&to, 0, sizeof(to));
* reader = io_new_conn(STDIN_FILENO, read_in(NULL, &to));
* reader = io_new_conn(NULL, STDIN_FILENO, read_in, &to);
* io_set_finish(reader, finish, &to);
* io_new_conn(tochild[1], write_out(NULL, &to));
* io_new_conn(NULL, tochild[1], write_out, &to);
*
* // Read from child, write to stdout.
* reader = io_new_conn(fromchild[0], read_in(NULL, &from));
* reader = io_new_conn(NULL, fromchild[0], read_in, &from);
* io_set_finish(reader, finish, &from);
* io_new_conn(STDOUT_FILENO, write_out(NULL, &from));
* io_new_conn(NULL, STDOUT_FILENO, write_out, &from);
*
* io_loop();
* io_loop(NULL, NULL);
* wait(&status);
*
* return WIFEXITED(status) ? WEXITSTATUS(status) : 2;
@@ -141,9 +133,12 @@ int main(int argc, char *argv[])
return 1;

if (strcmp(argv[1], "depends") == 0) {
printf("ccan/typesafe_cb\n");
printf("ccan/container_of\n");
printf("ccan/list\n");
printf("ccan/tal\n");
printf("ccan/time\n");
printf("ccan/timer\n");
printf("ccan/typesafe_cb\n");
return 0;
}

108 changes: 50 additions & 58 deletions ccan/ccan/io/backend.h
Original file line number Diff line number Diff line change
@@ -2,18 +2,8 @@
#ifndef CCAN_IO_BACKEND_H
#define CCAN_IO_BACKEND_H
#include <stdbool.h>
#include <ccan/timer/timer.h>
#include <poll.h>

/* A setting for actions to always run (eg. zero-length reads). */
#define POLLALWAYS (((POLLIN|POLLOUT) + 1) & ~((POLLIN|POLLOUT)))

struct io_alloc {
void *(*alloc)(size_t size);
void *(*realloc)(void *ptr, size_t size);
void (*free)(void *ptr);
};
extern struct io_alloc io_alloc;
#include "io_plan.h"

struct fd {
int fd;
@@ -25,77 +15,79 @@ struct fd {
struct io_listener {
struct fd fd;

const tal_t *ctx;

/* These are for connections we create. */
void (*init)(int fd, void *arg);
struct io_plan *(*init)(struct io_conn *conn, void *arg);
void *arg;
};

struct io_timeout {
struct timer timer;
struct io_conn *conn;
enum io_plan_status {
/* As before calling next function. */
IO_UNSET,
/* Normal. */
IO_POLLING,
/* Waiting for io_wake */
IO_WAITING,
/* Always do this. */
IO_ALWAYS,
/* Closing (both plans will be the same). */
IO_CLOSING
};

struct io_plan (*next)(struct io_conn *, void *arg);
/**
* struct io_plan - one half of I/O to do
* @status: the status of this plan.
* @io: function to call when fd becomes read/writable, returns 0 to be
* called again, 1 if it's finished, and -1 on error (fd will be closed)
* @next: the next function which is called if io returns 1.
* @next_arg: the argument to @next
* @u1, @u2: scratch space for @io.
*/
struct io_plan {
enum io_plan_status status;

int (*io)(int fd, struct io_plan_arg *arg);

struct io_plan *(*next)(struct io_conn *, void *next_arg);
void *next_arg;

struct io_plan_arg arg;
};

/* One connection per client. */
struct io_conn {
struct fd fd;
bool debug;
/* For duplex to save. */
bool debug_saved;

/* always or closing list. */
struct io_conn *list;

void (*finish)(struct io_conn *, void *arg);
void *finish_arg;

struct io_conn *duplex;
struct io_timeout *timeout;

struct io_plan plan;
struct io_plan plan[2];
};

static inline bool timeout_active(const struct io_conn *conn)
{
return conn->timeout && conn->timeout->conn;
}

extern void *io_loop_return;

#ifdef DEBUG
extern struct io_conn *current;
static inline void set_current(struct io_conn *conn)
{
current = conn;
}
static inline bool doing_debug_on(struct io_conn *conn)
{
return io_debug_conn && io_debug_conn(conn);
}
static inline bool doing_debug(void)
{
return io_debug_conn;
}
#else
static inline void set_current(struct io_conn *conn)
{
}
static inline bool doing_debug_on(struct io_conn *conn)
{
return false;
}
static inline bool doing_debug(void)
{
return false;
}
#endif

bool add_listener(struct io_listener *l);
bool add_conn(struct io_conn *c);
bool add_duplex(struct io_conn *c);
void del_listener(struct io_listener *l);
void backend_plan_changed(struct io_conn *conn);
void backend_wait_changed(const void *wait);
void backend_add_timeout(struct io_conn *conn, struct timerel duration);
void backend_del_timeout(struct io_conn *conn);
void backend_new_closing(struct io_conn *conn);
void backend_new_always(struct io_conn *conn);
void backend_new_plan(struct io_conn *conn);
void remove_from_always(struct io_conn *conn);
void backend_plan_done(struct io_conn *conn);

void backend_wake(const void *wait);
void backend_del_conn(struct io_conn *conn);

void io_ready(struct io_conn *conn);
void io_ready(struct io_conn *conn, int pollflags);
void io_do_always(struct io_conn *conn);
void io_do_wakeup(struct io_conn *conn, struct io_plan *plan);
void *do_io_loop(struct io_conn **ready);
#endif /* CCAN_IO_BACKEND_H */
Loading