1/*
2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#ifndef _KEXTUNLOAD_MAIN_H
24#define _KEXTUNLOAD_MAIN_H
25
26#include <IOKit/kext/OSKext.h>
27
28#include <getopt.h>
29#include <sysexits.h>
30
31#include "kext_tools_util.h"
32
33#pragma mark Basic Types & Constants
34/*******************************************************************************
35* Constants
36*******************************************************************************/
37enum {
38    kKextunloadExitOK = EX_OK,
39    kKextunloadExitNotFound = 1,
40    kKextunloadExitNotPrivileged = 2,
41    kKextunloadExitPartialFailure = 3,
42
43    // don't think we use it
44    kKextunloadExitUnspecified = 11,
45
46    // don't actually exit with this, it's just a sentinel value
47    kKextunloadExitHelp = 33
48};
49
50#pragma mark Command-line Option Definitions
51/*******************************************************************************
52* Command-line options. This data is used by getopt_long_only().
53*
54* Options common to all kext tools are in kext_tools_util.h.
55*******************************************************************************/
56
57#define kOptNameClassName           "class"
58#define kOptNamePersonalitiesOnly   "personalities-only"
59
60// really old option letter for "module", same as bundle id
61#define kOptClassName          'c'
62#define kOptModule             'm'
63#define kOptPersonalitiesOnly  'p'
64
65#define kOptChars  "b:c:hm:pqr:v"
66
67int longopt = 0;
68
69struct option sOptInfo[] = {
70    { kOptNameHelp,                  no_argument,        NULL, kOptHelp },
71
72    { kOptNameBundleIdentifier,      required_argument,  NULL, kOptBundleIdentifier },
73
74    { kOptNameClassName,             required_argument,  NULL, kOptClassName },
75    { kOptNamePersonalitiesOnly,     no_argument,        NULL, kOptPersonalitiesOnly },
76
77    { kOptNameQuiet,                 required_argument,  NULL, kOptQuiet },
78    { kOptNameVerbose,               optional_argument,  NULL, kOptVerbose },
79
80    { NULL, 0, NULL, 0 }  // sentinel to terminate list
81};
82
83
84#pragma mark Tool Args Structure
85/*******************************************************************************
86* Tool Args Structure
87*******************************************************************************/
88typedef struct {
89    Boolean           unloadPersonalities;  // -p
90    uint32_t          terminateOption;      // -p
91
92    CFMutableArrayRef kextURLs;             // args
93    CFMutableArrayRef kextBundleIDs;        // -b/-m -- array of C strings!
94    CFMutableArrayRef kextClassNames;       // -c    -- array of C strings!
95
96    CFMutableArrayRef kexts;                // any kexts created from URLs
97} KextunloadArgs;
98
99#pragma mark Function Prototypes
100/*******************************************************************************
101* Function Prototypes
102*******************************************************************************/
103ExitStatus   readArgs(
104    int              argc,
105    char * const *   argv,
106    KextunloadArgs * toolArgs);
107ExitStatus   checkArgs(KextunloadArgs * toolArgs);
108ExitStatus   createKextsIfNecessary(KextunloadArgs * toolArgs);
109
110ExitStatus terminateKextClasses(
111    KextunloadArgs * toolArgs,
112    Boolean * fatal);
113ExitStatus unloadKextsByIdentifier(
114    KextunloadArgs * toolArgs,
115    Boolean * fatal);
116ExitStatus unloadKextsByURL(
117    KextunloadArgs * toolArgs,
118    Boolean * fatal);
119ExitStatus unloadKextWithIdentifier(
120    CFStringRef      kextIdentifier,
121    KextunloadArgs * toolArgs,
122    Boolean        * fatal);
123
124ExitStatus   formatKernResult(kern_return_t kernResult);
125void         usage(UsageLevel usageLevel);
126
127#endif /* _KEXTUNLOAD_MAIN_H */
128