summaryrefslogtreecommitdiff
path: root/libdiskfs/ifsock.c
blob: 0c21c53de47c11615b6fcd094f73dfcd46c26b1f (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Implement ifsock inteface
   Copyright (C) 1994 Free Software Foundation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "priv.h"
#include "ifsock_S.h"
#include <hurd/paths.h>
#include <sys/socket.h>
#include <stdio.h>
#include <hurd/socket.h>

static spin_lock_t pflocalserverlock = SPIN_LOCK_INITIALIZER;
static mach_port_t pflocalserver = MACH_PORT_NULL;

kern_return_t
diskfs_S_ifsock_getsockaddr (struct protid *cred,
			     mach_port_t *address)
{
  error_t err;
  struct node *np;
  
  /* Make sure this is a socket */
  if (!cred)
    return EOPNOTSUPP;
  np = cred->po->np;
  
 retry:
  mutex_lock (&np->lock);
  if ((np->dn_stat.st_mode & S_IFMT) != S_IFSOCK)
    {
      mutex_unlock (&np->lock);
      return EOPNOTSUPP;
    }
  err = diskfs_access (np, S_IREAD, cred);
  if (err)
    {
      mutex_unlock (&np->lock);
      return err;
    }

  if (np->sockaddr == MACH_PORT_NULL)
    {
      mach_port_t server;
      mach_port_t sockaddr;

      mutex_unlock (&np->lock);

      /* Fetch a port to the PF_LOCAL server, caching it. */

      spin_lock (&pflocalserverlock);
      if (pflocalserver == MACH_PORT_NULL)
	{
	  /* Find out who the PF_LOCAL server is. */
	  char buf[100];

	  spin_unlock (&pflocalserverlock);

	  /* Look it up */
	  sprintf (buf, "%s/%d", _SERVERS_SOCKET, PF_LOCAL);
	  server = path_lookup (buf, 0, 0);
	  if (server == MACH_PORT_NULL)
	    return EIEIO;

	  /* Set it unless someone is already here */
	  spin_lock (&pflocalserverlock);
	  if (pflocalserver != MACH_PORT_NULL)
	    mach_port_deallocate (mach_task_self (), server);
	  else
	    pflocalserver = server;
	  spin_unlock (&pflocalserverlock);
	  
	  goto retry;
	}
      server = pflocalserver;
      spin_unlock (&pflocalserverlock);
      
      /* Create an address for the node */
      err = socket_fabricate_address (server, &sockaddr);
      if (err)
	{
	  mutex_unlock (&np->lock);
	  return EIEIO;
	}

      mutex_lock (&np->lock);
      if (np->sockaddr != MACH_PORT_NULL)
	/* Someone beat us */
	mach_port_deallocate (mach_task_self (), sockaddr);
      else
	np->sockaddr = sockaddr;
    }      
  
  *address = np->sockaddr;
  mutex_unlock (&np->lock);
  return 0;
}