diff options
author | Miles Bader <miles@gnu.org> | 1996-03-29 00:19:57 +0000 |
---|---|---|
committer | Miles Bader <miles@gnu.org> | 1996-03-29 00:19:57 +0000 |
commit | 306c2ec44e9439982303eb082ec17a21f7527397 (patch) | |
tree | 5a3023fb4e6367db101eed50d7d76e48fd01a619 /libshouldbeinlibc | |
parent | 22b07fa4ecebdfd103d9aa0030b92dae4db3b620 (diff) |
Initial revision
Diffstat (limited to 'libshouldbeinlibc')
-rw-r--r-- | libshouldbeinlibc/portinfo.c | 156 | ||||
-rw-r--r-- | libshouldbeinlibc/portinfo.h | 58 | ||||
-rw-r--r-- | libshouldbeinlibc/portxlate.c | 178 | ||||
-rw-r--r-- | libshouldbeinlibc/portxlate.h | 67 | ||||
-rw-r--r-- | libshouldbeinlibc/xportinfo.c | 67 |
5 files changed, 526 insertions, 0 deletions
diff --git a/libshouldbeinlibc/portinfo.c b/libshouldbeinlibc/portinfo.c new file mode 100644 index 00000000..c4a3e4b6 --- /dev/null +++ b/libshouldbeinlibc/portinfo.c @@ -0,0 +1,156 @@ +/* Print information about a task's ports + + Copyright (C) 1996 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 "portinfo.h" + +/* Prints info about NAME in TASK to STREAM, in a way described by the flags + in SHOW. If TYPE is non-zero, it should be what mach_port_type returns + for NAME. */ +error_t +print_port_info (mach_port_t name, mach_port_type_t type, task_t task, + unsigned show, FILE *stream) +{ + int hex_names = (show & PORTINFO_HEX_NAMES); + int first = 1; + void comma () + { + if (first) + first = 0; + else + fprintf (stream, ", "); + } + void prefs (mach_port_right_t right) + { + mach_port_urefs_t refs; + error_t err = mach_port_get_refs (task, name, right, &refs); + if (! err) + fprintf (stream, " (refs: %u)", refs); + } + + if (type == 0) + { + error_t err = mach_port_type (task, name, &type); + if (err) + return err; + } + + fprintf (stream, hex_names ? "%#6x: " : "%6d: ", name); + + if (type & MACH_PORT_TYPE_RECEIVE) + { + comma (); + fprintf (stream, "receive"); + if (show & PORTINFO_DETAILS) + { + struct mach_port_status status; + error_t err = mach_port_get_receive_status (task, name, &status); + if (! err) + { + fprintf (stream, " ("); + if (status.mps_pset != MACH_PORT_NULL) + fprintf (stream, + hex_names ? "port-set: %#x, " : "port-set: %d, ", + status.mps_pset); + fprintf (stream, "seqno: %u", status.mps_seqno); + if (status.mps_mscount) + fprintf (stream, ", ms-count: %u", status.mps_mscount); + if (status.mps_qlimit != MACH_PORT_QLIMIT_DEFAULT) + fprintf (stream, ", qlimit: %u", status.mps_qlimit); + if (status.mps_msgcount) + fprintf (stream, ", msgs: %u", status.mps_msgcount); + fprintf (stream, "%s%s%s)", + status.mps_srights ? ", send-rights" : "", + status.mps_pdrequest ? ", pd-req" : "", + status.mps_nsrequest ? ", ns-req" : ""); + } + } + } + if (type & MACH_PORT_TYPE_SEND) + { + comma (); + fprintf (stream, "send"); + if (show & PORTINFO_DETAILS) + prefs (MACH_PORT_RIGHT_SEND); + } + if (type & MACH_PORT_TYPE_SEND_ONCE) + { + comma (); + fprintf (stream, "send-once"); + } + if (type & MACH_PORT_TYPE_DEAD_NAME) + { + comma (); + fprintf (stream, "dead-name"); + if (show & PORTINFO_DETAILS) + prefs (MACH_PORT_RIGHT_DEAD_NAME); + } + if (type & MACH_PORT_TYPE_PORT_SET) + { + comma (); + fprintf (stream, "port-set"); + if (show & PORTINFO_DETAILS) + { + mach_port_t *members = 0; + mach_msg_type_number_t members_len = 0, i; + error_t err = + mach_port_get_set_status (task, name, &members, &members_len); + if (! err) + if (members_len == 0) + fprintf (stream, " (empty)"); + else + { + fprintf (stream, hex_names ? " (%#x" : " (%u", members[0]); + for (i = 1; i < members_len; i++) + fprintf (stream, hex_names ? ", %#x" : ", %u", members[i]); + fprintf (stream, ")"); + vm_deallocate (mach_task_self (), (vm_address_t)members, + members_len * sizeof *members); + } + } + } + putc ('\n', stream); + + return 0; +} + +/* Prints info about every port in TASK that has a type in ONLY to STREAM. */ +error_t +print_task_print_task_ports_info (task_t task, mach_port_type_t only, + unsigned show, FILE *stream) +{ + mach_port_t *names = 0; + mach_port_type_t *types = 0; + mach_msg_type_number_t names_len = 0, types_len = 0, i; + error_t err = mach_port_names (task, &names, &names_len, &types, &types_len); + + if (err) + return err; + + for (i = 0; i < names_len; i++) + if (types[i] & only) + print_port_info (names[i], types[i], task, show, stream); + + vm_deallocate (mach_task_self (), + (vm_address_t)names, names_len * sizeof *names); + vm_deallocate (mach_task_self (), + (vm_address_t)types, types_len * sizeof *types); + + return 0; +} diff --git a/libshouldbeinlibc/portinfo.h b/libshouldbeinlibc/portinfo.h new file mode 100644 index 00000000..697495b8 --- /dev/null +++ b/libshouldbeinlibc/portinfo.h @@ -0,0 +1,58 @@ +/* Print information about a task's ports + + Copyright (C) 1996 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. */ + +#ifndef __PORTINFO_H__ +#define __PORTINFO_H__ + +#include <stdio.h> +#include <errno.h> +#include <mach.h> + +#include <portxlate.h> + +/* Flags describing what to show. */ +#define PORTINFO_DETAILS 0x1 +#define PORTINFO_MEMBERS 0x4 +#define PORTINFO_HEX_NAMES 0x8 + +/* Prints info about NAME in TASK to STREAM, in a way described by the flags + in SHOW. If TYPE is non-zero, it should be what mach_port_type returns + for NAME. */ +error_t print_port_info (mach_port_t name, mach_port_type_t type, task_t task, + unsigned show, FILE *stream); + +/* Prints info about every port in TASK that has a type in ONLY to STREAM. */ +error_t print_task_print_task_ports_info (task_t task, mach_port_type_t only, + unsigned show, FILE *stream); + +/* Prints info about NAME translated through X to STREAM, in a way described + by the flags in SHOW. If TYPE is non-zero, it should be what + mach_port_type returns for NAME in X->to_task. */ +error_t print_xlated_port_info (mach_port_t name, mach_port_type_t type, + struct port_name_xlator *x, + unsigned show, FILE *stream); + +/* Prints info about every port common to both tasks in X, but only if the + port in X->from_task has a type in ONLY, to STREAM. */ +error_t print_xlated_ports_info (struct port_name_xlator *x, + mach_port_type_t only, + unsigned show, FILE *stream); + +#endif /* __PORTINFO_H__ */ diff --git a/libshouldbeinlibc/portxlate.c b/libshouldbeinlibc/portxlate.c new file mode 100644 index 00000000..07b2fc03 --- /dev/null +++ b/libshouldbeinlibc/portxlate.c @@ -0,0 +1,178 @@ +/* Translate mach port names between two tasks + + Copyright (C) 1996 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 <malloc.h> + +#include "portxlate.h" + +/* Return a new port name translator translating names between FROM_TASK and + TO_TASK, in XLATOR, or an error. */ +error_t +port_name_xlator_create (mach_port_t from_task, mach_port_t to_task, + struct port_name_xlator **xlator) +{ + error_t err; + struct port_name_xlator *x = malloc (sizeof (struct port_name_xlator)); + + if (! x) + return ENOMEM; + + x->from_task = from_task; + x->to_task = to_task; + x->to_names = 0; + x->to_types = 0; + x->to_names_len = 0; + x->to_types_len = 0; + + /* Cache a list of names in TO_TASK. */ + err = mach_port_names (to_task, + &x->to_names, &x->to_names_len, + &x->to_types, &x->to_types_len); + + if (! err) + /* Make an array to hold ports from TO_TASK which have been translated + into our namespace. */ + { + x->ports = malloc (sizeof (mach_port_t) * x->to_names_len); + if (x->ports) + { + int i; + for (i = 0; i < x->to_names_len; i++) + x->ports[i] = MACH_PORT_NULL; + } + else + { + vm_deallocate (mach_task_self (), + (vm_address_t)x->to_names, + x->to_names_len * sizeof (mach_port_t)); + vm_deallocate (mach_task_self (), + (vm_address_t)x->to_types, + x->to_types_len * sizeof (mach_port_type_t)); + err = ENOMEM; + } + } + + if (err) + free (x); + else + *xlator = x; + + return err; +} + +/* Free the port name translator X and any resources it holds. */ +void +port_name_xlator_free (struct port_name_xlator *x) +{ + int i; + + for (i = 0; i < x->to_names_len; i++) + if (x->ports[i] != MACH_PORT_NULL) + mach_port_deallocate (mach_task_self (), x->ports[i]); + free (x->ports); + + vm_deallocate (mach_task_self (), + (vm_address_t)x->to_names, + x->to_names_len * sizeof (mach_port_t)); + vm_deallocate (mach_task_self (), + (vm_address_t)x->to_types, + x->to_types_len * sizeof (mach_port_type_t)); + + mach_port_deallocate (mach_task_self (), x->to_task); + mach_port_deallocate (mach_task_self (), x->from_task); + + free (x); +} + +/* Translate the port FROM between the tasks in X, returning the translated + name in TO, and the types of TO in TO_TYPE, or an error. If TYPE is + non-zero, it should be what mach_port_type returns for FROM. */ +error_t +port_name_xlator_xlate (struct port_name_xlator *x, + mach_port_t from, mach_port_type_t from_type, + mach_port_t *to, mach_port_type_t *to_type) +{ + error_t err; + mach_port_t port; + mach_msg_type_number_t i; + mach_port_type_t aquired_type; + mach_port_type_t valid_to_types; + + if (from_type == 0) + { + error_t err = mach_port_type (x->from_task, from, &from_type); + if (err) + return err; + } + + if (from_type & MACH_PORT_TYPE_RECEIVE) + valid_to_types = MACH_PORT_TYPE_SEND; + else if (from_type & MACH_PORT_TYPE_SEND) + valid_to_types = MACH_PORT_TYPE_SEND | MACH_PORT_TYPE_RECEIVE; + else + return EKERN_INVALID_RIGHT; + + /* Translate the name FROM, in FROM_TASK's namespace into our namespace. */ + err = + mach_port_extract_right (x->from_task, from, + ((from_type & MACH_PORT_TYPE_RECEIVE) + ? MACH_MSG_TYPE_MAKE_SEND + : MACH_MSG_TYPE_COPY_SEND), + &port, + &aquired_type); + + if (err) + return err; + + /* Look for likely candidates in TO_TASK's namespace to test against PORT. */ + for (i = 0; i < x->to_names_len; i++) + { + if (x->ports[i] == MACH_PORT_NULL && (x->to_types[i] & valid_to_types)) + /* Port I shows possibilities... */ + { + err = + mach_port_extract_right (x->to_task, + x->to_names[i], + ((x->to_types[i] & MACH_PORT_TYPE_RECEIVE) + ? MACH_MSG_TYPE_MAKE_SEND + : MACH_MSG_TYPE_COPY_SEND), + &x->ports[i], + &aquired_type); + if (err) + x->to_types[i] = 0; /* Don't try to fetch this port again. */ + } + + if (x->ports[i] == port) + /* We win! Port I in TO_TASK is the same as PORT. */ + break; + } + + mach_port_deallocate (mach_task_self (), port); + + if (i < x->to_names_len) + /* Port I is the right translation; return its name in TO_TASK. */ + { + *to = x->to_names[i]; + *to_type = x->to_types[i]; + return 0; + } + else + return EKERN_INVALID_NAME; +} diff --git a/libshouldbeinlibc/portxlate.h b/libshouldbeinlibc/portxlate.h new file mode 100644 index 00000000..13ddb148 --- /dev/null +++ b/libshouldbeinlibc/portxlate.h @@ -0,0 +1,67 @@ +/* Translate mach port names between two tasks + + Copyright (C) 1996 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. */ + +#ifndef __PORTXLATE_H__ +#define __PORTXLATE_H__ + +#include <errno.h> +#include <mach.h> + +/* A data structure specifying two tasks, and info used to translate port + names between them. */ +struct port_name_xlator +{ + /* The tasks between which we are translating port names. */ + mach_port_t from_task; + mach_port_t to_task; + + /* True if we're translating receive rights in FROM_TASK; otherwise, we're + translating send rights. */ + int from_is_receive; + + /* Arrays of port names and type masks from TO_TASK, fetched by + mach_port_names. These are vm_allocated. */ + mach_port_t *to_names; + mach_msg_type_number_t to_names_len; + mach_port_type_t *to_types; + mach_msg_type_number_t to_types_len; + + /* An array of rights in the current task to the ports in TO_NAMES/TO_TASK, + or MACH_PORT_NULL, indicating that none has been fetched yet. + This vector is malloced. */ + mach_port_t *ports; +}; + +/* Return a new port name translator translating names between FROM_TASK and + TO_TASK, in XLATOR, or an error. */ +error_t port_name_xlator_create (mach_port_t from_task, mach_port_t to_task, + struct port_name_xlator **xlator); + +/* Free the port name translator X and any resources it holds. */ +void port_name_xlator_free (struct port_name_xlator *x); + +/* Translate the port FROM between the tasks in X, returning the translated + name in TO, and the types of TO in TO_TYPE, or an error. If TYPE is + non-zero, it should be what mach_port_type returns for FROM. */ +error_t port_name_xlator_xlate (struct port_name_xlator *x, + mach_port_t from, mach_port_type_t from_type, + mach_port_t *to, mach_port_type_t *to_type); + +#endif /* __PORTXLATE_H__ */ diff --git a/libshouldbeinlibc/xportinfo.c b/libshouldbeinlibc/xportinfo.c new file mode 100644 index 00000000..0ad7da48 --- /dev/null +++ b/libshouldbeinlibc/xportinfo.c @@ -0,0 +1,67 @@ +/* Print information about a port, with the name translated between tasks + + Copyright (C) 1996 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 "portinfo.h" + +/* Prints info about NAME translated through X to STREAM, in a way described + by the flags in SHOW. If TYPE is non-zero, it should be what + mach_port_type returns for NAME in X->to_task. */ +error_t +print_xlated_port_info (mach_port_t name, mach_port_type_t type, + struct port_name_xlator *x, + unsigned show, FILE *stream) +{ + mach_port_t old_name = name; + error_t err = port_name_xlator_xlate (x, name, type, &name, &type); + if (! err) + { + fprintf (stream, (show & PORTINFO_HEX_NAMES) ? "%#6x => " : "%6d => ", + old_name); + err = print_port_info (name, type, x->to_task, show, stream); + } + return err; +} + +/* Prints info about every port common to both tasks in X, but only if the + port in X->from_task has a type in ONLY, to STREAM. */ +error_t +print_xlated_ports_info (struct port_name_xlator *x, mach_port_type_t only, + unsigned show, FILE *stream) +{ + mach_port_t *names = 0; + mach_port_type_t *types = 0; + mach_msg_type_number_t names_len = 0, types_len = 0, i; + error_t err = + mach_port_names (x->from_task, &names, &names_len, &types, &types_len); + + if (err) + return err; + + for (i = 0; i < names_len; i++) + if (types[i] & only) + print_xlated_port_info (names[i], types[i], x, show, stream); + + vm_deallocate (mach_task_self (), + (vm_address_t)names, names_len * sizeof *names); + vm_deallocate (mach_task_self (), + (vm_address_t)types, types_len * sizeof *types); + + return 0; +} |