summaryrefslogtreecommitdiff
path: root/trans/firmlink.c
blob: ca159ea59fc742b1968eac45564a9085a1289045 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* A translator for `firmlinks'

   Copyright (C) 1997 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <argp.h>
#include <error.h>

#include <hurd/trivfs.h>

#include <version.h>

const char *argp_program_version = STANDARD_HURD_VERSION (firmlink);

static const struct argp_option options[] =
{
  { 0 }
};

static const char args_doc[] = "TARGET";
static const char doc[] = "A translator for firmlinks"
"\vA firmlink is sort of half-way between a symbolic link and a hard link;"
"\n"
"\nLike a symbolic link, it is `by name', and contains no actual reference to"
" the target.  However, the lookup returns a node which will redirect parent"
" lookups so that attempts to find the cwd that go through the link will"
" reflect the link name, not the target name.  The target referenced by the"
" firmlink is looked up in the namespace of the translator, not the client.";

/* Link parameters.  */
static char *target = 0;	/* What we translate too.  */

static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  if (key == ARGP_KEY_ARG && state->arg_num == 0)
    target = arg;
  else if (key == ARGP_KEY_ARG || key == ARGP_KEY_NO_ARGS)
    argp_usage (state);
  else
    return ARGP_ERR_UNKNOWN;
  return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc };

void
main (int argc, char **argv)
{
  error_t err;
  mach_port_t bootstrap;
  struct trivfs_control *fsys;

  /* Parse our options...  */
  argp_parse (&argp, argc, argv, 0, 0, 0);

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap == MACH_PORT_NULL)
    error (1, 0, "Must be started as a translator");

  /* Reply to our parent */
  err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
  if (err)
    error (2, err, "Contacting parent");

  /* Launch. */
  ports_manage_port_operations_one_thread (fsys->pi.bucket, trivfs_demuxer, 0);

  exit (0);
}

static error_t
firmlink (mach_port_t parent, const char *target_name, int flags,
	  mach_port_t *link)
{
  error_t err;
  file_t authed_link;
  file_t target = file_name_lookup (target_name, flags & ~O_CREAT, 0);

  if (target == MACH_PORT_NULL)
    return errno;

  err = file_reparent (target, parent, &authed_link);
  mach_port_deallocate (mach_task_self (), target);
  mach_port_deallocate (mach_task_self (), parent);

  if (! err)
    {
      err = io_restrict_auth (authed_link, link, 0, 0, 0, 0);
      mach_port_deallocate (mach_task_self (), authed_link);
    }

  return err;
}

/* Trivfs hooks  */

int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid = 0;

int trivfs_support_read = 1;
int trivfs_support_write = 0;
int trivfs_support_exec = 0;

int trivfs_allow_open = O_READ;

static error_t
getroot (struct trivfs_control *cntl,
	 mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
	 mach_port_t dotdot,
	 uid_t *uids, u_int nuids, uid_t *gids, u_int ngids,
	 int flags,
	 retry_type *do_retry, char *retry_name,
	 mach_port_t *node, mach_msg_type_name_t *node_type)
{
  error_t err = firmlink (dotdot, target, flags, node);

  if (err == ENOENT)
    /* No target?  Act like a link.  */
    return EAGAIN;

  if (! err)
    {
      *node_type = MACH_MSG_TYPE_MOVE_SEND;
      *do_retry = FS_RETRY_REAUTH;
      retry_name[0] = '\0';
    }

  return err;
}

/* Called by trivfs_S_fsys_getroot before any other processing takes place;
   if the return value is EAGAIN, normal trivfs getroot processing continues,
   otherwise the rpc returns with that return value.  */
error_t (*trivfs_getroot_hook) () = getroot;

void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
  st->st_size = strlen (target);
  st->st_blocks = 0;
  st->st_mode &= ~S_IFMT;
  st->st_mode |= S_IFLNK;
}

error_t
trivfs_goaway (struct trivfs_control *cntl, int flags)
{
  error_t err;
  int force = (flags & FSYS_GOAWAY_FORCE);
  struct port_bucket *bucket = ((struct port_info *)cntl)->bucket;

  err = ports_inhibit_bucket_rpcs (bucket);
  if (err == EINTR || (err && !force))
    return err;

  if (ports_count_class (cntl->protid_class) > 0 && !force)
    /* Still some opens, and we're not being forced to go away, so don't.  */
    {
      ports_enable_class (cntl->protid_class);
      ports_resume_bucket_rpcs (bucket);
      return EBUSY;
    }

  exit (0);
}

/* We store the file offset in po->hook (ick!) */

/* Read data from an IO object.  If offset if -1, read from the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount desired to be read is in AMT.  */
error_t
trivfs_S_io_read (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  vm_address_t *data, mach_msg_type_number_t *data_len,
		  off_t offs, mach_msg_type_number_t amount)
{
  error_t err = 0;

  if (! cred)
    err = EOPNOTSUPP;
  else if (! (cred->po->openmodes & O_READ))
    err = EBADF;
  else
    {
      off_t max = strlen (target);
      off_t start = offs >= 0 ? offs : (off_t)cred->po->hook;
      if (start < 0)
	return EINVAL;
      if (start + amount > max)
	amount = max - start;
      if (amount > *data_len)
	err = vm_allocate (mach_task_self (), data, amount, 1);
      if (!err && amount > 0)
	{
	  memcpy ((char *)(*data + start), target, amount);
	  if (offs < 0)
	    cred->po->hook = (void *)(start + amount); /* Update PO offset.  */
	}
      *data_len = amount;
    }

  return err;
}

/* Tell how much data can be read from the object without blocking for
   a "long time" (this should be the same meaning of "long time" used
   by the nonblocking flag.  */
error_t
trivfs_S_io_readable (struct trivfs_protid *cred,
		      mach_port_t reply, mach_msg_type_name_t reply_type,
		      mach_msg_type_number_t *amount)
{
  if (! cred)
    return EOPNOTSUPP;
  else if (! (cred->po->openmodes & O_READ))
    return EBADF;
  else if ((off_t)cred->po->hook < 0)
    return EINVAL;
  else
    *amount = strlen (target) - (off_t)cred->po->hook;
  return 0;
}

/* Change current read/write offset */
error_t
trivfs_S_io_seek (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  off_t offset, int whence, off_t *new_offset)
{
  return EOPNOTSUPP;
}

/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
   Block until one of the indicated types of i/o can be done "quickly", and
   return the types that are then available.  ID_TAG is returned as passed; it
   is just for the convenience of the user in matching up reply messages with
   specific requests sent.  */
error_t
trivfs_S_io_select (struct trivfs_protid *cred,
		    mach_port_t reply, mach_msg_type_name_t reply_type,
		    int *type, int *tag)
{
  return EOPNOTSUPP;
}