0

My project aims at reading log messages directly from /dev/log UNIX domain socket in Java. Currently I am using junixsocket. Below is a sample code of client that reads from a unix socket.

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import org.newsclub.net.unix.AFUNIXSocketException;

public class SimpleTestClient {
    public static void main(String[] args) throws IOException {
        final File socketFile = new File("/dev/log");

        AFUNIXSocket sock = AFUNIXSocket.newInstance();
        try {
            sock.connect(new AFUNIXSocketAddress(socketFile));
        } catch (AFUNIXSocketException e) {
            System.out.println("Cannot connect to server. Have you started it?\n");
            System.out.flush();
            throw e;
        }
        System.out.println("Connected");

        InputStream is = sock.getInputStream();

        byte[] buf = new byte[8192];

        int read = is.read(buf);
        System.out.println("Server says: " + new String(buf, 0, read));
    
        is.close();

        sock.close();

        System.out.println("End of communication.");
    }
}

The above mentioned code is unable to connect to /dev/log. It throws an exception:

Cannot connect to server. Have you started it?
Exception in thread "main" org.newsclub.net.unix.AFUNIXSocketException: Protocol wrong type for socket (socket: /dev/log)
        at org.newsclub.net.unix.NativeUnixSocket.connect(Native Method)
        at org.newsclub.net.unix.AFUNIXSocketImpl.connect(AFUNIXSocketImpl.java:125)
        at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:97)
        at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:87)
        at SimpleTestClient.main(SimpleTestClient.java:40)

I am unable to figure out how to solve this problem. Any help would be appreciable.

3
  • Code that depends on the success of code in a try block should be in the same try block. The real problem here is the connect failure, not the failure to read. You just printed it and continued as though it hadn't hapoened. Don't write code like this. Commented Oct 12, 2014 at 11:02
  • @EJP - Yes I understand that the program is unable to connect to /dev/log socket. My question is why is it not able to connect to the socket? What am I doing wrong? Commented Oct 12, 2014 at 11:30
  • The error message asks 'have you started it?'. What's the answer? Commented Nov 19, 2019 at 7:13

1 Answer 1

1

Since you cannot connect to an existing server socket as mentioned in the log traces, then you haven't bound one one the mentioned file, so try creating an AF_UNIX server socket then connect to it.

It could be done in a separate class:

public class DevLogServer {

  public static void main(String[] args) throws IOException {

    final File socketFile = new File("/dev/log");
    AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();
    try {
      server.bind(new AFUNIXSocketAddress(socketFile));
    } catch (Exception e) {
      throw e;
    }

  }

}

Edit as per @Ankit comment:

You may also need to make sure the syslod daemon is stopped by runnig below command in a terminal window:

sudo service syslog stop

You may alternatively need to grand write permission to the /dev directory.

Sign up to request clarification or add additional context in comments.

3 Comments

But isn't /dev/log socket always up? swift.siphos.be/linux_sea/logfilemanagement.html
I would like to add few more point: - User first has to stop the syslod deamon by using the command 'sudo service syslog stop' - User also needs to change the permissions of /dev directory so that the program is able to create a socket inside /dev directory.
Added you notes to the main answer to gain more visibility :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.