1/*
2 * periodic-wrapper.c
3 * Copyright (c) 2012-2013 Apple Inc. All rights reserved.
4 */
5
6#include <xpc/xpc.h>
7#include <xpc/private.h>
8#include <asl.h>
9#include <vproc_priv.h>
10
11extern char **environ;
12
13int
14main(int argc, char *argv[])
15{
16	int64_t is_managed;
17	char activity_name[32];
18
19	xpc_track_activity();
20
21	is_managed = 0;
22	(void)vproc_swap_integer(NULL, VPROC_GSK_IS_MANAGED, NULL, &is_managed);
23	if (!is_managed) {
24		exit(1);
25	}
26
27	if (argc != 2) {
28		exit(1);
29	}
30
31	if (strcmp(argv[1], "daily") == 0) {
32	} else if (strcmp(argv[1], "weekly") == 0) {
33	} else if (strcmp(argv[1], "monthly") == 0) {
34	} else {
35		exit(1);
36	}
37
38	snprintf(activity_name, sizeof(activity_name), "com.apple.periodic-%s", argv[1]);
39
40	xpc_activity_register(activity_name, XPC_ACTIVITY_CHECK_IN, ^(xpc_activity_t activity) {
41		xpc_activity_state_t state;
42
43		state = xpc_activity_get_state(activity);
44		if (state == XPC_ACTIVITY_STATE_RUN) {
45			pid_t pid;
46			int status;
47			bool success = false;
48
49			asl_log(NULL, NULL, ASL_LEVEL_NOTICE, "Running %s periodic task.", argv[1]);
50
51			const char *args[] = { "periodic", argv[1], NULL };
52			if (posix_spawn(&pid, "/usr/sbin/periodic", NULL, NULL, (char * const *)args, environ) == 0) {
53				if (waitpid(pid, &status, 0) != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
54					success = true;
55				}
56			}
57
58			if (!success) {
59				asl_log(NULL, NULL, ASL_LEVEL_NOTICE, "Error running %s periodic task.", argv[1]);
60			}
61		}
62	});
63
64	dispatch_main();
65}
66