Commit 1ef74258 1ef742582942bb2a7ef4ce8531cf3b306b3f82d7 by Sergey Poznyakoff

Remove the MU_STREAM_FD_AUTOCLOSE stream flag.

This is the default behavior now.  The reverse can be requested by
a special MU_IOCTL_FD,MU_IOCTL_FD_SET_BORROW ioctl.

* include/mailutils/stream.h (MU_STREAM_FD_AUTOCLOSE): Remove. This
is the default behavior now.
(MU_IOCTL_FD): New ioctl.
(MU_IOCTL_FD_GET_BORROW, MU_IOCTL_FD_SET_BORROW): New opcodes.
* include/mailutils/sys/file_stream.h (_MU_FILE_STREAM_FD_BORROWED): New flag.

* libmailutils/stream/file_stream.c (fd_close): Close the fd unless
_MU_FILE_STREAM_FD_BORROWED bit is set.
(fd_open): Clear the _MU_FILE_STREAM_FD_BORROWED bit.
(fd_ioctl): Handle MU_IOCTL_FD.
(mu_file_stream_create): Remove MU_STREAM_FD_AUTOCLOSE.

* ibmailutils/base/version.c (mu_print_options): Set borrow mode on the
temporary stdout stream.
* libmailutils/cfg/driver.c (mu_cfg_tree_reduce): Set borrow mode on the
temporary stderr stream.
* libmailutils/stdstream/strerr.c (mu_stdstream_strerr_create): Set borrow
mode on the stderr stream.
* libmailutils/tests/tempfile.c: set borrow mode on the temporary stream,
because it may be reopened later (verify mode).

* examples/mta.c: Remove MU_STREAM_FD_AUTOCLOSE.
* imap4d/io.c: Likewise.
* libmailutils/stream/prog_stream.c: Likewise.
* libmailutils/stream/socket_stream.c: Likewise.
* libmailutils/stream/temp_file_stream.c: Likewise.
* maidag/lmtp.c: Likewise.
* pop3d/extra.c: Likewise.

These will disappear when the corresponding utilities are rewritten to
use MU stdstream:

* mail/escape.c (quote0): set borrow mode on the output stream.
* mimeview/mimeview.c (display_file): set borrow mode on the input stream.

Likewise, these will disappear when MH uses MU stdstream:

