9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek#define IPC_SERVER_HANDSHAKE "VERSION\tipc-proxy\t1\t0\n"
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekstatic void ipc_connection_cmd_free(struct ipc_connection_cmd **_cmd,
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek cmds = array_get_modifiable(&cmd->conn->cmds, &count);
0429e21a45aa26c133cb4d8285a60446a0611e44Pavel Březina for (i = 0; i < count; i++) {
5a4239490c7fb7d732180a9d40f27f0247c56631Jakub Hrozek cmd->callback(IPC_CMD_STATUS_ERROR, reason, cmd->context);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekipc_connection_cmd_find(struct ipc_connection *conn, unsigned int tag)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekipc_connection_input_line(struct ipc_connection *conn, char *line)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek /* <tag> [:+-]<data> */
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek switch (data[0]) {
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek i_error("IPC server: Input for unexpected command tag %u", tag);
4cae8609b513c267af11c0409bfe1d17d3a5da2fMichal Zidekstatic void ipc_connection_input(struct ipc_connection *conn)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek if ((line = i_stream_next_line(conn->input)) == NULL)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek "IPC server not compatible with this server "
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek "(mixed old and new binaries?)");
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek if ((line = i_stream_next_line(conn->input)) == NULL)
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl "IPC server sent invalid handshake");
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl if (ipc_group_update_name(conn->group, args[1]) < 0) {
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl ipc_connection_destroy(&conn, TRUE, t_strdup_printf(
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl "IPC server named itself unexpectedly: %s "
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl "(existing ones were %s)",
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek ipc_connection_destroy(&conn, TRUE, t_strdup_printf(
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl while ((line = i_stream_next_line(conn->input)) != NULL) {
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek ipc_connection_destroy(&conn, TRUE, t_strdup_printf(
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek "Invalid input from IPC server '%s': %s",
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekstruct ipc_connection *ipc_connection_create(int listen_fd, int fd)
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek conn->group = ipc_group_lookup_listen_fd(listen_fd);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek conn->io = io_add(fd, IO_READ, ipc_connection_input, conn);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek conn->input = i_stream_create_fd(fd, (size_t)-1);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek conn->output = o_stream_create_fd(fd, (size_t)-1);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek o_stream_set_no_error_handling(conn->output, TRUE);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek o_stream_nsend_str(conn->output, IPC_SERVER_HANDSHAKE);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek DLLIST_PREPEND(&conn->group->connections, conn);
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekvoid ipc_connection_destroy(struct ipc_connection **_conn,
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek const char *group_name = conn->group->name != NULL ?
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek t_strdup_printf("#%d", conn->group->listen_fd);
1112e84494bcfd0f658e073d25f15ed877d047aaPavel Reichl error = t_strdup_printf("IPC: '%s' PID %d server connection: %s",
eeac17ebbe38f16deaa8599231cccfc97aaac85cPavel Reichl DLLIST_REMOVE(&conn->group->connections, conn);
eeac17ebbe38f16deaa8599231cccfc97aaac85cPavel Reichl master_service_client_connection_destroyed(master_service);
eeac17ebbe38f16deaa8599231cccfc97aaac85cPavel Reichlipc_connection_lookup_id(struct ipc_group *group, unsigned int id)
eeac17ebbe38f16deaa8599231cccfc97aaac85cPavel Reichl for (conn = group->connections; conn != NULL; conn = conn->next) {
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozekvoid ipc_connection_cmd(struct ipc_connection *conn, const char *cmd,
9cb46bc62f22e0104f1b41a423b014c281ef5fc2Jakub Hrozek t_strdup_printf("%u\t%s\n", ipc_cmd->tag, cmd));