[LIP] Getting client credentials through a UNIX Domain Socket.
Shehjar Tikoo <shehjar_tikoo-/[email protected]>
| Newsgroups | gmane.user-groups.linux.india.programmers |
|---|---|
| Message-ID | <[email protected]> |
Hi all,
I need a little help on getting client process'
credentials on a Unix Domain Socket.I have been
googling for the past few hours but have'nt been
able to locate a page with accurate info on how
to get client credentials through UNIX Domains sockets
'on linux'.
Some of sites that have some info, refer to the
SCM_CREDENTIALS control messages and recommend
going through the kernel source, which I did.
Now the code I wrote is a mix of what I read in
UNPv1,Stevens and what I read on the web, but it
doesn't work.
Please see the code below or the next paragraph for
a brief on the problem.The code is longer than
usual, please bear with me.
The problem I face is like this, when I try to get
client credentials on linux, I get a 'Resource
temporarily unavailible' in recvmsg.The code returns
thereafter.
When I comment out the return statements,
the execution continues after printing the error and
the read calls that I make are able to read from the
socket descriptor(here in connfd) without any
problems.
TIA,
Shehjar
===========CODE BELOW================
/* Some of the code is from UNPv1, Stevens. */
/* For getting client credentials. */
struct msghdr msg;
struct ucred cli_cred;
union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(struct ucred))];
} control_un;
struct cmsghdr *cmptr;
cmptr=NULL;
msg.msg_control=control_un.control;
msg.msg_controllen=sizeof(control_un.control);
msg.msg_name=NULL;
msg.msg_namelen=0;
/* I don't want to read anything from the
socket except the credentials.
*/
msg.msg_iov=NULL;
msg.msg_iovlen=0;
/* Getting credentials here. */
if(recvmsg(connfd, &msg, 0)<0) {
tmperr=errno;
/* This is where I get a
* 'Resource temporarily unavailible'
*/
fprintf(log, "dl_run:Could not get
client");
fprintf(log," credentials:%s\n",
strerror(tmperr));
return -1;
}
if(msg.msg_controllen > sizeof(struct
cmsghdr)) {
cmptr=CMSG_FIRSTHDR(&msg);
if(cmptr->cmsg_len !=
CMSG_LEN(sizeof(struct
ucred))) {
fprintf(log, "dl_run:No client
credentials.\n");
return -1;
}
if(cmptr->cmsg_level!=SOL_SOCKET) {
fprintf(log, "dl_run:No client
credentials.\n");
return -1;
}
if(cmptr->cmsg_type!=SCM_CREDENTIALS)
{
fprintf(log, "dl_run:No client
credentials.\n");
return -1;
}
memcpy(&cli_cred, CMSG_DATA(cmptr),
sizeof(struct
ucred));
}
else {
fprintf(log, "dl_run: No client
credentials.\n");
return -1;
}
=========================
Thanks again.
__________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click