summaryrefslogtreecommitdiff
path: root/libnetfs/file-set-translator.c
blob: b107ccd53e5a8f100c8941c389566c7ca84f69fd (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
   Copyright (C) 1996, 1997, 1999, 2001 Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */

#include "netfs.h"
#include <hurd/paths.h>
#include <hurd/fsys.h>

error_t
netfs_S_file_set_translator (struct protid *user,
			     int passive_flags, int active_flags,
			     int killtrans_flags, char *passive,
			     mach_msg_type_number_t passivelen,
			     mach_port_t active)
{
  struct node *np;
  error_t err = 0;
  mach_port_t control;

  if (!user)
    return EOPNOTSUPP;

  if (!(passive_flags & FS_TRANS_SET) && !(active_flags & FS_TRANS_SET))
    return 0;

  if (passivelen && passive[passivelen - 1])
    return EINVAL;

  np = user->po->np;
  pthread_mutex_lock (&np->lock);

  if (active_flags & FS_TRANS_SET
      && ! (active_flags & FS_TRANS_ORPHAN))
    {
      /* Validate--user must be owner */
      err = netfs_validate_stat (np, user->user);
      if (err)
	goto out;

      err = fshelp_isowner (&np->nn_stat, user->user);
      if (err)
	goto out;

      err = fshelp_fetch_control (&np->transbox, &control);
      if (err)
	goto out;

      if (control != MACH_PORT_NULL
	  && (active_flags & FS_TRANS_EXCL) == 0)
	{
	  pthread_mutex_unlock (&np->lock);
	  err = fsys_goaway (control, killtrans_flags);
	  if (err && err != MIG_SERVER_DIED && err != MACH_SEND_INVALID_DEST)
	    return err;
	  err = 0;
	  pthread_mutex_lock (&np->lock);
	}
    }

  if ((passive_flags & FS_TRANS_SET)
      && (passive_flags & FS_TRANS_EXCL))
    {
      err = netfs_validate_stat (np, user->user);
      if (!err && (np->nn_stat.st_mode & S_IPTRANS))
	err = EBUSY;
      if (err)
	goto out;
    }

  if (active_flags & FS_TRANS_SET)
    {
      err = fshelp_set_active (&np->transbox, active,
			       active_flags & FS_TRANS_EXCL);
      if (err)
	goto out;
    }

  if (passive_flags & FS_TRANS_SET)
    {
      mode_t newmode = 0;
      if (!(passive_flags & FS_TRANS_FORCE))
	{
	  /* Short circuited translators */

	  if (!strcmp (passive, _HURD_SYMLINK))
	    newmode = S_IFLNK;
	  else if (!(strcmp (passive, _HURD_CHRDEV)))
	    newmode = S_IFCHR;
	  else if (!strcmp (passive, _HURD_BLKDEV))
	    newmode = S_IFBLK;
	  else if (!strcmp (passive, _HURD_FIFO))
	    newmode = S_IFIFO;
	  else if (!strcmp (passive, _HURD_IFSOCK))
	    newmode = S_IFSOCK;
	}

      switch (newmode)
	{
	  int major, minor;
	  char *arg;

	case S_IFBLK:
	case S_IFCHR:
	  /* Find the device number from the arguments
	     of the translator. */
	  arg = passive + strlen (passive) + 1;
	  assert (arg <= passive + passivelen);
	  if (arg == passive + passivelen)
	    {
	      pthread_mutex_unlock (&np->lock);
	      return EINVAL;
	    }
	  major = strtol (arg, 0, 0);

	  arg = arg + strlen (arg) + 1;
	  assert (arg < passive + passivelen);
	  if (arg == passive + passivelen)
	    {
	      pthread_mutex_unlock (&np->lock);
	      return EINVAL;
	    }
	  minor = strtol (arg, 0, 0);

	  err = netfs_attempt_mkdev (user->user, np,
				     newmode, makedev (major, minor));
	  if (err == EOPNOTSUPP)
	    goto fallback;
	  break;

	case S_IFLNK:
	  arg = passive + strlen (passive) + 1;
	  assert (arg <= passive + passivelen);
	  if (arg == passive + passivelen)
	    {
	      pthread_mutex_unlock (&np->lock);
	      return EINVAL;
	    }

	  err = netfs_attempt_mksymlink (user->user, np, arg);
	  if (err == EOPNOTSUPP)
	    goto fallback;
	  break;

	default:
	  err = netfs_validate_stat (np, user->user);
	  if (!err)
	    err = netfs_attempt_chmod (user->user, np,
				       ((np->nn_stat.st_mode & ~S_IFMT)
					| newmode));
	  if (err == EOPNOTSUPP)
	    goto fallback;
	  break;

	case 0:
	fallback:
	  err = netfs_set_translator (user->user, np,
				      passive, passivelen);
	  break;
	}
    }

 out:
  pthread_mutex_unlock (&np->lock);
  return err;
}