summaryrefslogtreecommitdiff
path: root/acpi/main.c
blob: 774eac08d818cd896b820ea14580c4bcb66fbc99 (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
/*
   Copyright (C) 2017 Free Software Foundation, Inc.

   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 the GNU Hurd.  If not, see <<a rel="nofollow" href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>>.
*/

/* Translator initialization and demuxing */

#include <stdio.h>
#include <error.h>
#include <fcntl.h>
#include <version.h>
#include <argp.h>
#include <hurd/netfs.h>
#include <hurd/fsys.h>
#include <hurd/startup.h>
#include <hurd/paths.h>

#include "acpi_S.h"
#include "startup_notify_S.h"
#include "libnetfs/io_S.h"
#include "libnetfs/fs_S.h"
#include "libports/notify_S.h"
#include "libnetfs/fsys_S.h"
#include "libports/interrupt_S.h"
#include "libnetfs/ifsock_S.h"
#include <device/device.h>
#include <pthread.h>
#include <acpi/acpi_init.h>
#include <acpifs.h>

/* Libnetfs stuff */
int netfs_maxsymlinks = 0;
char *netfs_server_name = "acpi";
char *netfs_server_version = HURD_VERSION;

volatile struct mapped_time_value *acpifs_maptime;

struct acpifs *fs;

static mach_port_t acpi_control_port;

struct port_class *acpi_shutdown_notify_class;

error_t
S_startup_dosync (mach_port_t notify_port)
{
  /* Nothing to sync */
  return 0;
}

static void
essential_task (void)
{
  mach_port_t host, startup;
  error_t err;

  err = get_privileged_ports (&host, 0);
  if (err)
    {
      mach_print ("WARNING: acpi cannot register as essential task\n");
      return;
    }
  startup = file_name_lookup (_SERVERS_STARTUP, 0, 0);
  if (startup == MACH_PORT_NULL)
    {
      mach_print ("WARNING: acpi cannot register as essential task\n");
      mach_port_deallocate (mach_task_self (), host);
      return;
    }
  startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL,
                          program_invocation_short_name, host);
  mach_port_deallocate (mach_task_self (), startup);
  mach_port_deallocate (mach_task_self (), host);
}

static void
arrange_shutdown_notification (void)
{
  error_t err;
  mach_port_t initport, notify;
  struct port_info *pi;

  acpi_shutdown_notify_class = ports_create_class (0, 0);

  /* Arrange to get notified when the system goes down */
  err = ports_create_port (acpi_shutdown_notify_class, netfs_port_bucket,
                           sizeof (struct port_info), &pi);
  if (err)
    return;

  initport = file_name_lookup (_SERVERS_STARTUP, 0, 0);
  if (initport == MACH_PORT_NULL)
    {
      mach_print ("WARNING: acpi not registered for shutdown\n");
      return;
    }

  notify = ports_get_send_right (pi);
  ports_port_deref (pi);
  startup_request_notification (initport, notify,
                                MACH_MSG_TYPE_MAKE_SEND,
                                program_invocation_short_name);
  mach_port_deallocate (mach_task_self (), notify);
  mach_port_deallocate (mach_task_self (), initport);
}

int
netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
{
  mig_routine_t routine;

  if ((routine = netfs_io_server_routine (inp)) ||
      (routine = netfs_fs_server_routine (inp)) ||
      (routine = ports_notify_server_routine (inp)) ||
      (routine = netfs_fsys_server_routine (inp)) ||
      (routine = ports_interrupt_server_routine (inp)) ||
      (routine = netfs_ifsock_server_routine (inp)) ||
      (routine = acpi_server_routine (inp)) ||
      (routine = startup_notify_server_routine (inp)))
    {
      (*routine) (inp, outp);
      return TRUE;
    }
  else
    return FALSE;
}

static mach_port_t
acpifs_startup(mach_port_t bootstrap, int flags)
{
  error_t err;
  mach_port_t realnode;
  struct port_info *newpi;

  err = ports_create_port (netfs_control_class, netfs_port_bucket,
			     sizeof (struct port_info), &newpi);
  if (err)
    error (11, err, "Translator startup failure: acpifs_startup");

  acpi_control_port = ports_get_send_right (newpi);

  err = fsys_startup (bootstrap, flags, acpi_control_port, MACH_MSG_TYPE_COPY_SEND,
                          &realnode);
  assert_perror_backtrace (err);

  essential_task();

  arrange_shutdown_notification();

  return realnode;
}

int
main (int argc, char **argv)
{
  error_t err;
  mach_port_t bootstrap;
  file_t underlying_node = MACH_PORT_NULL;

  /* Parse options */
  alloc_file_system (&fs);
  argp_parse (netfs_runtime_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");

  /* Initialize netfs and start the translator. */
  netfs_init ();

  err = maptime_map (0, 0, &acpifs_maptime);
  if (err)
    err = maptime_map (1, 0, &acpifs_maptime);
  if (err)
    error (1, err, "mapping time");

  underlying_node = netfs_startup (bootstrap, O_READ);

  /* Create the root node first */
  err = init_root_node (underlying_node);
  if (err)
    error (1, err, "creating the root node");

  err = init_file_system (fs);
  if (err)
    error (1, err, "creating the ACPI filesystem");

  /* Create the filesystem tree */
  err = create_fs_tree (fs);
  if (err)
    error (1, err, "creating the ACPI filesystem tree");

  /* Set permissions */
  err = fs_set_permissions (fs);
  if (err)
    error (1, err, "setting permissions");

  /* Enable ACPI mode of machine */
  acpi_init ();

  acpifs_startup (bootstrap, O_READ);

  netfs_server_loop (); /* Never returns.  */

  return 0;
}