script.c revision 7cb128dc4cae2a03a742f63ba7afee23c78e3af0
7cb128dc4cae2a03a742f63ba7afee23c78e3af0Phil Carmody/* Copyright (c) 2010-2015 Dovecot authors, see the included COPYING file */
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainenstatic void script_verify_version(const char *line)
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen !version_string_verify(line, "script", SCRIPT_MAJOR_VERSION)) {
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen i_fatal("Client not compatible with this binary "
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen "(connecting to wrong socket?)");
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainenexec_child(struct master_service_connection *conn, const char *const *args)
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen unsigned int i, socket_count;
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* close all fds */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen socket_count = master_service_get_socket_count(master_service);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen for (i = 0; i < socket_count; i++) {
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainenstatic bool client_exec_script(struct master_service_connection *conn)
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen const char *const *args;
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen input = buffer_create_dynamic(pool_datastack_create(), IO_BLOCK_SIZE);
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen /* Input contains:
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen VERSION .. <lf>
8656ae1d89e3576902376658cd8657d30748414fTimo Sirainen [timeout=<timeout>]
4f68fa8a117642e2c134a29d8e35569bce6c3158Timo Sirainen <noreply> | "-" <lf>
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen buf = buffer_append_space_unsafe(input, IO_BLOCK_SIZE);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* peek in socket input buffer */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen ret = recv(conn->fd, buf, IO_BLOCK_SIZE, MSG_PEEK);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen script_verify_version(t_strcut(str_c(input), '\n'));
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_fatal("recv(MSG_PEEK) failed: disconnected");
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* scan for final \n\n */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen end = CONST_PTR_OFFSET(input->data, prev_size + ret);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen scanpos = pos - (const unsigned char *)input->data;
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* read data for real (up to and including \n\n) */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen ret = recv(conn->fd, buf, scanpos-prev_size, 0);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_fatal("recv() failed: size of definitive recv() differs from peek");
b5b44e6c99e62ba5f7600ac72c26327a1f8dfb8dTimo Sirainen /* drop the last two LFs */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* no need to fork and check exit status */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen /* check script exit status */
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_error("Script terminated abnormally, exit status %d", (int)ret);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_error("Script terminated abnormally, signal %d", WTERMSIG(status));
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_fatal("Script stopped, signal %d", WSTOPSIG(status));
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen i_fatal("Script terminated abnormally, return status %d", status);
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainenstatic void client_connected(struct master_service_connection *conn)
2d3731908f924bc8a2b7887a712cc033e618c998Timo Sirainen response[0] = client_exec_script(conn) ? '+' : '-';
db776508d6e959ae6e66050e22e2763521e6d272Timo Sirainen master_service = master_service_init("script", 0, &argc, &argv, "+");
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen master_service_init_log(master_service, "script: ");
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen master_service_set_service_count(master_service, 1);
cbfdc79faac95adc8a3f76ee2216d3fea0c19826Timo Sirainen binary = t_strconcat(PKG_LIBEXECDIR"/", argv[0], NULL);