summaryrefslogtreecommitdiff
path: root/login/utmp.c
blob: f366d44b779f770415f58573413db7ce2b4adae4 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* Login record (utmp) server.

   Copyright (C) 1995, 1999 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. */

/* This program maintains an translator node that represents a particular
   login session and answers queries about that session's status.  The PID of
   the session's root process is given, and the session will be considered
   defunct if this process goes away.  */

#include <hurd.h>
#include <hurd/ports.h>
#include <hurd/trivfs.h>
#include <hurd/paths.h>
#include <sys/socket.h>
#include <hurd/socket.h>
#include <hurd/fsys.h>
#include <mach/notify.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>

/* ---------------------------------------------------------------- */
/* Global state.  */

/* A string describing the user's location.  */
static char *location = NULL;

/* The login collection we're for.  */
static int login_collection = 0;

/* A string containing a list of devices the user is using for this login
   session, separated by '\0'.  */
static char *devices = "";
/* The total length of DEVICES.  */
static int devices_len = 0;
/* The amount of space allocated for DEVICES.  */
static int devices_alloced = 0;

/* ---------------------------------------------------------------- */

/* If NAME is in the global DEVICES vector, return a pointer to it in
   DEVICES, otherwise return NULL.  */
static char *
_find_device(char *name)
{
  char *dev = devices;
  int left = devices_len;

  while (left > 0)
    {
      if (strcmp(name, dev) == 0)
	return dev;
      else
	{
	  int skip = strlen (dev) + 1;
	  dev += skip;
	  left -= skip;
	}
    }

  return 0;
}

/* Add NAME to the global DEVICES vector, unless it's already there.  If a
   memory allocation error occurs, ENOMEM is returned, otherwise 0.  */
static error_t
add_device(char *name)
{
  int len = strlen(name) + 1;

  if (!_find_device(name))
    {
      if (len + devices_len > devices_alloced)
	{
	  int alloc = len + devices_len;
	  char *new_devs = realloc(devices, alloc);
	  if (new_devs == NULL)
	    return ENOMEM;

	  devices = new_devs;
	  devices_alloced = alloc;
	}

      strcpy(devices + devices_len, name);
      devices_len += len;
    }

  return 0;
}

/* Remove NAME from the global DEVICES vector, if it's there.  */
static void
remove_device(char *name)
{
  char *dev = _find_device(name);

  if (dev)
    {
      int len = strlen(name) + 1;
      int tail_len = devices_len - ((dev - devices) + len);
      if (tail_len > 0)
	bcopy(dev + len, dev, tail_len);
    }
}

/* Given that DEV points to a device name in the global DEVICES vector,
   return a pointer to the next one, or NULL if DEV was the last.  */
static char *
next_device(char *dev)
{
  int next_pos = (dev - devices) + strlen(dev) + 1;
  if (next_pos == devices_len)
    return NULL;
  else
    return devices + next_pos;
}

/* ---------------------------------------------------------------- */

/* Try and start the translator for CTL_PORT on NODE.  If successful, this
   call will not return until the translator is stopped; otherwise it returns
   an error code describing the reason why it couldn't start.  */
static error_t
start_translator(file_t node, fsys_t ctl_port)
{
  mach_port_t prev;
  error_t err =
    file_set_translator(node, 0, FS_TRANS_EXCL, 0, NULL, 0, ctl_port);

  if (err)
    return err;

  /* Set up a watch on the process we're interested in.  */
  err =
    mach_port_request_notification(mach_task_self(),
				   watched_process, MACH_NOTIFY_DEAD_NAME, 1,
				   node,
				   MACH_MSG_TYPE_MAKE_SEND_ONCE, &prev);
  if (prev)
    mach_port_deallocate(mach_task_self(), prev);

  /* Boogie. */
  ports_manage_port_operations_onethread();

  return 0;
}

/* Find an unoccupied (one with no active translator) filename starting with
   NAME_PFX, and start the translator for CTL_PORT on it.  If successful, this
   call will not return until the translator is stopped; otherwise it returns
   an error code describing the reason why it couldn't start.  When
   successful, this function sets UTMP_NODE_NAME to the name of the file we
   started the translator on.  */
static error_t
find_node_and_start_translator(char *name_pfx, fsys_t ctl_port)
{
  error_t err;
  int num = 0;

  do
    {
      file_t node;

      sprintf(utmp_node_name, "%s:%d", name_pfx, num++);

      node = file_name_lookup(utmp_node_name, O_CREAT | O_NOTRANS, 0666);
      if (node == MACH_PORT_NULL)
	err = errno;
      else
	err = start_translator(node, ctl_port);
    }
  while (err == EBUSY);

  return err;
}

/* ---------------------------------------------------------------- */

#define SHORT_OPTIONS "p"

static struct option options[] =
{
  {"pid", required_argument, 0, 'p'},
  { 0 }
}

