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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ankit
  • 1,240
  • 2
  • 13
  • 16
  • 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. – user207421 Oct 12 '14 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? – Ankit Oct 12 '14 at 11:30
  • The error message asks 'have you started it?'. What's the answer? – user207421 Nov 19 '19 at 07:13

1 Answers1

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.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • But isn't /dev/log socket always up? http://swift.siphos.be/linux_sea/logfilemanagement.html – Ankit Oct 12 '14 at 11:13
  • 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. – Ankit Nov 09 '14 at 14:18
  • Added you notes to the main answer to gain more visibility :) – tmarwen Nov 09 '14 at 21:28