On 07/22/2010 02:29 AM, Mansuri, Wasim (NSN - IN/Bangalore) wrote:
Hello All,
I want to listen to /dev/log. For that I am using the following code.
Please let me know whats the issue in the following code snip. Code I am running with root user.
Thanks, wasim
if ((sockfd = socket(AF_UNIX,SOCK_STREAM, 0)) == -1) { printf("Failed To open AF_UNIX Socket"); return -1; } sunx.sun_family = AF_UNIX; strncpy(sunx.sun_path, PR_PATH_LOG, sizeof(sunx.sun_path)); /*if (connect(sockfd, &syslog_addr, sizeof(syslog_addr)) == -1) { //Connect call goes successful, But after that I am not able to recv any data.*/ if (bind(sockfd, (struct sockaddr *) &sunx,sizeof(sunx.sun_family)+strlen(sunx.sun_path)) < 0) { /* bind fails saying 98 : Address already in use Even if I kill syslogd*/ printf("Connect failed with the errno %s %d : %s\n",PR_PATH_LOG, errno,strerror(errno)); close(sockfd); }
if(recv( sockfd, (void *) buff, 1024,0) <=0) /* recv never come out if I use connect even after I do logger */ { printf("Error in recv call... errno = %d : %s : %s\n", errno, strerror(errno),buff); break; }
I'm not 100% understanding exactly what you want to do. First, I think you should use 'PF-INET' not 'AF_UNIX'. In any case, the sequence of calls should be: socket(2), bind(2), listen(2), accept(2), recv(2).
The connect(2) function is used on the sending side.
You can use the lsof(8) command to see if that socket is already in use.