Permalink
Browse files

added get_peerpid/get_peercred

  • Loading branch information...
tonyrog committed Feb 14, 2013
1 parent f58c923 commit 32f2c7435e163edff02165b34c1875b856bc1b6d
Showing with 46 additions and 0 deletions.
  1. +25 −0 c_src/afunix_drv.c
  2. +21 −0 src/afunix.erl
View
@@ -80,6 +80,7 @@ typedef unsigned long long llu_t;
#include <sys/ioctl.h>
#include <sys/un.h>
+#include <sys/ucred.h>
// FIXME use dlib!!!
#define DLOG_DEBUG 7
@@ -381,6 +382,7 @@ typedef unsigned long long llu_t;
#define UDP_OPT_ADD_MEMBERSHIP 14 /* add an IP group membership */
#define UDP_OPT_DROP_MEMBERSHIP 15 /* drop an IP group membership */
#define INET_OPT_IPV6_V6ONLY 16 /* IPv6 only socket, no mapped v4 addrs */
+
/* LOPT is local options */
#define INET_LOPT_BUFFER 20 /* min buffer size hint */
#define INET_LOPT_HEADER 21 /* list header size */
@@ -401,6 +403,9 @@ typedef unsigned long long llu_t;
#define INET_LOPT_TCP_MSGQ_HIWTRMRK 36 /* set local high watermark */
#define INET_LOPT_TCP_MSGQ_LOWTRMRK 37 /* set local low watermark */
+#define UNIX_OPT_PEERCRED 101
+#define UNIX_OPT_PEERPID 102
+
/* INET_REQ_GETSTAT enumeration */
#define INET_STAT_RECV_CNT 1
#define INET_STAT_RECV_MAX 2
@@ -2965,6 +2970,26 @@ static ErlDrvSSizeT inet_fill_opts(inet_descriptor* desc,
put_int32(arg_sz,ptr);
continue;
}
+
+ case UNIX_OPT_PEERCRED: {
+ struct xucred x;
+ socklen_t xucredlen = sizeof(x);
+ if (IS_SOCKET_ERROR(sock_getopt(desc->s,SOL_LOCAL,LOCAL_PEERCRED,
+ &x,&xucredlen)) ||
+ (x.cr_version != XUCRED_VERSION)) {
+ TRUNCATE_TO(0,ptr);
+ continue;
+ }
+ *ptr++ = opt;
+ put_int32(x.cr_uid, ptr);
+ continue;
+ }
+
+ case UNIX_OPT_PEERPID:
+ type = LOCAL_PEERPID;
+ proto = SOL_LOCAL;
+ break;
+
default:
RETURN_ERROR();
}
View
@@ -24,11 +24,17 @@
-export([shutdown/2]).
-export([controlling_process/2]).
-export([fdopen/2]).
+-export([get_peercred/1]).
+-export([get_peerpid/1]).
-include_lib("kernel/src/inet_int.hrl").
-define(INET_AF_UNIX, 5).
+-define(UNIX_OPT_PEERCRED, 101).
+-define(UNIX_OPT_PEERPID, 102).
+
+
%-define(DEBUG, 1).
-ifdef(DEBUG).
-define(DBG_FORMAT(Format, Args), (io:format((Format), (Args)))).
@@ -263,6 +269,21 @@ async_connect(S, Name, Time) ->
{error,_}=Error -> Error
end.
+%% get peer-credentials (only effecive uid right now)
+get_peercred(S) ->
+ case ctl_cmd(S, ?INET_REQ_GETOPTS, [?UNIX_OPT_PEERCRED]) of
+ {ok,[?UNIX_OPT_PEERCRED,U3,U2,U1,U0]} ->
+ {ok, ?u32(U3,U2,U1,U0)};
+ {error,_}=Error -> Error
+ end.
+%% get peer-credentials (only effecive uid right now)
+get_peerpid(S) ->
+ case ctl_cmd(S, ?INET_REQ_GETOPTS, [?UNIX_OPT_PEERPID]) of
+ {ok,[?UNIX_OPT_PEERPID,U3,U2,U1,U0]} ->
+ {ok, ?u32(U3,U2,U1,U0)};
+ {error,_}=Error -> Error
+ end.
+
prim_bind(S,"") when is_port(S) ->
{ok,""};
prim_bind(S,Name) when is_port(S), is_list(Name) ->

0 comments on commit 32f2c74

Please sign in to comment.