Skip to content

Commit d391091

Browse files
committed
server session MAINTENANCE unix socket acccept refactor
1 parent 4e903c3 commit d391091

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/session_server.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,26 +1721,24 @@ nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *))
17211721
static int
17221722
nc_get_uid(int sock, uid_t *uid)
17231723
{
1724-
#ifdef SO_PEERCRED
1725-
struct ucred ucred;
1726-
socklen_t len;
1727-
len = sizeof(ucred);
1728-
if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
1729-
ERR("Failed to get credentials from unix socket (%s).",
1730-
strerror(errno));
1731-
close(sock);
1732-
return -1;
1733-
}
1724+
int ret;
1725+
1726+
#ifdef SO_PEERCRED
1727+
struct ucred ucred;
1728+
socklen_t len;
1729+
len = sizeof(ucred);
1730+
ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len);
1731+
if (!ret) {
17341732
*uid = ucred.uid;
1735-
#else
1736-
if (getpeereid(sock, uid, NULL) < 0) {
1737-
ERR("Failed to get credentials from unix socket (%s).",
1738-
strerror(errno));
1739-
close(sock);
1740-
return -1;
1741-
}
1742-
#endif
1733+
}
1734+
#else
1735+
ret = getpeereid(sock, uid, NULL);
1736+
#endif
17431737

1738+
if (ret < 0) {
1739+
ERR("Failed to get credentials from unix socket (%s).", strerror(errno));
1740+
return -1;
1741+
}
17441742
return 0;
17451743
}
17461744

@@ -1753,7 +1751,8 @@ nc_accept_unix(struct nc_session *session, int sock)
17531751
session->ti_type = NC_TI_UNIX;
17541752
uid_t uid;
17551753

1756-
if (nc_get_uid(sock, &uid) < 0) {
1754+
if (nc_get_uid(sock, &uid)) {
1755+
close(sock);
17571756
return -1;
17581757
}
17591758

0 commit comments

Comments
 (0)