Re: perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet.

Corinna Vinschen <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
On Jul  5 19:41, Takashi Yano wrote:
> Hi Corinna,
> 
> On Thu, 5 Jul 2018 11:08:51 +0200
> Corinna Vinschen wrote:
> > Ouch!  I didn't realize that writev is not a required function on
> > bare metal, sorry.
> > 
> > Takashi, we need a patch to implement perror/psignal without writev, for
> > instance by calling write twice.  Care to send a followup patch?
> 
> Patch attached.
> 
> I am not sure whether _newlib_flockfile_start()/end() is necessary or not.
> Could you please check?

Yes, we need it, the entire operation, flushing and writing, must be
cancel-safe and synchronized with other access to the stderr FILE.

Comparing with FreeBSD, there's also something missing.  After the
write operation, the offset in the FILE structure is incorrect.
Consequentially the __SOFF flag is reset to 0 last thing before
unlocking the file:

  stderr->_flags &= ~__SOFF;

Also:

> +#define WRITE_STR(str) \
>  { \
> -  v->iov_base = (void *)(str); \
> -  v->iov_len = strlen (v->iov_base); \
> -  v ++; \
> -  iov_cnt ++; \
> +  const char *p = (str); \
> +  size_t len = strlen (p); \
> +  while (len) \
> +    { \
> +      ssize_t len1 = write (fileno (stderr), p, len); \
> +      if (len1 < 0) break; \

Please put the break on a line of its own:

   if (len1 < 0) \
     break; \

> [...]
> +#define WRITE_STR(str) \
>  { \
> -  v->iov_base = (void *)(str); \
> -  v->iov_len = strlen (v->iov_base); \
> -  v ++; \
> -  iov_cnt ++; \
> +  const char *p = (str); \
> +  size_t len = strlen (p); \
> +  while (len) \
> +    { \
> +      ssize_t len1 = _write_r (ptr, fileno (fp), p, len); \
> +      if (len1 < 0) break; \

Ditto.

> +      len -= len1; \
> +      p += len1; \
> +    } \
>  }
>  
>  void
> @@ -73,31 +77,25 @@ _perror_r (struct _reent *ptr,
>  {
>    char *error;
>    int dummy;
> -  struct iovec iov[4];
> -  struct iovec *v = iov;
> -  int iov_cnt = 0;
>    FILE *fp = _stderr_r (ptr);
>  
>    CHECK_INIT (ptr, fp);
> +
> +  fflush (fp);

I only just noticed, sorry.  Please call

     _fflush_r (ptr, fp);


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAls9/rkACgkQ9TYGna5E
T6Drrg/+IjK/trjWqbEtZCCHjARqu0KhCLmz0nUcNkt83zpE5du6eHk2TKX/srLK
RrOKPadeq0ztDmvQ8855zakpt00iee6WWMqe0AQrgZSfr8f3mHHZMzvL+BIJrxSL
O7Vd4vDt/8EY8M7L0rpjdmUN+Nj+NGSqKZS/pR/Sh3R3RcCEs4Vg8JIkvxJBNuAN
2fkySVi0aevwM3hKfBYds9axA3ELW6xUkzsDNF2FUw8DlbvK8AZxScijCw9a8/eu
+x5Aq5mWjmfC+kpjLiWWLoAe/TzvwuZ34LYHaWWPHJTTfDS06usDXL7jC6t8vc7d
fyAV///u2uqK67VtuwgHTd2oA1e1H8bz02OQUmBkCykHukHXvsdKwpD1j0f91xMf
5EYSFM9C2zW+GEEznuh/jF6EsgB9tHLMGOATAQvb6BOlrH/bbnYsV0Bt/TM9gjl3
XNGqOM6+3td5v6VTd2r6Z0pWCb4wQV9P+SGiRPxs65HYDu0TPqNN8y4+7nN39TRu
03bNg+GSM20Menom8uvs0BOGy3TvQ2w87ANJTjyqLZ0bSLCinnpEL3Pm08ku+C0B
xR74z6/DiVGzt3omQK8GmvDSpNlIZ6Sd1tpAmSdP66miKgWFzhAVjhFDAoIB/n3R
Y2nlbirPkXuf/V6cP55315hQGM4J86Y4Fmd4589sTrG9NhU6t5k=
=o1k0
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.