bind() to /dev/log failed

Rick Stevens ricks at nerd.com
Thu Jul 22 16:21:34 UTC 2010


On 07/21/2010 11:29 PM, 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 can see a couple of things.  First, [r|ng]syslogd _listens_ to 
/dev/log, it is not the output.  If you want to log things, you write
to /dev/log and syslogd will log it.  This, for example, is how the
utility "logger(1)" works.  If you're trying to monitor what's being
fed _to_ [r|ng]syslogd, this probably won't work as syslogd has already
opened the socket for read.

Second, If you're going to do a bind(), you must do it before the
connect(), not after.

Third, a bind() is not necessary if you are a client--only if you're
the server.  You can bind() as a client if you wish to have a specific
address assigned to you.  However your code is trying to use the
already existing data for syslogd.  You'd need to create a unique
pathname to bind to you.

----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, C2 Hosting          ricks at nerd.com -
- AIM/Skype: therps2        ICQ: 22643734            Yahoo: origrps2 -
-                                                                    -
-                     "Celibacy is not hereditary."                  -
-                                      -- Guy Goden                  -
----------------------------------------------------------------------


More information about the users mailing list