On Sat, 10 Oct 2015, Philip Guenther wrote:
> For userspace, SUN_LEN() is an unnecessary BSD-ism.  The kernel has to 
> accept sizeof(struct sockaddr_un) here, so do the simple, portable thing.

That was the usr.bin diff; here's the usr.sbin diff.

This also switches some strncpy() to strlcpy(), as the trailing part of 
the path does *not* need to NULs.

(Note that userspace does *not* need to set sun_len; the kernel ignores 
the incoming value and calculates the value it needs.)

ok?

Phliip Guenther

Index: apm/apm.c
===================================================================
RCS file: /cvs/src/usr.sbin/apm/apm.c,v
retrieving revision 1.29
diff -u -p -r1.29 apm.c
--- apm/apm.c   16 Jan 2015 06:40:15 -0000      1.29
+++ apm/apm.c   10 Oct 2015 23:43:22 -0000
@@ -130,9 +130,8 @@ open_socket(const char *sockname)
                err(1, "cannot create local socket");
 
        s_un.sun_family = AF_UNIX;
-       strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
-       s_un.sun_len = SUN_LEN(&s_un);
-       if (connect(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) {
+       strlcpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
+       if (connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) {
                errr = errno;
                close(sock);
                errno = errr;
Index: apmd/apmd.c
===================================================================
RCS file: /cvs/src/usr.sbin/apmd/apmd.c,v
retrieving revision 1.76
diff -u -p -r1.76 apmd.c
--- apmd/apmd.c 28 Aug 2015 16:13:58 -0000      1.76
+++ apmd/apmd.c 10 Oct 2015 23:43:22 -0000
@@ -212,14 +212,13 @@ bind_socket(const char *sockname)
                error("cannot create local socket", NULL);
 
        s_un.sun_family = AF_UNIX;
-       strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
-       s_un.sun_len = SUN_LEN(&s_un);
+       strlcpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
 
        /* remove it if present, we're moving in */
        (void) remove(sockname);
 
        old_umask = umask(077);
-       if (bind(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1)
+       if (bind(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == -1)
                error("cannot connect to APM socket", NULL);
        umask(old_umask);
        if (chmod(sockname, 0660) == -1 || chown(sockname, 0, 0) == -1)
Index: lpr/common_source/startdaemon.c
===================================================================
RCS file: /cvs/src/usr.sbin/lpr/common_source/startdaemon.c,v
retrieving revision 1.15
diff -u -p -r1.15 startdaemon.c
--- lpr/common_source/startdaemon.c     16 Jan 2015 06:40:17 -0000      1.15
+++ lpr/common_source/startdaemon.c     10 Oct 2015 23:43:23 -0000
@@ -66,7 +66,7 @@ startdaemon(char *printer)
        strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path));
        siginterrupt(SIGINT, 1);
        PRIV_START;
-       if (connect(s, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
+       if (connect(s, (struct sockaddr *)&un, sizeof(un)) < 0) {
                int saved_errno = errno;
                if (errno == EINTR && gotintr) {
                        PRIV_END;
Index: lpr/lpd/lpd.c
===================================================================
RCS file: /cvs/src/usr.sbin/lpr/lpd/lpd.c,v
retrieving revision 1.59
diff -u -p -r1.59 lpd.c
--- lpr/lpd/lpd.c       29 Sep 2015 02:37:29 -0000      1.59
+++ lpr/lpd/lpd.c       10 Oct 2015 23:43:23 -0000
@@ -280,7 +280,7 @@ main(int argc, char **argv)
        un.sun_family = AF_LOCAL;
        strlcpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path));
        PRIV_START;
-       if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
+       if (bind(funix, (struct sockaddr *)&un, sizeof(un)) < 0) {
                syslog(LOG_ERR, "ubind: %m");
                exit(1);
        }
Index: syslogd/syslogd.c
===================================================================
RCS file: /cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.195
diff -u -p -r1.195 syslogd.c
--- syslogd/syslogd.c   10 Oct 2015 20:35:01 -0000      1.195
+++ syslogd/syslogd.c   10 Oct 2015 23:43:25 -0000
@@ -491,9 +491,6 @@ main(int argc, char *argv[])
                        die(0);
        }
 
-#ifndef SUN_LEN
-#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
-#endif
        for (i = 0; i < nunix; i++) {
                fd_unix[i] = unix_socket(path_unix[i], SOCK_DGRAM, 0666);
                if (fd_unix[i] == -1) {
@@ -2813,7 +2810,7 @@ unix_socket(char *path, int type, mode_t
        old_umask = umask(0177);
 
        unlink(path);
-       if (bind(fd, (struct sockaddr *)&s_un, SUN_LEN(&s_un)) == -1) {
+       if (bind(fd, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) {
                snprintf(ebuf, sizeof(ebuf), "cannot bind %s", path);
                logerror(ebuf);
                umask(old_umask);


Reply via email to