Skip to content

Commit

Permalink
Add dumpjpcategory (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfugil authored Aug 12, 2024
1 parent 7528bdc commit 15664be
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DESTDIR ?=
SRC := attach.c blame.c bootstrap.c enable.c env.c error.c examine.c kickstart.c
SRC += kill.c launchctl.c limit.c list.c load.c manager.c plist.c print.c reboot.c
SRC += remove.c runstats.c start_stop.c userswitch.c version.c xpc_helper.c
SRC += dumpjpcategory.c

ifeq ($(DEBUG),1)
CFLAGS += -O0 -g -fsanitize=address,undefined -fno-omit-frame-pointer
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
1. [x] examine
1. [x] config
1. [x] dumpstate
1. [x] dumpjpcategory
1. [x] reboot
1. [x] userswitch
1. [x] load
Expand Down
70 changes: 70 additions & 0 deletions dumpjpcategory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*-
* SPDX-License-Identifier: BSD 2-Clause License
*
* Copyright (c) 2024 Procursus Team <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <spawn.h>
#include <stdio.h>

#include <xpc/xpc.h>
#include "xpc_private.h"

#include "launchctl.h"

int
dumpjpcategory_cmd(xpc_object_t *msg, int argc, char **argv, char **envp, char **apple)
{
xpc_object_t dict, reply;
dict = xpc_dictionary_create(NULL, NULL, 0);
*msg = dict;
vm_address_t addr;
vm_size_t sz = 0x100000;

launchctl_setup_xpc_dict_for_service_name("system", dict, NULL);
xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO);

if (__builtin_available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)) {
addr = launchctl_create_shmem(dict, sz);
} else {
xpc_dictionary_set_fd(dict, "fd", STDOUT_FILENO);
}

int retval = launchctl_send_xpc_to_launchd(XPC_ROUTINE_DUMPJPCATEGORY, dict, &reply);

if (retval == ENOTSUP) {
fprintf(stderr, "Dump jetsamproperties category is not supported on this platform.\n");
}

if (__builtin_available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)) {
if (retval == 0)
launchctl_print_shmem(reply, addr, sz, stdout);
vm_deallocate(mach_task_self(), addr, sz);
}

return retval;
}
1 change: 1 addition & 0 deletions launchctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static const struct {
{ "examine", "Runs the specified analysis tool against launchd in a non-reentrant manner.", "[<tool> [arg0, arg1, ... , @PID, ...]]", examine_cmd },
{ "config", "Modifies persistent configuration parameters for launchd domains.", NULL, config_cmd },
{ "dumpstate", "Dumps launchd state to stdout.", NULL, dumpstate_cmd },
{ "dumpjpcategory", "Dumps the jetsam properties category for all services.", NULL, dumpjpcategory_cmd },
{ "reboot", "Initiates a system reboot of the specified type.", "[system|halt|obliterate|userspace] [-s]", reboot_cmd },
{ "userswitch", "Initiates a user switch.", "<old-uid> <new-uid>", userswitch_cmd },
{ "load", "Bootstraps a service or directory of services.", "<service-path, service-path2, ...>", load_cmd },
Expand Down
3 changes: 3 additions & 0 deletions launchctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ cmd_main userswitch_cmd;
// limit.c
cmd_main limit_cmd;

// dumpjpcategory.c
cmd_main dumpjpcategory_cmd;

void launchctl_xpc_object_print(xpc_object_t, const char *name, int level);
int launchctl_send_xpc_to_launchd(uint64_t routine, xpc_object_t msg, xpc_object_t *reply);
void launchctl_setup_xpc_dict(xpc_object_t dict);
Expand Down
1 change: 1 addition & 0 deletions xpc_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum {
XPC_ROUTINE_EXAMINE = 826,
XPC_ROUTINE_PRINT = 828,
XPC_ROUTINE_DUMPSTATE = 834,
XPC_ROUTINE_DUMPJPCATEGORY = 837
};

XPC_DECL(xpc_pipe);
Expand Down

0 comments on commit 15664be

Please sign in to comment.