summaryrefslogtreecommitdiff
path: root/libcons/vcons-open.c
blob: 7256b4c847e428c3a2c6fa550edf54c4cb1a7355 (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
/* vcons-open.c - Open a virtual console.
   Copyright (C) 2002 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   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 <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/fcntl.h>

#include <hurd.h>
#include <mach.h>

#include "cons.h"

/* Open the virtual console VCONS.  VCONS->cons is locked.  */
error_t
cons_vcons_open (vcons_t vcons)
{
  error_t err = 0;
  char *name;
  file_t vconsp = MACH_PORT_NULL;
  file_t file = MACH_PORT_NULL;
  int fd = -1;
  struct stat statbuf;
  mach_port_t notify = MACH_PORT_NULL;

  if (asprintf (&name, "%u", vcons->id) < 0)
    return err;

  /* Open the directory port of the virtual console.  */
  vconsp = file_name_lookup_under (vcons->cons->dirport, name,
				   O_DIRECTORY | O_RDONLY, 0);
  if (vconsp == MACH_PORT_NULL)
    {
      err = errno;
      goto err;
    }

  /* Within that directory, open the input node.  */
  file = file_name_lookup_under (vconsp, "input", O_WRONLY /* | O_NONBLOCK */, 0);
  if (file == MACH_PORT_NULL)
    err = errno;
  else
    {
      vcons->input = openport (file, O_WRONLY /* | O_NONBLOCK */);
      if (vcons->input < 0)
	err = errno;
      else
	/* openport() consumed the reference.  */
	file = MACH_PORT_NULL;
    }
  if (err)
    goto err;

  /* Within that directory, also open the display node.  */
  file = file_name_lookup_under (vconsp, "display", O_RDONLY, 0);
  if (file == MACH_PORT_NULL)
    err = errno;
  else
    {
      /* Acquire an additional reference for openport().  */
      err = mach_port_mod_refs (mach_task_self (), file,
				MACH_PORT_RIGHT_SEND, +1);
      if (err)
	goto err;
      fd = openport (file, O_RDONLY);
      if (fd < 0)
	err = errno;
    }
  if (err)
    goto err;

  /* Map the whole file.  */
  if (fstat (fd, &statbuf) < 0)
    {
      err = errno;
      goto err;
    }
  vcons->display_size = statbuf.st_size;
  vcons->display = mmap (0, vcons->display_size, PROT_READ, MAP_SHARED, fd, 0);
  if (vcons->display == MAP_FAILED)
    {
      err = errno;
      goto err;
    }

  if (vcons->display->magic != CONS_MAGIC
      || vcons->display->version >> CONS_VERSION_MAJ_SHIFT != 0)
    {
      err = EINVAL;
      goto err;
    }
  vcons->state.screen.width = vcons->display->screen.width;
  vcons->state.screen.height = vcons->display->screen.height;
  vcons->state.screen.lines = vcons->display->screen.lines;
  vcons->state.screen.matrix = ((wchar_t *) vcons->display)
    + vcons->display->screen.matrix;
  vcons->state.changes.length = vcons->display->changes.length;
  vcons->state.changes.buffer = ((uint32_t *) vcons->display)
    + vcons->display->changes.buffer;

  /* Set up the port we receive notification messages on.  */
  err = ports_create_port (cons_port_class, cons_port_bucket,
                           sizeof (*vcons->notify), &vcons->notify);
  if (err)
    goto err;
  vcons->notify->cons = NULL;
  vcons->notify->vcons = vcons;

  /* Request notification messages.  */
  notify = ports_get_right (vcons->notify);
  mach_port_set_qlimit (mach_task_self (), notify, 1);

  /* When this succeeds, we will immediately receive notification
     messages for this virtual console.  */
  err = file_notice_changes (file, notify, MACH_MSG_TYPE_MAKE_SEND);
  if (!err)
    goto out;

 err:
  if (vcons->input >= 0)
    {
      close (vcons->input);
      vcons->input = -1;
    }
  if (vcons->display != MAP_FAILED)
    {
      munmap (vcons->display, vcons->display_size);
      vcons->display = MAP_FAILED;
    }
  if (notify)
    {
      mach_port_deallocate (mach_task_self (), notify);
      vcons->notify = MACH_PORT_NULL;
    }
 out:
  if (fd > 0)
    close (fd);
  if (file != MACH_PORT_NULL)
    mach_port_deallocate (mach_task_self (), file);
  if (vconsp != MACH_PORT_NULL)
    mach_port_deallocate (mach_task_self (), vconsp);
  free (name);
  return err;
}