int
main (int argc, char **argv)
{
  int opt;
  error_t err;
  fsys_t ctl_port;
  int pid = 0;
  process_t proc_server = getproc();

  while ((opt = getopt_long(argc, argv, SHORT_OPTIONS, options, 0)) != EOF)
    switch (opt)
      {
      case 'p':			/* Watch pid N */
	pid = atoi(optarg);
	if (pid == 0)
	  error(1, 0, "%s: Bad process id", optarg);
	break;
      default:
	usage(-1);
      }
	
  if (pid == 0)
    /* By default watch the top of the current login collection.  */
    {
      err = proc_getloginid(proc_server, getpid(), &pid);
      if (err)
	error(2, err, "Couldn't get current login collection");
    }

  /* Setup a watch on the process we're interested in.  */
  err = proc_pid2proc(proc_server, pid, &watched_process);
  if (err)
    error(3, err, "Couldn't get process port for watched process");

  _libports_initialize ();

  ctl_port = trivfs_handle_port(MACH_PORT_NULL, PT_CTL, PT_NODE);
  
}

/* Looks for the dead-name notification message that indicates the process
   we're watching for has died, and kills off ourselves if it sees it.  */
static int
notice_process_died_msg(mach_msg_header_t *inp, mach_msg_header_t *outp)
{
  if (inp->msgh_id == MACH_NOTIFY_DEAD_NAME)
    {
      mach_dead_name_notification_t *notify_msg =
	(mach_dead_name_notification_t *)inp;

      if (notify_msg->not_port == watched_process)
	/* This is what we've been waiting for!  Now we can die.  */
	die(0);
    }

  return 0;
}

/* ---------------------------------------------------------------- */

/* Copy the char array DATA, of length DATA_LEN, into the MIG return buffer
   pointed to by BUF, whose length is pointed to by BUF_LEN, reallocating BUF
   with vm_allocate if it isn't long enough.  BUF and BUF_LEN are updated to
   point to the final storage & real length.  If a vm allocation failure
   occurs, EKERN_NO_SPACE is returned, otherwise 0.  */
static error_t
return_data(char *data, int data_len, char **buf, int *buf_len)
{
  if (data_len > *buf_len)
    {
      *buf = mmap (0, data_len, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
      if (*buf == (char *) -1)
	return errno;
    }

  *buf_len = data_len;
  bcopy(data, buf, data_len);

  return 0;
}

/* ---------------------------------------------------------------- */
/* login RPC handlers.  */

/* Send an immediate message to the user.  */
error_t
S_login_message_user(file_t utmp, char *msg, int msg_len)
{
  error_t err;
#if 0
  struct trivfs_protid *cred = ports_check_port_type (sockfile, PT_NODE);
  int perms;
  
  if (!cred)
    return EOPNOTSUPP;
  
  err = file_check_access (cred->realnode, &perms);
  if (!err && !(perms & O_READ))
    err = EACCES;
#endif

  return 0;
}

/* Return a human-readable string describing the user's physical location.  */
error_t
S_login_get_location(file_t utmp, char **buf, unsigned *len)
{
  return return_data(location, strlen(location) + 1, buf, len);
}

/* Return how much time has passed since the user last used an input device. */
error_t
S_login_get_idle_time(file_t utmp, time_value_t *tv)
{
  tv->seconds = 0;
  tv->microseconds = 0;

  if (devices_len > 0)
    {
      char *dev;
      for (dev = devices; dev != NULL; dev = next_device(dev))
	{
	  struct stat stat;
	  if (stat(dev, &state) == 0
	      && (stat.st_atim.tv_sec < tv->seconds
		  || (stat.st_atim.tv_sec == tv->seconds
		      && stat.st_atim.tv_nsec / 1000 < tv->microseconds)))
	    {
	      tv->seconds = stat.st_atim.tv_sc;
	      tv->microseconds = stat.st_atim.tv_nsec / 1000;
	    }
	}
    }

  return 0;
}

/* Return a list of file names for input devices being used, separated by null
   characters.  This call is optional; clients should not depend on it.  */
error_t
S_login_get_input_devices(file_t utmp, char **buf, unsigned *len)
{
  return return_data(devices, devices_len, buf, len);
}

/* Return the process collection ID for the user's login collection. */
error_t
S_login_get_login_collection(file_t utmp, int *pid)
{
  *pid = login_collection;
  return 0;
}

/* ---------------------------------------------------------------- */
/* Trivfs hooks  */

/* Port types */
#define PT_CTL 0
#define PT_NODE 1
#define PT_PROC 2		/* Our process handle. */

int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid = 0;

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

int trivfs_allow_open = 0;

int trivfs_protid_porttypes[] = {PT_NODE};
int trivfs_cntl_porttypes[] = {PT_CTL};
int trivfs_protid_nporttypes = 1;
int trivfs_cntl_nporttypes = 1;

void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
}

error_t
trivfs_goaway (struct trivfs_control *fsys, int flags)
{
  die(0);
}

/* ---------------------------------------------------------------- */
/* Ports hooks  */

void (*ports_cleanroutines[])(void *) =
{
  [PT_CTL] = trivfs_clean_cntl,
  [PT_NODE] = trivfs_clean_protid,
};

int
ports_demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
{
  return notice_process_died_msg(inp, outp)
    || login_server(inp, outp) || trivfs_demuxer(inp, outp);
}

void
ports_notice_idle (int nhard, int nsoft)
{
  if (!nhard && !nsoft)
    die(0);
}

void
ports_no_live_ports ()
{
  die(0);
}

void
ports_no_hard_ports ()
{
  die(0);
}