File: sockopen.c

package info (click to toggle)
eflite 0.3.8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 272 kB
  • ctags: 180
  • sloc: ansic: 1,653; makefile: 91
file content (35 lines) | stat: -rw-r--r-- 620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* sockopen.c -- Function to open a socket
 * $id$
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <syslog.h>

int sockopen(const char *fname)
{
  int sock;
  struct sockaddr_un addr;

  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock == -1)
  {
    return -1;
  }
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, fname, sizeof(addr.sun_path));
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
  {
    close(sock);
    return -1;
  }
  if (listen(sock, 512) == -1)
  {
    close(sock);
    return -1;
  }
  return sock;
}