aboutsummaryrefslogtreecommitdiff
path: root/libpager/pager-ro-port.c
blob: 713b042302066a62a5a51c8e18651d5987269b20 (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
/* Copyright (C) 2021 Free Software Foundation, Inc.

   Written by Sergey Bugaev <bugaevc@gmail.com>.

   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 "priv.h"
#include <mach/mach4.h>

mach_port_t
pager_get_ro_port (struct pager *p)
{
  error_t err = 0;;
  mach_port_t port = MACH_PORT_NULL;
  mach_port_t rw_port;
  vm_offset_t offset = 0;
  vm_offset_t start = 0;
  vm_size_t len = ~0;

  pthread_mutex_lock (&p->interlock);

  if (!MACH_PORT_VALID (p->ro_proxy))
    {
      rw_port = ports_get_send_right (p);
      if (!MACH_PORT_VALID (rw_port))
        {
          err = errno;
          goto out;
        }

      err = memory_object_create_proxy (mach_task_self (),
                                        VM_PROT_READ | VM_PROT_EXECUTE,
                                        &rw_port, 1,
                                        &offset, 1, &start, 1, &len, 1,
                                        &p->ro_proxy);

      mach_port_deallocate (mach_task_self (), rw_port);

      if (err)
        goto out;
    }

  port = p->ro_proxy;

 out:
  pthread_mutex_unlock (&p->interlock);
  errno = err;
  return port;
}