* mh/anno.c (main): Set borrow mode on the input stream.
* mh/mh_init.c (mh_real_install): Likewise.
* mh/mh_whatnow.c (_whatnow): Likewise.
* mh/mhl.c (open_output): Set borrow mode on the output stream.
* mh/mhn.c (mhn_show,store_handler): Likewise.
1 parent 841fc484
......@@ -824,8 +824,7 @@ mta_smtp (int argc, char **argv)
return 1;
}
rc = mu_fd_stream_create (&str, NULL, sfd,
MU_STREAM_RDWR|MU_STREAM_FD_AUTOCLOSE);
rc = mu_fd_stream_create (&str, NULL, sfd, MU_STREAM_RDWR);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
......
......@@ -29,13 +29,11 @@ io_setio (int ifd, int ofd)
if (ofd == -1)
imap4d_bye (ERR_NO_OFILE);
if (mu_stdio_stream_create (&istream, ifd,
MU_STREAM_READ | MU_STREAM_FD_AUTOCLOSE))
if (mu_stdio_stream_create (&istream, ifd, MU_STREAM_READ))
imap4d_bye (ERR_STREAM_CREATE);
mu_stream_set_buffer (istream, mu_buffer_line, 0);
if (mu_stdio_stream_create (&ostream, ofd,
MU_STREAM_WRITE | MU_STREAM_FD_AUTOCLOSE))
if (mu_stdio_stream_create (&ostream, ofd, MU_STREAM_WRITE))
imap4d_bye (ERR_STREAM_CREATE);
mu_stream_set_buffer (ostream, mu_buffer_line, 0);
......
......@@ -43,8 +43,7 @@ enum mu_buffer_type
#define MU_STREAM_CREAT 0x00000010
/* So far used only by TCP streams. */
#define MU_STREAM_NONBLOCK 0x00000020
/* For fd streams only */
#define MU_STREAM_FD_AUTOCLOSE 0x00000040
/* Not used 0x00000040 */
/* Not used. Intended for mailboxes only. */
#define MU_STREAM_NONLOCK 0x00000080
/* Not used as well 0x00000100 */
......@@ -71,6 +70,7 @@ enum mu_buffer_type
#define MU_IOCTL_NULLSTREAM 6 /* Null stream (see below) */
#define MU_IOCTL_LOGSTREAM 7 /* Log stream (see below) */
#define MU_IOCTL_XSCRIPTSTREAM 8 /* Transcript stream (see below) */
#define MU_IOCTL_FD 9 /* File descriptor manipulation */
/* Opcodes common for various families */
#define MU_IOCTL_OP_GET 0
......@@ -148,6 +148,17 @@ enum mu_buffer_type
*/
#define MU_IOCTL_XSCRIPTSTREAM_LEVEL 0
/* Opcodes for MU_IOCTL_FD */
/* Get "borrow state". Borrowed descriptors remain in open state
after the stream is closed.
Arg: int *
*/
#define MU_IOCTL_FD_GET_BORROW 0
/* Set borrow state.
Arg: int *
*/
#define MU_IOCTL_FD_SET_BORROW 1
#define MU_TRANSPORT_INPUT 0
#define MU_TRANSPORT_OUTPUT 1
#define MU_TRANSPORT_VALID_TYPE(n) \
......
......@@ -23,6 +23,7 @@
#define _MU_FILE_STREAM_TEMP 0x01
#define _MU_FILE_STREAM_ECHO_OFF 0x02
#define _MU_FILE_STREAM_FD_BORROWED 0x04
struct _mu_file_stream
{
......
......@@ -150,6 +150,7 @@ mu_print_options ()
mu_stream_ref (mu_strout);
else
{
int yes = 1;
int rc = mu_stdio_stream_create (&mu_strout, MU_STDOUT_FD, 0);
if (rc)
{
......@@ -157,6 +158,7 @@ mu_print_options ()
MU_STDOUT_FD, mu_strerror (rc));
abort ();
}
mu_stream_ioctl (mu_strout, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
}
mu_format_options (mu_strout, 1);
mu_stream_unref (mu_strout);
......
......@@ -586,8 +586,11 @@ mu_cfg_tree_reduce (mu_cfg_tree_t *parse_tree, const char *progname,
return 0;
if (flags & MU_PARSE_CONFIG_DUMP)
{
int yes = 1;
mu_stream_t stream;
mu_stdio_stream_create (&stream, MU_STDERR_FD, 0);
mu_stream_ioctl (stream, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
mu_cfg_format_parse_tree (stream, parse_tree, MU_CFG_FMT_LOCUS);
mu_stream_destroy (&stream);
}
......
......@@ -43,6 +43,7 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
{
case MU_STRERR_STDERR:
{
int yes = 1;
mu_stream_t str;
rc = mu_stdio_stream_create (&str, MU_STDERR_FD, 0);
......@@ -52,6 +53,10 @@ mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
tag, mu_strerror (rc));
return MU_ERR_FAILURE;
}
/* Make sure 2 is not closed when str is destroyed.
FIXME: Actually I don't know if it is needed, but just
in case... */
mu_stream_ioctl (str, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
if (!tag)
transport = str;
else
......
......@@ -65,7 +65,7 @@ fd_close (struct _mu_stream *str)
struct _mu_file_stream *fstr = (struct _mu_file_stream *) str;
if (fstr->fd != -1)
{
if ((str->flags & MU_STREAM_FD_AUTOCLOSE) && close (fstr->fd))
if (!(fstr->flags & _MU_FILE_STREAM_FD_BORROWED) && close (fstr->fd))
return errno;
fstr->fd = -1;
}
......@@ -119,7 +119,7 @@ fd_open (struct _mu_stream *str)
str->flags &= ~MU_STREAM_SEEK;
/* Make sure it will be closed */
str->flags |= MU_STREAM_FD_AUTOCLOSE;
fstr->flags &= ~_MU_FILE_STREAM_FD_BORROWED;
fstr->fd = fd;
return 0;
......@@ -269,6 +269,24 @@ fd_ioctl (struct _mu_stream *str, int code, int opcode, void *ptr)
}
break;
case MU_IOCTL_FD:
if (!ptr)
return EINVAL;
switch (opcode)
{
case MU_IOCTL_FD_GET_BORROW:
*(int*) ptr = !!(fstr->flags & _MU_FILE_STREAM_FD_BORROWED);
break;
case MU_IOCTL_FD_SET_BORROW:
if (*(int*)ptr)
fstr->flags |= _MU_FILE_STREAM_FD_BORROWED;
else
fstr->flags &= ~_MU_FILE_STREAM_FD_BORROWED;
break;
}
break;
default:
return ENOSYS;
}
......@@ -331,8 +349,7 @@ mu_file_stream_create (mu_stream_t *pstream, const char *filename, int flags)
int rc = _mu_file_stream_create (&fstr,
sizeof (struct _mu_file_stream),
filename, -1,
flags | MU_STREAM_SEEK |
MU_STREAM_FD_AUTOCLOSE);
flags | MU_STREAM_SEEK);
if (rc == 0)
{
mu_stream_t stream = (mu_stream_t) fstr;
......
......@@ -440,7 +440,7 @@ _prog_open (mu_stream_t stream)
if (REDIRECT_STDOUT_P (flags))
{
rc = mu_stdio_stream_create (&fs->in, pfd[0],
MU_STREAM_READ|MU_STREAM_FD_AUTOCLOSE|seekable_flag);
MU_STREAM_READ|seekable_flag);
if (rc)
{
_prog_close (stream);
......@@ -451,7 +451,7 @@ _prog_open (mu_stream_t stream)
if (REDIRECT_STDIN_P (flags))
{
rc = mu_stdio_stream_create (&fs->out, pfd[1],
MU_STREAM_WRITE|MU_STREAM_FD_AUTOCLOSE|seekable_flag);
MU_STREAM_WRITE|seekable_flag);
if (rc)
{
_prog_close (stream);
......
......@@ -99,8 +99,7 @@ mu_socket_stream_create (mu_stream_t *pstream, const char *filename, int flags)
/* Create transport stream. */
rc = _mu_file_stream_create (&fstr, sizeof (*fstr),
filename, -1,
(flags | MU_STREAM_FD_AUTOCLOSE) &
~MU_STREAM_SEEK);
flags & ~MU_STREAM_SEEK);
if (rc)
return rc;
fstr->stream.open = _socket_open;
......
......@@ -67,8 +67,7 @@ mu_temp_file_stream_create (mu_stream_t *pstream,
NULL,
-1,
MU_STREAM_RDWR | MU_STREAM_SEEK |
MU_STREAM_CREAT |
MU_STREAM_FD_AUTOCLOSE);
MU_STREAM_CREAT);
if (rc == 0)
{
struct _mu_temp_file_stream *tstr = (struct _mu_temp_file_stream *)str;
......
......@@ -49,6 +49,7 @@ main (int argc, char **argv)
char *infile = NULL;
int verify = 0;
int verbose = 0;
int yes = 1;
progname = argv[0];
......@@ -139,6 +140,7 @@ main (int argc, char **argv)
MU_ASSERT (mu_file_stream_create (&in, infile, MU_STREAM_READ));
MU_ASSERT (mu_fd_stream_create (&out, filename, fd, MU_STREAM_WRITE));
mu_stream_ioctl (out, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
MU_ASSERT (mu_stream_copy (out, in, 0, &size));
if (verbose)
printf ("copied %lu bytes to the temporary\n", (unsigned long) size);
......@@ -154,6 +156,7 @@ main (int argc, char **argv)
MU_ASSERT (mu_stdio_stream_create (&out, MU_STDOUT_FD, 0));
MU_ASSERT (mu_fd_stream_create (&in, filename, fd,
MU_STREAM_READ|MU_STREAM_SEEK));
mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
MU_ASSERT (mu_stream_copy (out, in, 0, &size));
if (verbose)
printf ("dumped %lu bytes\n", (unsigned long) size);
......
......@@ -136,7 +136,9 @@ mu_app_init (struct argp *myargp, const char **capa,
char *comment;
char *canonical_name = get_canonical_name ();
mu_stream_t stream;
mu_stdio_stream_create (&stream, MU_STDOUT_FD, 0);
mu_asprintf (&comment,
"Configuration file structure for %s utility.",
mu_program_name);
......
......@@ -579,8 +579,7 @@ lmtp_connection (int fd, struct sockaddr *sa, int salen, void *data,
mu_stream_t str;
int rc;
rc = mu_fd_stream_create (&str, NULL, fd,
MU_STREAM_RDWR | MU_STREAM_FD_AUTOCLOSE);
rc = mu_fd_stream_create (&str, NULL, fd, MU_STREAM_RDWR);
if (rc)
{
mu_diag_funcall (MU_DIAG_ERROR, "mu_fd_stream_create", NULL, rc);
......
......@@ -444,6 +444,7 @@ quote0 (msgset_t *mspec, mu_message_t mesg, void *data)
char *prefix = "\t";
mu_stream_t outstr, flt;
char *argv[3];
int yes = 1;
fprintf (stdout, _("Interpolating: %lu\n"),
(unsigned long) mspec->msg_part[0]);
......@@ -457,6 +458,9 @@ quote0 (msgset_t *mspec, mu_message_t mesg, void *data)
mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create", NULL, rc);
return rc;
}
/* Set borrow flag to prevent fileno (ofile) from being closed on
destroying outstr. */
mu_stream_ioctl (outstr, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
argv[0] = "INLINE-COMMENT";
argv[1] = prefix;
argv[2] = NULL;
......
......@@ -123,6 +123,7 @@ main (int argc, char **argv)
mu_stream_t in;
size_t size = 0;
char *p;
int yes = 1;
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
......@@ -130,7 +131,7 @@ main (int argc, char **argv)
mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
exit (1);
}
mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
if (isatty (0))
{
printf (_("Component name: "));
......
......@@ -759,6 +759,7 @@ mh_real_install (char *name, int automode)
mu_stream_t in;
int rc;
FILE *fp;
int yes;
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
......@@ -766,6 +767,7 @@ mh_real_install (char *name, int automode)
mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
exit (1);
}
mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
mhdir = mh_safe_make_file_name (home, "Mail");
......
......@@ -336,6 +336,7 @@ _whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
size_t size = 0;
struct mu_wordsplit ws;
int wsflags = MU_WRDSF_DEFFLAGS|MU_WRDSF_COMMENT;
int yes = 1;
rc = mu_stdio_stream_create (&in, MU_STDIN_FD, 0);
if (rc)
......@@ -343,6 +344,7 @@ _whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
mu_error (_("cannot create input stream: %s"), mu_strerror (rc));
exit (1);
}
mu_stream_ioctl (in, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
do
{
......
......@@ -151,7 +151,14 @@ open_output ()
if (moreproc)
rc = mu_command_stream_create (&output, moreproc, MU_STREAM_WRITE);
else
{
rc = mu_stdio_stream_create (&output, MU_STDOUT_FD, MU_STREAM_WRITE);
if (rc == 0)
{
int yes = 1;
mu_stream_ioctl (output, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
}
}
if (rc)
{
......@@ -170,7 +177,14 @@ list_message (char *name, mu_stream_t output)
mu_message_t msg;
if (!name)
{
rc = mu_stdio_stream_create (&input, MU_STDIN_FD, MU_STREAM_SEEK);
if (rc == 0)
{
int yes = 1;
mu_stream_ioctl (input, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
}
}
else
rc = mu_file_stream_create (&input, name, MU_STREAM_READ);
if (rc)
......
......@@ -1593,6 +1593,7 @@ mhn_show ()
{
int rc;
mu_stream_t ostr;
int yes = 1;
rc = mu_stdio_stream_create (&ostr, MU_STDOUT_FD, 0);
if (rc)
......@@ -1600,6 +1601,7 @@ mhn_show ()
mu_error (_("cannot create output stream: %s"), mu_strerror (rc));
exit (1);
}
mu_stream_ioctl (ostr, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
mhl_format = mhl_format_compile (formfile);
......@@ -1805,6 +1807,11 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
rc = mu_stdio_stream_create (&out, MU_STDOUT_FD, 0);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_stdio_stream_create", NULL, rc);
else
{
int yes = 1;
mu_stream_ioctl (out, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
}
break;
}
......
......@@ -254,9 +254,12 @@ display_file (const char *type)
mu_error (_("cannot create header: %s"), mu_strerror (status));
else
{
int yes = 1;
mu_stdio_stream_create (&stream, fileno (mimeview_fp),
MU_STREAM_READ|
MU_STREAM_SEEK);
mu_stream_ioctl (stream, MU_IOCTL_FD, MU_IOCTL_FD_SET_BORROW, &yes);
display_stream_mailcap (mimeview_file, stream, hdr,
no_ask_types, interactive, dry_run,
......
......@@ -134,13 +134,11 @@ pop3d_setio (int ifd, int ofd)
if (ofd == -1)
pop3d_abquit (ERR_NO_OFILE);
if (mu_stdio_stream_create (&istream, ifd,
MU_STREAM_READ | MU_STREAM_FD_AUTOCLOSE))
if (mu_stdio_stream_create (&istream, ifd, MU_STREAM_READ))
pop3d_abquit (ERR_NO_IFILE);
mu_stream_set_buffer (istream, mu_buffer_line, 0);
if (mu_stdio_stream_create (&ostream, ofd,
MU_STREAM_WRITE | MU_STREAM_FD_AUTOCLOSE))
if (mu_stdio_stream_create (&ostream, ofd, MU_STREAM_WRITE))
pop3d_abquit (ERR_NO_OFILE);
/* Combine the two streams into an I/O one. */
......