Create socket

296 views
Skip to first unread message

Archos

unread,
Jun 14, 2012, 8:00:07 PM6/14/12
to golang-nuts
How to create a socket file in Go?

In C would be the function [mknod](http://linux.die.net/man/2/mknod)

Kyle Lemons

unread,
Jun 14, 2012, 8:10:48 PM6/14/12
to Archos, golang-nuts
Are you trying to use unix domain sockets?  If so, listening on the file should do it.

Archos

unread,
Jun 14, 2012, 8:16:30 PM6/14/12
to golang-nuts

On Jun 15, 1:10 am, Kyle Lemons <kev...@google.com> wrote:
> Are you trying to use unix domain sockets?  If so, listening on the file
> should do it.
But I want to have a file in /dev, just like is done by rsyslogd which
uses the next sockets file:

# ll /dev/log
srw-rw-rw- 1 root root 0 jun 14 07:11 /dev/log=

Kyle Lemons

unread,
Jun 14, 2012, 8:22:26 PM6/14/12
to Archos, golang-nuts
Can you not listen on the path you want?

Archos

unread,
Jun 14, 2012, 8:34:47 PM6/14/12
to golang-nuts

On Jun 15, 1:22 am, Kyle Lemons <kev...@google.com> wrote:
> Can you not listen on the path you want?
Ok, the domain will listen there. It's only just that I thought it
could be better if were created that device (like in C) at installing
the program.

Kyle Lemons

unread,
Jun 14, 2012, 8:48:06 PM6/14/12
to Archos, golang-nuts
I don't think that's how rsyslogd does it either.  Sockets are owned by a particular PID, if I understand it correctly, and you can't listen to a socket created by another process, so you delete it and recreate it if it's there on startup (possibly after checking if the original daemon is still alive).

Dave Cheney

unread,
Jun 14, 2012, 8:51:52 PM6/14/12
to Archos, golang-nuts

Archos

unread,
Jul 26, 2012, 8:20:06 PM7/26/12
to golan...@googlegroups.com
Just for archiving:

        oldmask := syscall.Umask(0)
        err := syscall.Mknod("/path/to/socket", syscall.S_IFSOCK|0666, 0)
        if err != nil {
            log.Fatal(err)
        }
        syscall.Umask(oldmask)

Archos

unread,
Jul 27, 2012, 4:19:28 AM7/27/12
to golan...@googlegroups.com
Ops! Call to syscall.Umask before of exit if you want not lose the mask:


        oldmask := syscall.Umask(0)
        err := syscall.Mknod("/path/to/socket", syscall.S_IFSOCK|0666, 0)
        syscall.Umask(oldmask)

Archos

unread,
Jul 27, 2012, 4:29:37 AM7/27/12
to golan...@googlegroups.com
It is not necessary to call ` syscall.Umask(oldmask)`, at least that you need call to functions that use the mask and you need the original value.
Reply all
Reply to author
Forward
0 new messages