bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug#187391: PortingIssues sockaddr_un


From: Ognyan Kulev
Subject: Re: Bug#187391: PortingIssues sockaddr_un
Date: Mon, 07 Apr 2003 16:48:07 +0300
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030327 Debian/1.3-4

Ognyan Kulev wrote:
This discussion is about
http://hurd.gnufans.org/bin/view/Distrib/PortingIssues#Uncompliant_use_of_sockaddr_un_t
We need an agreement about what should be in this page.

Robert Millan wrote:

On Sat, Apr 05, 2003 at 06:01:16PM +0300, Ognyan Kulev wrote:

sub.sun_path[sizeof(sun.sun_path) - 1] = '\0';

I've changed my mind about this, so I've added it again.

I understand that my changes are somewhat intrusive to what Robert Millan wrote, but I wouldn't do it if I didn't beleive they are right. Please say your mind about the current text.

 Uncompliant use of sockaddr_un

The following declaration:

sockaddr_un sun = { AF_UNIX, "/path/to/socket" };

won't work on GNU/Hurd. The Glibc API requires that the second parameter of a sockaddr_un struct is array of chars, but NOT pointer to array of chars. So we have to copy them directly into the sun_path address. Glibc wants string of chars there that doesn't need to end in nul character _and_ correct size of sockaddr_un passed to socket functions. When calling socket functions one should always use SUN_LEN (su) for the sockaddr length argument. An example:

sockaddr_un su;
/* AF_LOCAL is the canonical flag in Glibc.
   Use AF_UNIX if you want compatibility.  */
su.sun_family = AF_LOCAL;
/* Copy the string into sun_path. It must be
   no longer than approximately 100 characters. */
strcpy (su.sun_path, "/path/to/socket");

SUN_LEN macro is not defined in The Single UNIX Specification Version 3 (see sys/un.h manpage). You can use the following definition to handle this case (definition taken from NetBSD):

#ifndef SUN_LEN
#define SUN_LEN(su) \
   (sizeof(*(su)) - sizeof((su)->sun_path) + \
   strlen((su)->sun_path))
#endif

If you have pointer to array of characters as file name, you'd better use the following code to set sun_path:

strncpy (su.sun_path, filename, sizeof (su.sun_path));
su.sun_path[sizeof (su.sun_path) - 1] = '\0';

If you expect file name longer than approximately 100 characters, use the following code. This is the recommended code for GNU systems. You can include it whenever __GLIBC__ is defined.

/* `offsetof', `alloca' and `AF_LOCAL' may not
   be available everywhere.  */
socklen_t su_len = offsetof (struct sockaddr_un, sun_path)
                   + strlen (filename) + 1;
struct sockaddr_un *su = alloca (su_len);
su->sun_family = AF_LOCAL;
strcpy (su->sun_path, filename);

NOTE: the current API is subject to change, see the notes in Glibc's docs ("info libc" and search for sockaddr_un) and Debian bug #187391.

Regards
--
Ognyan Kulev <ogi@fmi.uni-sofia.bg>, "\"Programmer\""





reply via email to

[Prev in Thread] Current Thread [Next in Thread]