]> git.saurik.com Git - apple/launchd.git/blame - launchd/src/launchd_runtime.h
launchd-329.3.1.tar.gz
[apple/launchd.git] / launchd / src / launchd_runtime.h
CommitLineData
5b0a4722
A
1/*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20#ifndef __LAUNCHD_RUNTIME_H__
21#define __LAUNCHD_RUNTIME_H__
22
23#include <mach/mach.h>
24#include <sys/types.h>
25#include <bsm/libbsm.h>
26#include <stdbool.h>
ddbbfbc1
A
27#include <stdint.h>
28#include <float.h>
5b0a4722
A
29#include <syslog.h>
30
31#include "launchd_runtime_kill.h"
ddbbfbc1
A
32#include "launchd_ktrace.h"
33
34#if 0
35
36/* I need to do more testing of these macros */
37
38#define min_of_type(x) \
39 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long double), LDBL_MIN, \
40 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), double), DBL_MIN, \
41 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), float), FLT_MIN, \
42 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char), 0, \
43 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), signed char), INT8_MIN, \
44 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), short), INT16_MIN, \
45 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int), INT32_MIN, \
46 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long), (__builtin_choose_expr(sizeof(x) == 4, INT32_MIN, INT64_MIN)), \
47 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long long), INT64_MIN, \
48 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned char), 0, \
49 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned short), 0, \
50 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned int), 0, \
51 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned long), 0, \
52 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned long long), 0, \
53 (void)0))))))))))))))
54
55#define max_of_type(x) \
56 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long double), LDBL_MAX, \
57 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), double), DBL_MAX, \
58 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), float), FLT_MAX, \
59 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char), UINT8_MAX, \
60 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), signed char), INT8_MAX, \
61 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), short), INT16_MIN, \
62 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int), INT32_MAX, \
63 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long), (__builtin_choose_expr(sizeof(x) == 4, INT32_MAX, INT64_MAX)), \
64 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), long long), INT64_MAX, \
65 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned char), UINT8_MAX, \
66 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned short), UINT16_MAX, \
67 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned int), UINT32_MAX, \
68 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned long), (__builtin_choose_expr(sizeof(x) == 4, UINT32_MAX, UINT64_MAX)), \
69 __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), unsigned long long), UINT64_MAX, \
70 (void)0))))))))))))))
71
72#endif
73
74#define likely(x) __builtin_expect((bool)(x), true)
75#define unlikely(x) __builtin_expect((bool)(x), false)
5b0a4722
A
76
77struct ldcred {
78 uid_t euid;
79 uid_t uid;
80 gid_t egid;
81 gid_t gid;
82 pid_t pid;
5b0a4722
A
83};
84
85/*
86 * Use launchd_assumes() when we can recover, even if it means we leak or limp along.
87 *
88 * Use launchd_assert() for core initialization routines.
89 */
90#define launchd_assumes(e) \
ddbbfbc1 91 (unlikely(!(e)) ? _log_launchd_bug(__rcs_file_version__, __FILE__, __LINE__, #e), false : true)
5b0a4722
A
92
93#define launchd_assert(e) if (__builtin_constant_p(e)) { char __compile_time_assert__[e ? 1 : -1] __attribute__((unused)); } else if (!launchd_assumes(e)) { abort(); }
94
95void _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test);
96
97typedef void (*kq_callback)(void *, struct kevent *);
98typedef boolean_t (*mig_callback)(mach_msg_header_t *, mach_msg_header_t *);
99typedef void (*timeout_callback)(void);
100
ddbbfbc1
A
101extern bool pid1_magic;
102extern bool low_level_debug;
103extern char g_username[128];
104extern char g_my_label[128];
105extern bool g_shutdown_debugging;
106extern bool g_verbose_boot;
107extern bool g_use_gmalloc;
108extern bool g_log_per_user_shutdown;
109extern bool g_log_strict_usage;
110extern bool g_embedded_shutdown_log;
587e987e
A
111extern bool g_runtime_busy_time;
112extern size_t runtime_busy_cnt;
ddbbfbc1
A
113extern int32_t g_sync_frequency;
114extern pid_t g_wsp;
115
fe044cc9 116mach_port_t runtime_get_kernel_port(void);
ddbbfbc1 117extern boolean_t launchd_internal_demux(mach_msg_header_t *Request, mach_msg_header_t *Reply);
5b0a4722
A
118
119void runtime_add_ref(void);
120void runtime_del_ref(void);
ddbbfbc1
A
121void runtime_add_weak_ref(void);
122void runtime_del_weak_ref(void);
587e987e
A
123void runtime_install_timer(void);
124void runtime_remove_timer(void);
5b0a4722
A
125
126void launchd_runtime_init(void);
127void launchd_runtime_init2(void);
128void launchd_runtime(void) __attribute__((noreturn));
129
ddbbfbc1
A
130void launchd_log_vm_stats(void);
131
5b0a4722
A
132int runtime_close(int fd);
133int runtime_fsync(int fd);
134
135#define RUNTIME_ADVISABLE_IDLE_TIMEOUT 30
136
137void runtime_set_timeout(timeout_callback to_cb, unsigned int sec);
138kern_return_t runtime_add_mport(mach_port_t name, mig_callback demux, mach_msg_size_t msg_size);
139kern_return_t runtime_remove_mport(mach_port_t name);
ddbbfbc1 140struct ldcred *runtime_get_caller_creds(void);
5b0a4722
A
141
142const char *signal_to_C_name(unsigned int sig);
143const char *reboot_flags_to_C_names(unsigned int flags);
144const char *proc_flags_to_C_names(unsigned int flags);
145
5b0a4722
A
146int kevent_bulk_mod(struct kevent *kev, size_t kev_cnt);
147int kevent_mod(uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata);
ddbbfbc1 148void log_kevent_struct(int level, struct kevent *kev_base, int indx);
5b0a4722
A
149
150pid_t runtime_fork(mach_port_t bsport);
151
ddbbfbc1
A
152mach_msg_return_t launchd_exc_runtime_once(mach_port_t port, mach_msg_size_t rcv_msg_size, mach_msg_size_t send_msg_size, mig_reply_error_t *bufRequest, mig_reply_error_t *bufReply, mach_msg_timeout_t to);
153
5b0a4722
A
154kern_return_t runtime_log_forward(uid_t forward_uid, gid_t forward_gid, vm_offset_t inval, mach_msg_type_number_t invalCnt);
155kern_return_t runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt);
156
ddbbfbc1
A
157#define LOG_APPLEONLY 0x4141504c /* AAPL in hex */
158#define LOG_SCOLDING 0x3030493b
159#define LOG_CONSOLE (1 << 31)
5b0a4722
A
160
161struct runtime_syslog_attr {
162 const char *from_name;
163 const char *about_name;
164 const char *session_name;
165 int priority;
166 uid_t from_uid;
167 pid_t from_pid;
168 pid_t about_pid;
169};
170
171int runtime_setlogmask(int maskpri);
172void runtime_closelog(void);
173void runtime_syslog(int pri, const char *message, ...) __attribute__((format(printf, 2, 3)));
174void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) __attribute__((format(printf, 2, 0)));
ddbbfbc1 175void runtime_log_push(void);
5b0a4722 176
ddbbfbc1
A
177int64_t runtime_get_wall_time(void) __attribute__((warn_unused_result));
178uint64_t runtime_get_opaque_time(void) __attribute__((warn_unused_result));
179uint64_t runtime_get_opaque_time_of_event(void) __attribute__((pure, warn_unused_result));
180uint64_t runtime_opaque_time_to_nano(uint64_t o) __attribute__((const, warn_unused_result));
181uint64_t runtime_get_nanoseconds_since(uint64_t o) __attribute__((pure, warn_unused_result));
5b0a4722
A
182
183kern_return_t launchd_set_bport(mach_port_t name);
184kern_return_t launchd_get_bport(mach_port_t *name);
185kern_return_t launchd_mport_notify_req(mach_port_t name, mach_msg_id_t which);
186kern_return_t launchd_mport_notify_cancel(mach_port_t name, mach_msg_id_t which);
187kern_return_t launchd_mport_create_recv(mach_port_t *name);
188kern_return_t launchd_mport_deallocate(mach_port_t name);
189kern_return_t launchd_mport_make_send(mach_port_t name);
ddbbfbc1 190kern_return_t launchd_mport_copy_send(mach_port_t name);
5b0a4722
A
191kern_return_t launchd_mport_close_recv(mach_port_t name);
192
193#endif