2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <dispatch/dispatch.h>
29 #include "SecTranslocateShared.hpp"
30 #include "SecTranslocateServer.hpp"
31 #include "SecTranslocateUtilities.hpp"
32 #include "SecTranslocateDANotification.hpp"
33 #include "SecTranslocateXPCServer.hpp"
34 #include "SecTranslocateLSNotification.hpp"
35 #undef check //The CoreServices code pulls in a check macro that we don't want
37 #include <security_utilities/unix++.h>
38 #include <security_utilities/logging.h>
42 using namespace Security::UnixPlusPlus
;
44 namespace SecTranslocate
{
48 /* Try to cleanup every 12 hrs */
49 #define TRANSLOCATION_CLEANUP_INTERVAL 12ULL * 60ULL * 60ULL * NSEC_PER_SEC
50 #define TRANSLOCATION_CLEANUP_LEEWAY TRANSLOCATION_CLEANUP_INTERVAL/2ULL
52 /* Initialize a dispatch queue to serialize operations */
53 TranslocatorServer::TranslocatorServer(dispatch_queue_t q
):syncQ(q
), da(q
), ls(q
),xpc(q
)
57 Syslog::critical("SecTranslocate: TranslocatorServer failed to create the dispatch queue");
58 UnixError::throwMe(ENOMEM
);
60 dispatch_retain(syncQ
);
62 setupPeriodicCleanup();
64 Syslog::warning("SecTranslocate: Server started");
67 /* Destroy the dispatch queue and listeners when they are no longer needed */
68 TranslocatorServer::~TranslocatorServer()
72 dispatch_release(syncQ
);
77 dispatch_source_cancel(cleanupTimer
);
82 // This is intended for use by the host process of the server if necessary
83 // Create a translocation for original path if appropriate
84 string
TranslocatorServer::translocatePathForUser(const TranslocationPath
&originalPath
, const string
&destPath
)
86 __block string newPath
;
87 __block exception_ptr
exception(0);
89 dispatch_sync(syncQ
, ^{
92 newPath
= Security::SecTranslocate::translocatePathForUser(originalPath
,destPath
);
96 exception
= current_exception();
101 rethrow_exception(exception
);
106 string
TranslocatorServer::translocatePathForUser(const GenericTranslocationPath
&originalPath
, const string
&destPath
)
108 __block string newPath
;
109 __block exception_ptr
exception(0);
111 dispatch_sync(syncQ
, ^{
114 newPath
= Security::SecTranslocate::translocatePathForUser(originalPath
,destPath
);
118 exception
= current_exception();
123 rethrow_exception(exception
);
128 // This is intended for use by the host process of the server if necessary
129 // Destroy the translocation mount at translocatedPath if allowed
130 bool TranslocatorServer::destroyTranslocatedPathForUser(const string
&translocatedPath
)
132 __block
bool result
= false;
133 __block exception_ptr
exception(0);
134 dispatch_sync(syncQ
, ^{
137 result
= Security::SecTranslocate::destroyTranslocatedPathForUser(translocatedPath
);
141 exception
= current_exception();
146 rethrow_exception(exception
);
151 void TranslocatorServer::appLaunchCheckin(pid_t pid
)
153 //This is thrown on the queue as an async task in the call so don't need to do anything extra.
157 void TranslocatorServer::setupPeriodicCleanup()
159 cleanupTimer
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, 0, syncQ
);
161 dispatch_time_t when
= dispatch_time(DISPATCH_TIME_NOW
, TRANSLOCATION_CLEANUP_INTERVAL
);
162 dispatch_source_set_timer(cleanupTimer
, when
, TRANSLOCATION_CLEANUP_INTERVAL
, TRANSLOCATION_CLEANUP_LEEWAY
);
164 dispatch_source_set_cancel_handler(cleanupTimer
, ^{
165 dispatch_release(cleanupTimer
);
168 dispatch_source_set_event_handler(cleanupTimer
, ^{
171 Syslog::notice("SecTranslocate: attempting to cleanup unused translocation points");
172 tryToDestroyUnusedTranslocationMounts();
174 catch (Security::UnixError err
)
176 int error
= err
.unixError();
177 Syslog::error("SecTranslocate: got unix error[ %d : %s ] while trying to cleanup translocation points.",error
, strerror(error
));
181 Syslog::error("SecTranslocate: unknown error while trying to cleanup translocation points.");
185 dispatch_resume(cleanupTimer
);
188 } //namespace SecTranslocate
189 } //namespace SecTranslocate