bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen#define IPC_SERVER_HANDSHAKE "VERSION\tipc-server\t1\t0\nHANDSHAKE\t%s\t%s\n"
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_server_disconnect(struct ipc_server *server);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_server_input_line(struct ipc_server *server, char *line)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen /* tag cmd */
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen i_error("IPC proxy sent invalid input: %s", line);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_server_input(struct ipc_server *server)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen if ((line = i_stream_next_line(server->input)) == NULL)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen i_error("IPC proxy not compatible with this server "
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen "(mixed old and new binaries?)");
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen while ((line = i_stream_next_line(server->input)) != NULL)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_server_connect(struct ipc_server *server)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen i_error("connect(%s) failed: %m", server->path);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen server->to = timeout_add(IPC_SERVER_RECONNECT_MSECS,
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen server->io = io_add(server->fd, IO_READ, ipc_server_input, server);
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi server->input = i_stream_create_fd(server->fd, (size_t)-1);
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi server->output = o_stream_create_fd(server->fd, (size_t)-1);
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen o_stream_set_no_error_handling(server->output, TRUE);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen t_strdup_printf(IPC_SERVER_HANDSHAKE, server->name, my_pid));
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_server_disconnect(struct ipc_server *server)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen i_error("close(%s) failed: %m", server->path);
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenipc_server_init(const char *ipc_socket_path, const char *name,
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenvoid ipc_server_deinit(struct ipc_server **_server)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenvoid ipc_cmd_send(struct ipc_cmd *cmd, const char *data)
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen t_strdup_printf("%u\t:%s\n", cmd->tag, data));
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenstatic void ipc_cmd_finish(struct ipc_cmd *cmd, const char *line)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenvoid ipc_cmd_success_reply(struct ipc_cmd **_cmd, const char *data)
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainen ipc_cmd_finish(cmd, t_strconcat("+", data, NULL));
83942ac160cdfb922c3a2f29ddfae2a13ebf8b5dTimo Sirainenvoid ipc_cmd_fail(struct ipc_cmd **_cmd, const char *errormsg)