ActiveServices.java revision cff1bbf46ce79e370b80298522b0c2e23f3bdccc
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import java.io.FileDescriptor;
20import java.io.IOException;
21import java.io.PrintWriter;
22import java.io.StringWriter;
23import java.util.ArrayList;
24import java.util.HashSet;
25import java.util.Iterator;
26import java.util.List;
27
28import android.os.Build;
29import android.os.DeadObjectException;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.SystemProperties;
33import android.util.ArrayMap;
34import android.util.ArraySet;
35import com.android.internal.app.ProcessStats;
36import com.android.internal.os.BatteryStatsImpl;
37import com.android.internal.os.TransferPipe;
38import com.android.internal.util.FastPrintWriter;
39import com.android.server.am.ActivityManagerService.ItemMatcher;
40import com.android.server.am.ActivityManagerService.NeededUriGrants;
41
42import android.app.ActivityManager;
43import android.app.AppGlobals;
44import android.app.IApplicationThread;
45import android.app.IServiceConnection;
46import android.app.Notification;
47import android.app.PendingIntent;
48import android.app.Service;
49import android.content.ComponentName;
50import android.content.Context;
51import android.content.Intent;
52import android.content.pm.ApplicationInfo;
53import android.content.pm.PackageManager;
54import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
56import android.os.Binder;
57import android.os.IBinder;
58import android.os.Message;
59import android.os.Process;
60import android.os.RemoteException;
61import android.os.SystemClock;
62import android.os.UserHandle;
63import android.util.EventLog;
64import android.util.Slog;
65import android.util.SparseArray;
66import android.util.TimeUtils;
67
68public final class ActiveServices {
69    static final boolean DEBUG_SERVICE = ActivityManagerService.DEBUG_SERVICE;
70    static final boolean DEBUG_SERVICE_EXECUTING = ActivityManagerService.DEBUG_SERVICE_EXECUTING;
71    static final boolean DEBUG_DELAYED_SERVICE = ActivityManagerService.DEBUG_SERVICE;
72    static final boolean DEBUG_DELAYED_STARTS = DEBUG_DELAYED_SERVICE;
73    static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU;
74    static final boolean LOG_SERVICE_START_STOP = true;
75    static final String TAG = ActivityManagerService.TAG;
76    static final String TAG_MU = ActivityManagerService.TAG_MU;
77
78    // How long we wait for a service to finish executing.
79    static final int SERVICE_TIMEOUT = 20*1000;
80
81    // How long we wait for a service to finish executing.
82    static final int SERVICE_BACKGROUND_TIMEOUT = SERVICE_TIMEOUT * 10;
83
84    // How long a service needs to be running until restarting its process
85    // is no longer considered to be a relaunch of the service.
86    static final int SERVICE_RESTART_DURATION = 1*1000;
87
88    // How long a service needs to be running until it will start back at
89    // SERVICE_RESTART_DURATION after being killed.
90    static final int SERVICE_RESET_RUN_DURATION = 60*1000;
91
92    // Multiplying factor to increase restart duration time by, for each time
93    // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
94    static final int SERVICE_RESTART_DURATION_FACTOR = 4;
95
96    // The minimum amount of time between restarting services that we allow.
97    // That is, when multiple services are restarting, we won't allow each
98    // to restart less than this amount of time from the last one.
99    static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
100
101    // Maximum amount of time for there to be no activity on a service before
102    // we consider it non-essential and allow its process to go on the
103    // LRU background list.
104    static final int MAX_SERVICE_INACTIVITY = 30*60*1000;
105
106    // How long we wait for a background started service to stop itself before
107    // allowing the next pending start to run.
108    static final int BG_START_TIMEOUT = 15*1000;
109
110    final ActivityManagerService mAm;
111
112    // Maximum number of services that we allow to start in the background
113    // at the same time.
114    final int mMaxStartingBackground;
115
116    final SparseArray<ServiceMap> mServiceMap = new SparseArray<ServiceMap>();
117
118    /**
119     * All currently bound service connections.  Keys are the IBinder of
120     * the client's IServiceConnection.
121     */
122    final ArrayMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections
123            = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>();
124
125    /**
126     * List of services that we have been asked to start,
127     * but haven't yet been able to.  It is used to hold start requests
128     * while waiting for their corresponding application thread to get
129     * going.
130     */
131    final ArrayList<ServiceRecord> mPendingServices
132            = new ArrayList<ServiceRecord>();
133
134    /**
135     * List of services that are scheduled to restart following a crash.
136     */
137    final ArrayList<ServiceRecord> mRestartingServices
138            = new ArrayList<ServiceRecord>();
139
140    /**
141     * List of services that are in the process of being destroyed.
142     */
143    final ArrayList<ServiceRecord> mDestroyingServices
144            = new ArrayList<ServiceRecord>();
145
146    /** Amount of time to allow a last ANR message to exist before freeing the memory. */
147    static final int LAST_ANR_LIFETIME_DURATION_MSECS = 2 * 60 * 60 * 1000; // Two hours
148
149    String mLastAnrDump;
150
151    final Runnable mLastAnrDumpClearer = new Runnable() {
152        @Override public void run() {
153            synchronized (mAm) {
154                mLastAnrDump = null;
155            }
156        }
157    };
158
159    static final class DelayingProcess extends ArrayList<ServiceRecord> {
160        long timeoout;
161    }
162
163    /**
164     * Information about services for a single user.
165     */
166    class ServiceMap extends Handler {
167        final int mUserId;
168        final ArrayMap<ComponentName, ServiceRecord> mServicesByName
169                = new ArrayMap<ComponentName, ServiceRecord>();
170        final ArrayMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent
171                = new ArrayMap<Intent.FilterComparison, ServiceRecord>();
172
173        final ArrayList<ServiceRecord> mDelayedStartList
174                = new ArrayList<ServiceRecord>();
175        /* XXX eventually I'd like to have this based on processes instead of services.
176         * That is, if we try to start two services in a row both running in the same
177         * process, this should be one entry in mStartingBackground for that one process
178         * that remains until all services in it are done.
179        final ArrayMap<ProcessRecord, DelayingProcess> mStartingBackgroundMap
180                = new ArrayMap<ProcessRecord, DelayingProcess>();
181        final ArrayList<DelayingProcess> mStartingProcessList
182                = new ArrayList<DelayingProcess>();
183        */
184
185        final ArrayList<ServiceRecord> mStartingBackground
186                = new ArrayList<ServiceRecord>();
187
188        static final int MSG_BG_START_TIMEOUT = 1;
189
190        ServiceMap(Looper looper, int userId) {
191            super(looper);
192            mUserId = userId;
193        }
194
195        @Override
196        public void handleMessage(Message msg) {
197            switch (msg.what) {
198                case MSG_BG_START_TIMEOUT: {
199                    synchronized (mAm) {
200                        rescheduleDelayedStarts();
201                    }
202                } break;
203            }
204        }
205
206        void ensureNotStartingBackground(ServiceRecord r) {
207            if (mStartingBackground.remove(r)) {
208                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "No longer background starting: " + r);
209                rescheduleDelayedStarts();
210            }
211            if (mDelayedStartList.remove(r)) {
212                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "No longer delaying start: " + r);
213            }
214        }
215
216        void rescheduleDelayedStarts() {
217            removeMessages(MSG_BG_START_TIMEOUT);
218            final long now = SystemClock.uptimeMillis();
219            for (int i=0, N=mStartingBackground.size(); i<N; i++) {
220                ServiceRecord r = mStartingBackground.get(i);
221                if (r.startingBgTimeout <= now) {
222                    Slog.i(TAG, "Waited long enough for: " + r);
223                    mStartingBackground.remove(i);
224                    N--;
225                    i--;
226                }
227            }
228            while (mDelayedStartList.size() > 0
229                    && mStartingBackground.size() < mMaxStartingBackground) {
230                ServiceRecord r = mDelayedStartList.remove(0);
231                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (exec next): " + r);
232                if (r.pendingStarts.size() <= 0) {
233                    Slog.w(TAG, "**** NO PENDING STARTS! " + r + " startReq=" + r.startRequested
234                            + " delayedStop=" + r.delayedStop);
235                }
236                if (DEBUG_DELAYED_SERVICE) {
237                    if (mDelayedStartList.size() > 0) {
238                        Slog.v(TAG, "Remaining delayed list:");
239                        for (int i=0; i<mDelayedStartList.size(); i++) {
240                            Slog.v(TAG, "  #" + i + ": " + mDelayedStartList.get(i));
241                        }
242                    }
243                }
244                r.delayed = false;
245                startServiceInnerLocked(this, r.pendingStarts.get(0).intent, r, false, true);
246            }
247            if (mStartingBackground.size() > 0) {
248                ServiceRecord next = mStartingBackground.get(0);
249                long when = next.startingBgTimeout > now ? next.startingBgTimeout : now;
250                if (DEBUG_DELAYED_SERVICE) Slog.v(TAG, "Top bg start is " + next
251                        + ", can delay others up to " + when);
252                Message msg = obtainMessage(MSG_BG_START_TIMEOUT);
253                sendMessageAtTime(msg, when);
254            }
255            if (mStartingBackground.size() < mMaxStartingBackground) {
256                mAm.backgroundServicesFinishedLocked(mUserId);
257            }
258        }
259    }
260
261    public ActiveServices(ActivityManagerService service) {
262        mAm = service;
263        int maxBg = 0;
264        try {
265            maxBg = Integer.parseInt(SystemProperties.get("ro.config.max_starting_bg", "0"));
266        } catch(RuntimeException e) {
267        }
268        mMaxStartingBackground = maxBg > 0
269                ? maxBg : ActivityManager.isLowRamDeviceStatic() ? 1 : 8;
270    }
271
272    ServiceRecord getServiceByName(ComponentName name, int callingUser) {
273        // TODO: Deal with global services
274        if (DEBUG_MU)
275            Slog.v(TAG_MU, "getServiceByName(" + name + "), callingUser = " + callingUser);
276        return getServiceMap(callingUser).mServicesByName.get(name);
277    }
278
279    boolean hasBackgroundServices(int callingUser) {
280        ServiceMap smap = mServiceMap.get(callingUser);
281        return smap != null ? smap.mStartingBackground.size() >= mMaxStartingBackground : false;
282    }
283
284    private ServiceMap getServiceMap(int callingUser) {
285        ServiceMap smap = mServiceMap.get(callingUser);
286        if (smap == null) {
287            smap = new ServiceMap(mAm.mHandler.getLooper(), callingUser);
288            mServiceMap.put(callingUser, smap);
289        }
290        return smap;
291    }
292
293    ArrayMap<ComponentName, ServiceRecord> getServices(int callingUser) {
294        return getServiceMap(callingUser).mServicesByName;
295    }
296
297    ComponentName startServiceLocked(IApplicationThread caller,
298            Intent service, String resolvedType,
299            int callingPid, int callingUid, int userId) {
300        if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "startService: " + service
301                + " type=" + resolvedType + " args=" + service.getExtras());
302
303        final boolean callerFg;
304        if (caller != null) {
305            final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller);
306            if (callerApp == null) {
307                throw new SecurityException(
308                        "Unable to find app for caller " + caller
309                        + " (pid=" + Binder.getCallingPid()
310                        + ") when starting service " + service);
311            }
312            callerFg = callerApp.setSchedGroup != Process.THREAD_GROUP_BG_NONINTERACTIVE;
313        } else {
314            callerFg = true;
315        }
316
317
318        ServiceLookupResult res =
319            retrieveServiceLocked(service, resolvedType,
320                    callingPid, callingUid, userId, true, callerFg);
321        if (res == null) {
322            return null;
323        }
324        if (res.record == null) {
325            return new ComponentName("!", res.permission != null
326                    ? res.permission : "private to package");
327        }
328
329        ServiceRecord r = res.record;
330
331        if (!mAm.getUserManagerLocked().exists(r.userId)) {
332            Slog.d(TAG, "Trying to start service with non-existent user! " + r.userId);
333            return null;
334        }
335
336        NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked(
337                callingUid, r.packageName, service, service.getFlags(), null, r.userId);
338        if (unscheduleServiceRestartLocked(r, callingUid, false)) {
339            if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r);
340        }
341        r.lastActivity = SystemClock.uptimeMillis();
342        r.startRequested = true;
343        r.delayedStop = false;
344        r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
345                service, neededGrants));
346
347        final ServiceMap smap = getServiceMap(r.userId);
348        boolean addToStarting = false;
349        if (!callerFg && r.app == null && mAm.mStartedUsers.get(r.userId) != null) {
350            ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false);
351            if (proc == null || proc.curProcState > ActivityManager.PROCESS_STATE_RECEIVER) {
352                // If this is not coming from a foreground caller, then we may want
353                // to delay the start if there are already other background services
354                // that are starting.  This is to avoid process start spam when lots
355                // of applications are all handling things like connectivity broadcasts.
356                // We only do this for cached processes, because otherwise an application
357                // can have assumptions about calling startService() for a service to run
358                // in its own process, and for that process to not be killed before the
359                // service is started.  This is especially the case for receivers, which
360                // may start a service in onReceive() to do some additional work and have
361                // initialized some global state as part of that.
362                if (DEBUG_DELAYED_SERVICE) Slog.v(TAG, "Potential start delay of " + r + " in "
363                        + proc);
364                if (r.delayed) {
365                    // This service is already scheduled for a delayed start; just leave
366                    // it still waiting.
367                    if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Continuing to delay: " + r);
368                    return r.name;
369                }
370                if (smap.mStartingBackground.size() >= mMaxStartingBackground) {
371                    // Something else is starting, delay!
372                    Slog.i(TAG, "Delaying start of: " + r);
373                    smap.mDelayedStartList.add(r);
374                    r.delayed = true;
375                    return r.name;
376                }
377                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying: " + r);
378                addToStarting = true;
379            } else if (proc.curProcState >= ActivityManager.PROCESS_STATE_SERVICE) {
380                // We slightly loosen when we will enqueue this new service as a background
381                // starting service we are waiting for, to also include processes that are
382                // currently running other services or receivers.
383                addToStarting = true;
384                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Not delaying, but counting as bg: " + r);
385            } else if (DEBUG_DELAYED_STARTS) {
386                StringBuilder sb = new StringBuilder(128);
387                sb.append("Not potential delay (state=").append(proc.curProcState)
388                        .append(' ').append(proc.adjType);
389                String reason = proc.makeAdjReason();
390                if (reason != null) {
391                    sb.append(' ');
392                    sb.append(reason);
393                }
394                sb.append("): ");
395                sb.append(r.toString());
396                Slog.v(TAG, sb.toString());
397            }
398        } else if (DEBUG_DELAYED_STARTS) {
399            if (callerFg) {
400                Slog.v(TAG, "Not potential delay (callerFg=" + callerFg + " uid="
401                        + callingUid + " pid=" + callingPid + "): " + r);
402            } else if (r.app != null) {
403                Slog.v(TAG, "Not potential delay (cur app=" + r.app + "): " + r);
404            } else {
405                Slog.v(TAG, "Not potential delay (user " + r.userId + " not started): " + r);
406            }
407        }
408
409        return startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
410    }
411
412    ComponentName startServiceInnerLocked(ServiceMap smap, Intent service,
413            ServiceRecord r, boolean callerFg, boolean addToStarting) {
414        ProcessStats.ServiceState stracker = r.getTracker();
415        if (stracker != null) {
416            stracker.setStarted(true, mAm.mProcessStats.getMemFactorLocked(), r.lastActivity);
417        }
418        r.callStart = false;
419        synchronized (r.stats.getBatteryStats()) {
420            r.stats.startRunningLocked();
421        }
422        String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false);
423        if (error != null) {
424            return new ComponentName("!!", error);
425        }
426
427        if (r.startRequested && addToStarting) {
428            boolean first = smap.mStartingBackground.size() == 0;
429            smap.mStartingBackground.add(r);
430            r.startingBgTimeout = SystemClock.uptimeMillis() + BG_START_TIMEOUT;
431            if (DEBUG_DELAYED_SERVICE) {
432                RuntimeException here = new RuntimeException("here");
433                here.fillInStackTrace();
434                Slog.v(TAG, "Starting background (first=" + first + "): " + r, here);
435            } else if (DEBUG_DELAYED_STARTS) {
436                Slog.v(TAG, "Starting background (first=" + first + "): " + r);
437            }
438            if (first) {
439                smap.rescheduleDelayedStarts();
440            }
441        } else if (callerFg) {
442            smap.ensureNotStartingBackground(r);
443        }
444
445        return r.name;
446    }
447
448    private void stopServiceLocked(ServiceRecord service) {
449        if (service.delayed) {
450            // If service isn't actually running, but is is being held in the
451            // delayed list, then we need to keep it started but note that it
452            // should be stopped once no longer delayed.
453            if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Delaying stop of pending: " + service);
454            service.delayedStop = true;
455            return;
456        }
457        synchronized (service.stats.getBatteryStats()) {
458            service.stats.stopRunningLocked();
459        }
460        service.startRequested = false;
461        if (service.tracker != null) {
462            service.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(),
463                    SystemClock.uptimeMillis());
464        }
465        service.callStart = false;
466        bringDownServiceIfNeededLocked(service, false, false);
467    }
468
469    int stopServiceLocked(IApplicationThread caller, Intent service,
470            String resolvedType, int userId) {
471        if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service
472                + " type=" + resolvedType);
473
474        final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller);
475        if (caller != null && callerApp == null) {
476            throw new SecurityException(
477                    "Unable to find app for caller " + caller
478                    + " (pid=" + Binder.getCallingPid()
479                    + ") when stopping service " + service);
480        }
481
482        // If this service is active, make sure it is stopped.
483        ServiceLookupResult r = retrieveServiceLocked(service, resolvedType,
484                Binder.getCallingPid(), Binder.getCallingUid(), userId, false, false);
485        if (r != null) {
486            if (r.record != null) {
487                final long origId = Binder.clearCallingIdentity();
488                try {
489                    stopServiceLocked(r.record);
490                } finally {
491                    Binder.restoreCallingIdentity(origId);
492                }
493                return 1;
494            }
495            return -1;
496        }
497
498        return 0;
499    }
500
501    IBinder peekServiceLocked(Intent service, String resolvedType) {
502        ServiceLookupResult r = retrieveServiceLocked(service, resolvedType,
503                Binder.getCallingPid(), Binder.getCallingUid(),
504                UserHandle.getCallingUserId(), false, false);
505
506        IBinder ret = null;
507        if (r != null) {
508            // r.record is null if findServiceLocked() failed the caller permission check
509            if (r.record == null) {
510                throw new SecurityException(
511                        "Permission Denial: Accessing service " + r.record.name
512                        + " from pid=" + Binder.getCallingPid()
513                        + ", uid=" + Binder.getCallingUid()
514                        + " requires " + r.permission);
515            }
516            IntentBindRecord ib = r.record.bindings.get(r.record.intent);
517            if (ib != null) {
518                ret = ib.binder;
519            }
520        }
521
522        return ret;
523    }
524
525    boolean stopServiceTokenLocked(ComponentName className, IBinder token,
526            int startId) {
527        if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className
528                + " " + token + " startId=" + startId);
529        ServiceRecord r = findServiceLocked(className, token, UserHandle.getCallingUserId());
530        if (r != null) {
531            if (startId >= 0) {
532                // Asked to only stop if done with all work.  Note that
533                // to avoid leaks, we will take this as dropping all
534                // start items up to and including this one.
535                ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
536                if (si != null) {
537                    while (r.deliveredStarts.size() > 0) {
538                        ServiceRecord.StartItem cur = r.deliveredStarts.remove(0);
539                        cur.removeUriPermissionsLocked();
540                        if (cur == si) {
541                            break;
542                        }
543                    }
544                }
545
546                if (r.getLastStartId() != startId) {
547                    return false;
548                }
549
550                if (r.deliveredStarts.size() > 0) {
551                    Slog.w(TAG, "stopServiceToken startId " + startId
552                            + " is last, but have " + r.deliveredStarts.size()
553                            + " remaining args");
554                }
555            }
556
557            synchronized (r.stats.getBatteryStats()) {
558                r.stats.stopRunningLocked();
559            }
560            r.startRequested = false;
561            if (r.tracker != null) {
562                r.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(),
563                        SystemClock.uptimeMillis());
564            }
565            r.callStart = false;
566            final long origId = Binder.clearCallingIdentity();
567            bringDownServiceIfNeededLocked(r, false, false);
568            Binder.restoreCallingIdentity(origId);
569            return true;
570        }
571        return false;
572    }
573
574    public void setServiceForegroundLocked(ComponentName className, IBinder token,
575            int id, Notification notification, boolean removeNotification) {
576        final int userId = UserHandle.getCallingUserId();
577        final long origId = Binder.clearCallingIdentity();
578        try {
579            ServiceRecord r = findServiceLocked(className, token, userId);
580            if (r != null) {
581                if (id != 0) {
582                    if (notification == null) {
583                        throw new IllegalArgumentException("null notification");
584                    }
585                    if (r.foregroundId != id) {
586                        r.cancelNotification();
587                        r.foregroundId = id;
588                    }
589                    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
590                    r.foregroundNoti = notification;
591                    r.isForeground = true;
592                    r.postNotification();
593                    if (r.app != null) {
594                        updateServiceForegroundLocked(r.app, true);
595                    }
596                    getServiceMap(r.userId).ensureNotStartingBackground(r);
597                } else {
598                    if (r.isForeground) {
599                        r.isForeground = false;
600                        if (r.app != null) {
601                            mAm.updateLruProcessLocked(r.app, false, null);
602                            updateServiceForegroundLocked(r.app, true);
603                        }
604                    }
605                    if (removeNotification) {
606                        r.cancelNotification();
607                        r.foregroundId = 0;
608                        r.foregroundNoti = null;
609                    } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
610                        r.stripForegroundServiceFlagFromNotification();
611                    }
612                }
613            }
614        } finally {
615            Binder.restoreCallingIdentity(origId);
616        }
617    }
618
619    private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) {
620        boolean anyForeground = false;
621        for (int i=proc.services.size()-1; i>=0; i--) {
622            ServiceRecord sr = proc.services.valueAt(i);
623            if (sr.isForeground) {
624                anyForeground = true;
625                break;
626            }
627        }
628        mAm.updateProcessForegroundLocked(proc, anyForeground, oomAdj);
629    }
630
631    public void updateServiceConnectionActivitiesLocked(ProcessRecord clientProc) {
632        ArraySet<ProcessRecord> updatedProcesses = null;
633        for (int i=0; i<clientProc.connections.size(); i++) {
634            final ConnectionRecord conn = clientProc.connections.valueAt(i);
635            final ProcessRecord proc = conn.binding.service.app;
636            if (proc == null || proc == clientProc) {
637                continue;
638            } else if (updatedProcesses == null) {
639                updatedProcesses = new ArraySet<>();
640            } else if (updatedProcesses.contains(proc)) {
641                continue;
642            }
643            updatedProcesses.add(proc);
644            updateServiceClientActivitiesLocked(proc, null, false);
645        }
646    }
647
648    private boolean updateServiceClientActivitiesLocked(ProcessRecord proc,
649            ConnectionRecord modCr, boolean updateLru) {
650        if (modCr != null && modCr.binding.client != null) {
651            if (modCr.binding.client.activities.size() <= 0) {
652                // This connection is from a client without activities, so adding
653                // and removing is not interesting.
654                return false;
655            }
656        }
657
658        boolean anyClientActivities = false;
659        for (int i=proc.services.size()-1; i>=0 && !anyClientActivities; i--) {
660            ServiceRecord sr = proc.services.valueAt(i);
661            for (int conni=sr.connections.size()-1; conni>=0 && !anyClientActivities; conni--) {
662                ArrayList<ConnectionRecord> clist = sr.connections.valueAt(conni);
663                for (int cri=clist.size()-1; cri>=0; cri--) {
664                    ConnectionRecord cr = clist.get(cri);
665                    if (cr.binding.client == null || cr.binding.client == proc) {
666                        // Binding to ourself is not interesting.
667                        continue;
668                    }
669                    if (cr.binding.client.activities.size() > 0) {
670                        anyClientActivities = true;
671                        break;
672                    }
673                }
674            }
675        }
676        if (anyClientActivities != proc.hasClientActivities) {
677            proc.hasClientActivities = anyClientActivities;
678            if (updateLru) {
679                mAm.updateLruProcessLocked(proc, anyClientActivities, null);
680            }
681            return true;
682        }
683        return false;
684    }
685
686    int bindServiceLocked(IApplicationThread caller, IBinder token,
687            Intent service, String resolvedType,
688            IServiceConnection connection, int flags, int userId) {
689        if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service
690                + " type=" + resolvedType + " conn=" + connection.asBinder()
691                + " flags=0x" + Integer.toHexString(flags));
692        final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller);
693        if (callerApp == null) {
694            throw new SecurityException(
695                    "Unable to find app for caller " + caller
696                    + " (pid=" + Binder.getCallingPid()
697                    + ") when binding service " + service);
698        }
699
700        ActivityRecord activity = null;
701        if (token != null) {
702            activity = ActivityRecord.isInStackLocked(token);
703            if (activity == null) {
704                Slog.w(TAG, "Binding with unknown activity: " + token);
705                return 0;
706            }
707        }
708
709        int clientLabel = 0;
710        PendingIntent clientIntent = null;
711
712        if (callerApp.info.uid == Process.SYSTEM_UID) {
713            // Hacky kind of thing -- allow system stuff to tell us
714            // what they are, so we can report this elsewhere for
715            // others to know why certain services are running.
716            try {
717                clientIntent = service.getParcelableExtra(Intent.EXTRA_CLIENT_INTENT);
718            } catch (RuntimeException e) {
719            }
720            if (clientIntent != null) {
721                clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0);
722                if (clientLabel != 0) {
723                    // There are no useful extras in the intent, trash them.
724                    // System code calling with this stuff just needs to know
725                    // this will happen.
726                    service = service.cloneFilter();
727                }
728            }
729        }
730
731        if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
732            mAm.enforceCallingPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS,
733                    "BIND_TREAT_LIKE_ACTIVITY");
734        }
735
736        final boolean callerFg = callerApp.setSchedGroup != Process.THREAD_GROUP_BG_NONINTERACTIVE;
737
738        ServiceLookupResult res =
739            retrieveServiceLocked(service, resolvedType,
740                    Binder.getCallingPid(), Binder.getCallingUid(), userId, true, callerFg);
741        if (res == null) {
742            return 0;
743        }
744        if (res.record == null) {
745            return -1;
746        }
747        ServiceRecord s = res.record;
748
749        final long origId = Binder.clearCallingIdentity();
750
751        try {
752            if (unscheduleServiceRestartLocked(s, callerApp.info.uid, false)) {
753                if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: "
754                        + s);
755            }
756
757            if ((flags&Context.BIND_AUTO_CREATE) != 0) {
758                s.lastActivity = SystemClock.uptimeMillis();
759                if (!s.hasAutoCreateConnections()) {
760                    // This is the first binding, let the tracker know.
761                    ProcessStats.ServiceState stracker = s.getTracker();
762                    if (stracker != null) {
763                        stracker.setBound(true, mAm.mProcessStats.getMemFactorLocked(),
764                                s.lastActivity);
765                    }
766                }
767            }
768
769            mAm.startAssociationLocked(callerApp.uid, callerApp.processName,
770                    s.appInfo.uid, s.name, s.processName);
771
772            AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
773            ConnectionRecord c = new ConnectionRecord(b, activity,
774                    connection, flags, clientLabel, clientIntent);
775
776            IBinder binder = connection.asBinder();
777            ArrayList<ConnectionRecord> clist = s.connections.get(binder);
778            if (clist == null) {
779                clist = new ArrayList<ConnectionRecord>();
780                s.connections.put(binder, clist);
781            }
782            clist.add(c);
783            b.connections.add(c);
784            if (activity != null) {
785                if (activity.connections == null) {
786                    activity.connections = new HashSet<ConnectionRecord>();
787                }
788                activity.connections.add(c);
789            }
790            b.client.connections.add(c);
791            if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) {
792                b.client.hasAboveClient = true;
793            }
794            if (s.app != null) {
795                updateServiceClientActivitiesLocked(s.app, c, true);
796            }
797            clist = mServiceConnections.get(binder);
798            if (clist == null) {
799                clist = new ArrayList<ConnectionRecord>();
800                mServiceConnections.put(binder, clist);
801            }
802            clist.add(c);
803
804            if ((flags&Context.BIND_AUTO_CREATE) != 0) {
805                s.lastActivity = SystemClock.uptimeMillis();
806                if (bringUpServiceLocked(s, service.getFlags(), callerFg, false) != null) {
807                    return 0;
808                }
809            }
810
811            if (s.app != null) {
812                if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
813                    s.app.treatLikeActivity = true;
814                }
815                // This could have made the service more important.
816                mAm.updateLruProcessLocked(s.app, s.app.hasClientActivities
817                        || s.app.treatLikeActivity, b.client);
818                mAm.updateOomAdjLocked(s.app);
819            }
820
821            if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b
822                    + ": received=" + b.intent.received
823                    + " apps=" + b.intent.apps.size()
824                    + " doRebind=" + b.intent.doRebind);
825
826            if (s.app != null && b.intent.received) {
827                // Service is already running, so we can immediately
828                // publish the connection.
829                try {
830                    c.conn.connected(s.name, b.intent.binder);
831                } catch (Exception e) {
832                    Slog.w(TAG, "Failure sending service " + s.shortName
833                            + " to connection " + c.conn.asBinder()
834                            + " (in " + c.binding.client.processName + ")", e);
835                }
836
837                // If this is the first app connected back to this binding,
838                // and the service had previously asked to be told when
839                // rebound, then do so.
840                if (b.intent.apps.size() == 1 && b.intent.doRebind) {
841                    requestServiceBindingLocked(s, b.intent, callerFg, true);
842                }
843            } else if (!b.intent.requested) {
844                requestServiceBindingLocked(s, b.intent, callerFg, false);
845            }
846
847            getServiceMap(s.userId).ensureNotStartingBackground(s);
848
849        } finally {
850            Binder.restoreCallingIdentity(origId);
851        }
852
853        return 1;
854    }
855
856    void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) {
857        final long origId = Binder.clearCallingIdentity();
858        try {
859            if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r
860                    + " " + intent + ": " + service);
861            if (r != null) {
862                Intent.FilterComparison filter
863                        = new Intent.FilterComparison(intent);
864                IntentBindRecord b = r.bindings.get(filter);
865                if (b != null && !b.received) {
866                    b.binder = service;
867                    b.requested = true;
868                    b.received = true;
869                    for (int conni=r.connections.size()-1; conni>=0; conni--) {
870                        ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni);
871                        for (int i=0; i<clist.size(); i++) {
872                            ConnectionRecord c = clist.get(i);
873                            if (!filter.equals(c.binding.intent.intent)) {
874                                if (DEBUG_SERVICE) Slog.v(
875                                        TAG, "Not publishing to: " + c);
876                                if (DEBUG_SERVICE) Slog.v(
877                                        TAG, "Bound intent: " + c.binding.intent.intent);
878                                if (DEBUG_SERVICE) Slog.v(
879                                        TAG, "Published intent: " + intent);
880                                continue;
881                            }
882                            if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c);
883                            try {
884                                c.conn.connected(r.name, service);
885                            } catch (Exception e) {
886                                Slog.w(TAG, "Failure sending service " + r.name +
887                                      " to connection " + c.conn.asBinder() +
888                                      " (in " + c.binding.client.processName + ")", e);
889                            }
890                        }
891                    }
892                }
893
894                serviceDoneExecutingLocked(r, mDestroyingServices.contains(r), false);
895            }
896        } finally {
897            Binder.restoreCallingIdentity(origId);
898        }
899    }
900
901    boolean unbindServiceLocked(IServiceConnection connection) {
902        IBinder binder = connection.asBinder();
903        if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder);
904        ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder);
905        if (clist == null) {
906            Slog.w(TAG, "Unbind failed: could not find connection for "
907                  + connection.asBinder());
908            return false;
909        }
910
911        final long origId = Binder.clearCallingIdentity();
912        try {
913            while (clist.size() > 0) {
914                ConnectionRecord r = clist.get(0);
915                removeConnectionLocked(r, null, null);
916                if (clist.size() > 0 && clist.get(0) == r) {
917                    // In case it didn't get removed above, do it now.
918                    Slog.wtf(TAG, "Connection " + r + " not removed for binder " + binder);
919                    clist.remove(0);
920                }
921
922                if (r.binding.service.app != null) {
923                    // This could have made the service less important.
924                    if ((r.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
925                        r.binding.service.app.treatLikeActivity = true;
926                        mAm.updateLruProcessLocked(r.binding.service.app,
927                                r.binding.service.app.hasClientActivities
928                                || r.binding.service.app.treatLikeActivity, null);
929                    }
930                    mAm.updateOomAdjLocked(r.binding.service.app);
931                }
932            }
933        } finally {
934            Binder.restoreCallingIdentity(origId);
935        }
936
937        return true;
938    }
939
940    void unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind) {
941        final long origId = Binder.clearCallingIdentity();
942        try {
943            if (r != null) {
944                Intent.FilterComparison filter
945                        = new Intent.FilterComparison(intent);
946                IntentBindRecord b = r.bindings.get(filter);
947                if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r
948                        + " at " + b + ": apps="
949                        + (b != null ? b.apps.size() : 0));
950
951                boolean inDestroying = mDestroyingServices.contains(r);
952                if (b != null) {
953                    if (b.apps.size() > 0 && !inDestroying) {
954                        // Applications have already bound since the last
955                        // unbind, so just rebind right here.
956                        boolean inFg = false;
957                        for (int i=b.apps.size()-1; i>=0; i--) {
958                            ProcessRecord client = b.apps.valueAt(i).client;
959                            if (client != null && client.setSchedGroup
960                                    != Process.THREAD_GROUP_BG_NONINTERACTIVE) {
961                                inFg = true;
962                                break;
963                            }
964                        }
965                        requestServiceBindingLocked(r, b, inFg, true);
966                    } else {
967                        // Note to tell the service the next time there is
968                        // a new client.
969                        b.doRebind = true;
970                    }
971                }
972
973                serviceDoneExecutingLocked(r, inDestroying, false);
974            }
975        } finally {
976            Binder.restoreCallingIdentity(origId);
977        }
978    }
979
980    private final ServiceRecord findServiceLocked(ComponentName name,
981            IBinder token, int userId) {
982        ServiceRecord r = getServiceByName(name, userId);
983        return r == token ? r : null;
984    }
985
986    private final class ServiceLookupResult {
987        final ServiceRecord record;
988        final String permission;
989
990        ServiceLookupResult(ServiceRecord _record, String _permission) {
991            record = _record;
992            permission = _permission;
993        }
994    }
995
996    private class ServiceRestarter implements Runnable {
997        private ServiceRecord mService;
998
999        void setService(ServiceRecord service) {
1000            mService = service;
1001        }
1002
1003        public void run() {
1004            synchronized(mAm) {
1005                performServiceRestartLocked(mService);
1006            }
1007        }
1008    }
1009
1010    private ServiceLookupResult retrieveServiceLocked(Intent service,
1011            String resolvedType, int callingPid, int callingUid, int userId,
1012            boolean createIfNeeded, boolean callingFromFg) {
1013        ServiceRecord r = null;
1014        if (DEBUG_SERVICE) Slog.v(TAG, "retrieveServiceLocked: " + service
1015                + " type=" + resolvedType + " callingUid=" + callingUid);
1016
1017        userId = mAm.handleIncomingUser(callingPid, callingUid, userId,
1018                false, ActivityManagerService.ALLOW_NON_FULL_IN_PROFILE, "service", null);
1019
1020        ServiceMap smap = getServiceMap(userId);
1021        final ComponentName comp = service.getComponent();
1022        if (comp != null) {
1023            r = smap.mServicesByName.get(comp);
1024        }
1025        if (r == null) {
1026            Intent.FilterComparison filter = new Intent.FilterComparison(service);
1027            r = smap.mServicesByIntent.get(filter);
1028        }
1029        if (r == null) {
1030            try {
1031                ResolveInfo rInfo =
1032                    AppGlobals.getPackageManager().resolveService(
1033                                service, resolvedType,
1034                                ActivityManagerService.STOCK_PM_FLAGS, userId);
1035                ServiceInfo sInfo =
1036                    rInfo != null ? rInfo.serviceInfo : null;
1037                if (sInfo == null) {
1038                    Slog.w(TAG, "Unable to start service " + service + " U=" + userId +
1039                          ": not found");
1040                    return null;
1041                }
1042                ComponentName name = new ComponentName(
1043                        sInfo.applicationInfo.packageName, sInfo.name);
1044                if (userId > 0) {
1045                    if (mAm.isSingleton(sInfo.processName, sInfo.applicationInfo,
1046                            sInfo.name, sInfo.flags)
1047                            && mAm.isValidSingletonCall(callingUid, sInfo.applicationInfo.uid)) {
1048                        userId = 0;
1049                        smap = getServiceMap(0);
1050                    }
1051                    sInfo = new ServiceInfo(sInfo);
1052                    sInfo.applicationInfo = mAm.getAppInfoForUser(sInfo.applicationInfo, userId);
1053                }
1054                r = smap.mServicesByName.get(name);
1055                if (r == null && createIfNeeded) {
1056                    Intent.FilterComparison filter
1057                            = new Intent.FilterComparison(service.cloneFilter());
1058                    ServiceRestarter res = new ServiceRestarter();
1059                    BatteryStatsImpl.Uid.Pkg.Serv ss = null;
1060                    BatteryStatsImpl stats = mAm.mBatteryStatsService.getActiveStatistics();
1061                    synchronized (stats) {
1062                        ss = stats.getServiceStatsLocked(
1063                                sInfo.applicationInfo.uid, sInfo.packageName,
1064                                sInfo.name);
1065                    }
1066                    r = new ServiceRecord(mAm, ss, name, filter, sInfo, callingFromFg, res);
1067                    res.setService(r);
1068                    smap.mServicesByName.put(name, r);
1069                    smap.mServicesByIntent.put(filter, r);
1070
1071                    // Make sure this component isn't in the pending list.
1072                    for (int i=mPendingServices.size()-1; i>=0; i--) {
1073                        ServiceRecord pr = mPendingServices.get(i);
1074                        if (pr.serviceInfo.applicationInfo.uid == sInfo.applicationInfo.uid
1075                                && pr.name.equals(name)) {
1076                            mPendingServices.remove(i);
1077                        }
1078                    }
1079                }
1080            } catch (RemoteException ex) {
1081                // pm is in same process, this will never happen.
1082            }
1083        }
1084        if (r != null) {
1085            if (mAm.checkComponentPermission(r.permission,
1086                    callingPid, callingUid, r.appInfo.uid, r.exported)
1087                    != PackageManager.PERMISSION_GRANTED) {
1088                if (!r.exported) {
1089                    Slog.w(TAG, "Permission Denial: Accessing service " + r.name
1090                            + " from pid=" + callingPid
1091                            + ", uid=" + callingUid
1092                            + " that is not exported from uid " + r.appInfo.uid);
1093                    return new ServiceLookupResult(null, "not exported from uid "
1094                            + r.appInfo.uid);
1095                }
1096                Slog.w(TAG, "Permission Denial: Accessing service " + r.name
1097                        + " from pid=" + callingPid
1098                        + ", uid=" + callingUid
1099                        + " requires " + r.permission);
1100                return new ServiceLookupResult(null, r.permission);
1101            }
1102            if (!mAm.mIntentFirewall.checkService(r.name, service, callingUid, callingPid,
1103                    resolvedType, r.appInfo)) {
1104                return null;
1105            }
1106            return new ServiceLookupResult(r, null);
1107        }
1108        return null;
1109    }
1110
1111    private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) {
1112        if (DEBUG_SERVICE) Slog.v(TAG, ">>> EXECUTING "
1113                + why + " of " + r + " in app " + r.app);
1114        else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, ">>> EXECUTING "
1115                + why + " of " + r.shortName);
1116        long now = SystemClock.uptimeMillis();
1117        if (r.executeNesting == 0) {
1118            r.executeFg = fg;
1119            ProcessStats.ServiceState stracker = r.getTracker();
1120            if (stracker != null) {
1121                stracker.setExecuting(true, mAm.mProcessStats.getMemFactorLocked(), now);
1122            }
1123            if (r.app != null) {
1124                r.app.executingServices.add(r);
1125                r.app.execServicesFg |= fg;
1126                if (r.app.executingServices.size() == 1) {
1127                    scheduleServiceTimeoutLocked(r.app);
1128                }
1129            }
1130        } else if (r.app != null && fg && !r.app.execServicesFg) {
1131            r.app.execServicesFg = true;
1132            scheduleServiceTimeoutLocked(r.app);
1133        }
1134        r.executeFg |= fg;
1135        r.executeNesting++;
1136        r.executingStart = now;
1137    }
1138
1139    private final boolean requestServiceBindingLocked(ServiceRecord r,
1140            IntentBindRecord i, boolean execInFg, boolean rebind) {
1141        if (r.app == null || r.app.thread == null) {
1142            // If service is not currently running, can't yet bind.
1143            return false;
1144        }
1145        if ((!i.requested || rebind) && i.apps.size() > 0) {
1146            try {
1147                bumpServiceExecutingLocked(r, execInFg, "bind");
1148                r.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE);
1149                r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind,
1150                        r.app.repProcState);
1151                if (!rebind) {
1152                    i.requested = true;
1153                }
1154                i.hasBound = true;
1155                i.doRebind = false;
1156            } catch (RemoteException e) {
1157                if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r);
1158                return false;
1159            }
1160        }
1161        return true;
1162    }
1163
1164    private final boolean scheduleServiceRestartLocked(ServiceRecord r,
1165            boolean allowCancel) {
1166        boolean canceled = false;
1167
1168        ServiceMap smap = getServiceMap(r.userId);
1169        if (smap.mServicesByName.get(r.name) != r) {
1170            ServiceRecord cur = smap.mServicesByName.get(r.name);
1171            Slog.wtf(TAG, "Attempting to schedule restart of " + r
1172                    + " when found in map: " + cur);
1173            return false;
1174        }
1175
1176        final long now = SystemClock.uptimeMillis();
1177
1178        if ((r.serviceInfo.applicationInfo.flags
1179                &ApplicationInfo.FLAG_PERSISTENT) == 0) {
1180            long minDuration = SERVICE_RESTART_DURATION;
1181            long resetTime = SERVICE_RESET_RUN_DURATION;
1182
1183            // Any delivered but not yet finished starts should be put back
1184            // on the pending list.
1185            final int N = r.deliveredStarts.size();
1186            if (N > 0) {
1187                for (int i=N-1; i>=0; i--) {
1188                    ServiceRecord.StartItem si = r.deliveredStarts.get(i);
1189                    si.removeUriPermissionsLocked();
1190                    if (si.intent == null) {
1191                        // We'll generate this again if needed.
1192                    } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT
1193                            && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) {
1194                        r.pendingStarts.add(0, si);
1195                        long dur = SystemClock.uptimeMillis() - si.deliveredTime;
1196                        dur *= 2;
1197                        if (minDuration < dur) minDuration = dur;
1198                        if (resetTime < dur) resetTime = dur;
1199                    } else {
1200                        Slog.w(TAG, "Canceling start item " + si.intent + " in service "
1201                                + r.name);
1202                        canceled = true;
1203                    }
1204                }
1205                r.deliveredStarts.clear();
1206            }
1207
1208            r.totalRestartCount++;
1209            if (r.restartDelay == 0) {
1210                r.restartCount++;
1211                r.restartDelay = minDuration;
1212            } else {
1213                // If it has been a "reasonably long time" since the service
1214                // was started, then reset our restart duration back to
1215                // the beginning, so we don't infinitely increase the duration
1216                // on a service that just occasionally gets killed (which is
1217                // a normal case, due to process being killed to reclaim memory).
1218                if (now > (r.restartTime+resetTime)) {
1219                    r.restartCount = 1;
1220                    r.restartDelay = minDuration;
1221                } else {
1222                    r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;
1223                    if (r.restartDelay < minDuration) {
1224                        r.restartDelay = minDuration;
1225                    }
1226                }
1227            }
1228
1229            r.nextRestartTime = now + r.restartDelay;
1230
1231            // Make sure that we don't end up restarting a bunch of services
1232            // all at the same time.
1233            boolean repeat;
1234            do {
1235                repeat = false;
1236                for (int i=mRestartingServices.size()-1; i>=0; i--) {
1237                    ServiceRecord r2 = mRestartingServices.get(i);
1238                    if (r2 != r && r.nextRestartTime
1239                            >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN)
1240                            && r.nextRestartTime
1241                            < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) {
1242                        r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN;
1243                        r.restartDelay = r.nextRestartTime - now;
1244                        repeat = true;
1245                        break;
1246                    }
1247                }
1248            } while (repeat);
1249
1250        } else {
1251            // Persistent processes are immediately restarted, so there is no
1252            // reason to hold of on restarting their services.
1253            r.totalRestartCount++;
1254            r.restartCount = 0;
1255            r.restartDelay = 0;
1256            r.nextRestartTime = now;
1257        }
1258
1259        if (!mRestartingServices.contains(r)) {
1260            r.createdFromFg = false;
1261            mRestartingServices.add(r);
1262            r.makeRestarting(mAm.mProcessStats.getMemFactorLocked(), now);
1263        }
1264
1265        r.cancelNotification();
1266
1267        mAm.mHandler.removeCallbacks(r.restarter);
1268        mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime);
1269        r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay;
1270        Slog.w(TAG, "Scheduling restart of crashed service "
1271                + r.shortName + " in " + r.restartDelay + "ms");
1272        EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART,
1273                r.userId, r.shortName, r.restartDelay);
1274
1275        return canceled;
1276    }
1277
1278    final void performServiceRestartLocked(ServiceRecord r) {
1279        if (!mRestartingServices.contains(r)) {
1280            return;
1281        }
1282        bringUpServiceLocked(r, r.intent.getIntent().getFlags(), r.createdFromFg, true);
1283    }
1284
1285    private final boolean unscheduleServiceRestartLocked(ServiceRecord r, int callingUid,
1286            boolean force) {
1287        if (!force && r.restartDelay == 0) {
1288            return false;
1289        }
1290        // Remove from the restarting list; if the service is currently on the
1291        // restarting list, or the call is coming from another app, then this
1292        // service has become of much more interest so we reset the restart interval.
1293        boolean removed = mRestartingServices.remove(r);
1294        if (removed || callingUid != r.appInfo.uid) {
1295            r.resetRestartCounter();
1296        }
1297        if (removed) {
1298            clearRestartingIfNeededLocked(r);
1299        }
1300        mAm.mHandler.removeCallbacks(r.restarter);
1301        return true;
1302    }
1303
1304    private void clearRestartingIfNeededLocked(ServiceRecord r) {
1305        if (r.restartTracker != null) {
1306            // If this is the last restarting record with this tracker, then clear
1307            // the tracker's restarting state.
1308            boolean stillTracking = false;
1309            for (int i=mRestartingServices.size()-1; i>=0; i--) {
1310                if (mRestartingServices.get(i).restartTracker == r.restartTracker) {
1311                    stillTracking = true;
1312                    break;
1313                }
1314            }
1315            if (!stillTracking) {
1316                r.restartTracker.setRestarting(false, mAm.mProcessStats.getMemFactorLocked(),
1317                        SystemClock.uptimeMillis());
1318                r.restartTracker = null;
1319            }
1320        }
1321    }
1322
1323    private final String bringUpServiceLocked(ServiceRecord r,
1324            int intentFlags, boolean execInFg, boolean whileRestarting) {
1325        //Slog.i(TAG, "Bring up service:");
1326        //r.dump("  ");
1327
1328        if (r.app != null && r.app.thread != null) {
1329            sendServiceArgsLocked(r, execInFg, false);
1330            return null;
1331        }
1332
1333        if (!whileRestarting && r.restartDelay > 0) {
1334            // If waiting for a restart, then do nothing.
1335            return null;
1336        }
1337
1338        if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent);
1339
1340        // We are now bringing the service up, so no longer in the
1341        // restarting state.
1342        if (mRestartingServices.remove(r)) {
1343            clearRestartingIfNeededLocked(r);
1344        }
1345
1346        // Make sure this service is no longer considered delayed, we are starting it now.
1347        if (r.delayed) {
1348            if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (bring up): " + r);
1349            getServiceMap(r.userId).mDelayedStartList.remove(r);
1350            r.delayed = false;
1351        }
1352
1353        // Make sure that the user who owns this service is started.  If not,
1354        // we don't want to allow it to run.
1355        if (mAm.mStartedUsers.get(r.userId) == null) {
1356            String msg = "Unable to launch app "
1357                    + r.appInfo.packageName + "/"
1358                    + r.appInfo.uid + " for service "
1359                    + r.intent.getIntent() + ": user " + r.userId + " is stopped";
1360            Slog.w(TAG, msg);
1361            bringDownServiceLocked(r);
1362            return msg;
1363        }
1364
1365        // Service is now being launched, its package can't be stopped.
1366        try {
1367            AppGlobals.getPackageManager().setPackageStoppedState(
1368                    r.packageName, false, r.userId);
1369        } catch (RemoteException e) {
1370        } catch (IllegalArgumentException e) {
1371            Slog.w(TAG, "Failed trying to unstop package "
1372                    + r.packageName + ": " + e);
1373        }
1374
1375        final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0;
1376        final String procName = r.processName;
1377        ProcessRecord app;
1378
1379        if (!isolated) {
1380            app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);
1381            if (DEBUG_MU) Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid
1382                        + " app=" + app);
1383            if (app != null && app.thread != null) {
1384                try {
1385                    app.addPackage(r.appInfo.packageName, r.appInfo.versionCode, mAm.mProcessStats);
1386                    realStartServiceLocked(r, app, execInFg);
1387                    return null;
1388                } catch (RemoteException e) {
1389                    Slog.w(TAG, "Exception when starting service " + r.shortName, e);
1390                }
1391
1392                // If a dead object exception was thrown -- fall through to
1393                // restart the application.
1394            }
1395        } else {
1396            // If this service runs in an isolated process, then each time
1397            // we call startProcessLocked() we will get a new isolated
1398            // process, starting another process if we are currently waiting
1399            // for a previous process to come up.  To deal with this, we store
1400            // in the service any current isolated process it is running in or
1401            // waiting to have come up.
1402            app = r.isolatedProc;
1403        }
1404
1405        // Not running -- get it started, and enqueue this service record
1406        // to be executed when the app comes up.
1407        if (app == null) {
1408            if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags,
1409                    "service", r.name, false, isolated, false)) == null) {
1410                String msg = "Unable to launch app "
1411                        + r.appInfo.packageName + "/"
1412                        + r.appInfo.uid + " for service "
1413                        + r.intent.getIntent() + ": process is bad";
1414                Slog.w(TAG, msg);
1415                bringDownServiceLocked(r);
1416                return msg;
1417            }
1418            if (isolated) {
1419                r.isolatedProc = app;
1420            }
1421        }
1422
1423        if (!mPendingServices.contains(r)) {
1424            mPendingServices.add(r);
1425        }
1426
1427        if (r.delayedStop) {
1428            // Oh and hey we've already been asked to stop!
1429            r.delayedStop = false;
1430            if (r.startRequested) {
1431                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Applying delayed stop (in bring up): " + r);
1432                stopServiceLocked(r);
1433            }
1434        }
1435
1436        return null;
1437    }
1438
1439    private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg) {
1440        for (int i=r.bindings.size()-1; i>=0; i--) {
1441            IntentBindRecord ibr = r.bindings.valueAt(i);
1442            if (!requestServiceBindingLocked(r, ibr, execInFg, false)) {
1443                break;
1444            }
1445        }
1446    }
1447
1448    private final void realStartServiceLocked(ServiceRecord r,
1449            ProcessRecord app, boolean execInFg) throws RemoteException {
1450        if (app.thread == null) {
1451            throw new RemoteException();
1452        }
1453        if (DEBUG_MU)
1454            Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid
1455                    + ", ProcessRecord.uid = " + app.uid);
1456        r.app = app;
1457        r.restartTime = r.lastActivity = SystemClock.uptimeMillis();
1458
1459        app.services.add(r);
1460        bumpServiceExecutingLocked(r, execInFg, "create");
1461        mAm.updateLruProcessLocked(app, false, null);
1462        mAm.updateOomAdjLocked();
1463
1464        boolean created = false;
1465        try {
1466            if (LOG_SERVICE_START_STOP) {
1467                String nameTerm;
1468                int lastPeriod = r.shortName.lastIndexOf('.');
1469                nameTerm = lastPeriod >= 0 ? r.shortName.substring(lastPeriod) : r.shortName;
1470                EventLogTags.writeAmCreateService(
1471                        r.userId, System.identityHashCode(r), nameTerm, r.app.uid, r.app.pid);
1472            }
1473            synchronized (r.stats.getBatteryStats()) {
1474                r.stats.startLaunchedLocked();
1475            }
1476            mAm.ensurePackageDexOpt(r.serviceInfo.packageName);
1477            app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE);
1478            app.thread.scheduleCreateService(r, r.serviceInfo,
1479                    mAm.compatibilityInfoForPackageLocked(r.serviceInfo.applicationInfo),
1480                    app.repProcState);
1481            r.postNotification();
1482            created = true;
1483        } catch (DeadObjectException e) {
1484            Slog.w(TAG, "Application dead when creating service " + r);
1485            mAm.appDiedLocked(app);
1486        } finally {
1487            if (!created) {
1488                app.services.remove(r);
1489                r.app = null;
1490                scheduleServiceRestartLocked(r, false);
1491                return;
1492            }
1493        }
1494
1495        requestServiceBindingsLocked(r, execInFg);
1496
1497        updateServiceClientActivitiesLocked(app, null, true);
1498
1499        // If the service is in the started state, and there are no
1500        // pending arguments, then fake up one so its onStartCommand() will
1501        // be called.
1502        if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {
1503            r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
1504                    null, null));
1505        }
1506
1507        sendServiceArgsLocked(r, execInFg, true);
1508
1509        if (r.delayed) {
1510            if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "REM FR DELAY LIST (new proc): " + r);
1511            getServiceMap(r.userId).mDelayedStartList.remove(r);
1512            r.delayed = false;
1513        }
1514
1515        if (r.delayedStop) {
1516            // Oh and hey we've already been asked to stop!
1517            r.delayedStop = false;
1518            if (r.startRequested) {
1519                if (DEBUG_DELAYED_STARTS) Slog.v(TAG, "Applying delayed stop (from start): " + r);
1520                stopServiceLocked(r);
1521            }
1522        }
1523    }
1524
1525    private final void sendServiceArgsLocked(ServiceRecord r, boolean execInFg,
1526            boolean oomAdjusted) {
1527        final int N = r.pendingStarts.size();
1528        if (N == 0) {
1529            return;
1530        }
1531
1532        while (r.pendingStarts.size() > 0) {
1533            try {
1534                ServiceRecord.StartItem si = r.pendingStarts.remove(0);
1535                if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: "
1536                        + r + " " + r.intent + " args=" + si.intent);
1537                if (si.intent == null && N > 1) {
1538                    // If somehow we got a dummy null intent in the middle,
1539                    // then skip it.  DO NOT skip a null intent when it is
1540                    // the only one in the list -- this is to support the
1541                    // onStartCommand(null) case.
1542                    continue;
1543                }
1544                si.deliveredTime = SystemClock.uptimeMillis();
1545                r.deliveredStarts.add(si);
1546                si.deliveryCount++;
1547                if (si.neededGrants != null) {
1548                    mAm.grantUriPermissionUncheckedFromIntentLocked(si.neededGrants,
1549                            si.getUriPermissionsLocked());
1550                }
1551                bumpServiceExecutingLocked(r, execInFg, "start");
1552                if (!oomAdjusted) {
1553                    oomAdjusted = true;
1554                    mAm.updateOomAdjLocked(r.app);
1555                }
1556                int flags = 0;
1557                if (si.deliveryCount > 1) {
1558                    flags |= Service.START_FLAG_RETRY;
1559                }
1560                if (si.doneExecutingCount > 0) {
1561                    flags |= Service.START_FLAG_REDELIVERY;
1562                }
1563                r.app.thread.scheduleServiceArgs(r, si.taskRemoved, si.id, flags, si.intent);
1564            } catch (RemoteException e) {
1565                // Remote process gone...  we'll let the normal cleanup take
1566                // care of this.
1567                if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r);
1568                break;
1569            } catch (Exception e) {
1570                Slog.w(TAG, "Unexpected exception", e);
1571                break;
1572            }
1573        }
1574    }
1575
1576    private final boolean isServiceNeeded(ServiceRecord r, boolean knowConn, boolean hasConn) {
1577        // Are we still explicitly being asked to run?
1578        if (r.startRequested) {
1579            return true;
1580        }
1581
1582        // Is someone still bound to us keepign us running?
1583        if (!knowConn) {
1584            hasConn = r.hasAutoCreateConnections();
1585        }
1586        if (hasConn) {
1587            return true;
1588        }
1589
1590        return false;
1591    }
1592
1593    private final void bringDownServiceIfNeededLocked(ServiceRecord r, boolean knowConn,
1594            boolean hasConn) {
1595        //Slog.i(TAG, "Bring down service:");
1596        //r.dump("  ");
1597
1598        if (isServiceNeeded(r, knowConn, hasConn)) {
1599            return;
1600        }
1601
1602        // Are we in the process of launching?
1603        if (mPendingServices.contains(r)) {
1604            return;
1605        }
1606
1607        bringDownServiceLocked(r);
1608    }
1609
1610    private final void bringDownServiceLocked(ServiceRecord r) {
1611        //Slog.i(TAG, "Bring down service:");
1612        //r.dump("  ");
1613
1614        // Report to all of the connections that the service is no longer
1615        // available.
1616        for (int conni=r.connections.size()-1; conni>=0; conni--) {
1617            ArrayList<ConnectionRecord> c = r.connections.valueAt(conni);
1618            for (int i=0; i<c.size(); i++) {
1619                ConnectionRecord cr = c.get(i);
1620                // There is still a connection to the service that is
1621                // being brought down.  Mark it as dead.
1622                cr.serviceDead = true;
1623                try {
1624                    cr.conn.connected(r.name, null);
1625                } catch (Exception e) {
1626                    Slog.w(TAG, "Failure disconnecting service " + r.name +
1627                          " to connection " + c.get(i).conn.asBinder() +
1628                          " (in " + c.get(i).binding.client.processName + ")", e);
1629                }
1630            }
1631        }
1632
1633        // Tell the service that it has been unbound.
1634        if (r.app != null && r.app.thread != null) {
1635            for (int i=r.bindings.size()-1; i>=0; i--) {
1636                IntentBindRecord ibr = r.bindings.valueAt(i);
1637                if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr
1638                        + ": hasBound=" + ibr.hasBound);
1639                if (ibr.hasBound) {
1640                    try {
1641                        bumpServiceExecutingLocked(r, false, "bring down unbind");
1642                        mAm.updateOomAdjLocked(r.app);
1643                        ibr.hasBound = false;
1644                        r.app.thread.scheduleUnbindService(r,
1645                                ibr.intent.getIntent());
1646                    } catch (Exception e) {
1647                        Slog.w(TAG, "Exception when unbinding service "
1648                                + r.shortName, e);
1649                        serviceProcessGoneLocked(r);
1650                    }
1651                }
1652            }
1653        }
1654
1655        if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent);
1656        r.destroyTime = SystemClock.uptimeMillis();
1657        if (LOG_SERVICE_START_STOP) {
1658            EventLogTags.writeAmDestroyService(
1659                    r.userId, System.identityHashCode(r), (r.app != null) ? r.app.pid : -1);
1660        }
1661
1662        final ServiceMap smap = getServiceMap(r.userId);
1663        smap.mServicesByName.remove(r.name);
1664        smap.mServicesByIntent.remove(r.intent);
1665        r.totalRestartCount = 0;
1666        unscheduleServiceRestartLocked(r, 0, true);
1667
1668        // Also make sure it is not on the pending list.
1669        for (int i=mPendingServices.size()-1; i>=0; i--) {
1670            if (mPendingServices.get(i) == r) {
1671                mPendingServices.remove(i);
1672                if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r);
1673            }
1674        }
1675
1676        r.cancelNotification();
1677        r.isForeground = false;
1678        r.foregroundId = 0;
1679        r.foregroundNoti = null;
1680
1681        // Clear start entries.
1682        r.clearDeliveredStartsLocked();
1683        r.pendingStarts.clear();
1684
1685        if (r.app != null) {
1686            synchronized (r.stats.getBatteryStats()) {
1687                r.stats.stopLaunchedLocked();
1688            }
1689            r.app.services.remove(r);
1690            if (r.app.thread != null) {
1691                updateServiceForegroundLocked(r.app, false);
1692                try {
1693                    bumpServiceExecutingLocked(r, false, "destroy");
1694                    mDestroyingServices.add(r);
1695                    mAm.updateOomAdjLocked(r.app);
1696                    r.app.thread.scheduleStopService(r);
1697                } catch (Exception e) {
1698                    Slog.w(TAG, "Exception when destroying service "
1699                            + r.shortName, e);
1700                    serviceProcessGoneLocked(r);
1701                }
1702            } else {
1703                if (DEBUG_SERVICE) Slog.v(
1704                    TAG, "Removed service that has no process: " + r);
1705            }
1706        } else {
1707            if (DEBUG_SERVICE) Slog.v(
1708                TAG, "Removed service that is not running: " + r);
1709        }
1710
1711        if (r.bindings.size() > 0) {
1712            r.bindings.clear();
1713        }
1714
1715        if (r.restarter instanceof ServiceRestarter) {
1716           ((ServiceRestarter)r.restarter).setService(null);
1717        }
1718
1719        int memFactor = mAm.mProcessStats.getMemFactorLocked();
1720        long now = SystemClock.uptimeMillis();
1721        if (r.tracker != null) {
1722            r.tracker.setStarted(false, memFactor, now);
1723            r.tracker.setBound(false, memFactor, now);
1724            if (r.executeNesting == 0) {
1725                r.tracker.clearCurrentOwner(r, false);
1726                r.tracker = null;
1727            }
1728        }
1729
1730        smap.ensureNotStartingBackground(r);
1731    }
1732
1733    void removeConnectionLocked(
1734        ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) {
1735        IBinder binder = c.conn.asBinder();
1736        AppBindRecord b = c.binding;
1737        ServiceRecord s = b.service;
1738        ArrayList<ConnectionRecord> clist = s.connections.get(binder);
1739        if (clist != null) {
1740            clist.remove(c);
1741            if (clist.size() == 0) {
1742                s.connections.remove(binder);
1743            }
1744        }
1745        b.connections.remove(c);
1746        if (c.activity != null && c.activity != skipAct) {
1747            if (c.activity.connections != null) {
1748                c.activity.connections.remove(c);
1749            }
1750        }
1751        if (b.client != skipApp) {
1752            b.client.connections.remove(c);
1753            if ((c.flags&Context.BIND_ABOVE_CLIENT) != 0) {
1754                b.client.updateHasAboveClientLocked();
1755            }
1756            if (s.app != null) {
1757                updateServiceClientActivitiesLocked(s.app, c, true);
1758            }
1759        }
1760        clist = mServiceConnections.get(binder);
1761        if (clist != null) {
1762            clist.remove(c);
1763            if (clist.size() == 0) {
1764                mServiceConnections.remove(binder);
1765            }
1766        }
1767
1768        mAm.stopAssociationLocked(b.client.uid, b.client.processName, s.appInfo.uid, s.name);
1769
1770        if (b.connections.size() == 0) {
1771            b.intent.apps.remove(b.client);
1772        }
1773
1774        if (!c.serviceDead) {
1775            if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent
1776                    + ": shouldUnbind=" + b.intent.hasBound);
1777            if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0
1778                    && b.intent.hasBound) {
1779                try {
1780                    bumpServiceExecutingLocked(s, false, "unbind");
1781                    if (b.client != s.app && (c.flags&Context.BIND_WAIVE_PRIORITY) == 0
1782                            && s.app.setProcState <= ActivityManager.PROCESS_STATE_RECEIVER) {
1783                        // If this service's process is not already in the cached list,
1784                        // then update it in the LRU list here because this may be causing
1785                        // it to go down there and we want it to start out near the top.
1786                        mAm.updateLruProcessLocked(s.app, false, null);
1787                    }
1788                    mAm.updateOomAdjLocked(s.app);
1789                    b.intent.hasBound = false;
1790                    // Assume the client doesn't want to know about a rebind;
1791                    // we will deal with that later if it asks for one.
1792                    b.intent.doRebind = false;
1793                    s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent());
1794                } catch (Exception e) {
1795                    Slog.w(TAG, "Exception when unbinding service " + s.shortName, e);
1796                    serviceProcessGoneLocked(s);
1797                }
1798            }
1799
1800            if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
1801                boolean hasAutoCreate = s.hasAutoCreateConnections();
1802                if (!hasAutoCreate) {
1803                    if (s.tracker != null) {
1804                        s.tracker.setBound(false, mAm.mProcessStats.getMemFactorLocked(),
1805                                SystemClock.uptimeMillis());
1806                    }
1807                }
1808                bringDownServiceIfNeededLocked(s, true, hasAutoCreate);
1809            }
1810        }
1811    }
1812
1813    void serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res) {
1814        boolean inDestroying = mDestroyingServices.contains(r);
1815        if (r != null) {
1816            if (type == 1) {
1817                // This is a call from a service start...  take care of
1818                // book-keeping.
1819                r.callStart = true;
1820                switch (res) {
1821                    case Service.START_STICKY_COMPATIBILITY:
1822                    case Service.START_STICKY: {
1823                        // We are done with the associated start arguments.
1824                        r.findDeliveredStart(startId, true);
1825                        // Don't stop if killed.
1826                        r.stopIfKilled = false;
1827                        break;
1828                    }
1829                    case Service.START_NOT_STICKY: {
1830                        // We are done with the associated start arguments.
1831                        r.findDeliveredStart(startId, true);
1832                        if (r.getLastStartId() == startId) {
1833                            // There is no more work, and this service
1834                            // doesn't want to hang around if killed.
1835                            r.stopIfKilled = true;
1836                        }
1837                        break;
1838                    }
1839                    case Service.START_REDELIVER_INTENT: {
1840                        // We'll keep this item until they explicitly
1841                        // call stop for it, but keep track of the fact
1842                        // that it was delivered.
1843                        ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
1844                        if (si != null) {
1845                            si.deliveryCount = 0;
1846                            si.doneExecutingCount++;
1847                            // Don't stop if killed.
1848                            r.stopIfKilled = true;
1849                        }
1850                        break;
1851                    }
1852                    case Service.START_TASK_REMOVED_COMPLETE: {
1853                        // Special processing for onTaskRemoved().  Don't
1854                        // impact normal onStartCommand() processing.
1855                        r.findDeliveredStart(startId, true);
1856                        break;
1857                    }
1858                    default:
1859                        throw new IllegalArgumentException(
1860                                "Unknown service start result: " + res);
1861                }
1862                if (res == Service.START_STICKY_COMPATIBILITY) {
1863                    r.callStart = false;
1864                }
1865            }
1866            final long origId = Binder.clearCallingIdentity();
1867            serviceDoneExecutingLocked(r, inDestroying, inDestroying);
1868            Binder.restoreCallingIdentity(origId);
1869        } else {
1870            Slog.w(TAG, "Done executing unknown service from pid "
1871                    + Binder.getCallingPid());
1872        }
1873    }
1874
1875    private void serviceProcessGoneLocked(ServiceRecord r) {
1876        if (r.tracker != null) {
1877            int memFactor = mAm.mProcessStats.getMemFactorLocked();
1878            long now = SystemClock.uptimeMillis();
1879            r.tracker.setExecuting(false, memFactor, now);
1880            r.tracker.setBound(false, memFactor, now);
1881            r.tracker.setStarted(false, memFactor, now);
1882        }
1883        serviceDoneExecutingLocked(r, true, true);
1884    }
1885
1886    private void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,
1887            boolean finishing) {
1888        if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r
1889                + ": nesting=" + r.executeNesting
1890                + ", inDestroying=" + inDestroying + ", app=" + r.app);
1891        else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName);
1892        r.executeNesting--;
1893        if (r.executeNesting <= 0) {
1894            if (r.app != null) {
1895                if (DEBUG_SERVICE) Slog.v(TAG,
1896                        "Nesting at 0 of " + r.shortName);
1897                r.app.execServicesFg = false;
1898                r.app.executingServices.remove(r);
1899                if (r.app.executingServices.size() == 0) {
1900                    if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG,
1901                            "No more executingServices of " + r.shortName);
1902                    mAm.mHandler.removeMessages(ActivityManagerService.SERVICE_TIMEOUT_MSG, r.app);
1903                } else if (r.executeFg) {
1904                    // Need to re-evaluate whether the app still needs to be in the foreground.
1905                    for (int i=r.app.executingServices.size()-1; i>=0; i--) {
1906                        if (r.app.executingServices.valueAt(i).executeFg) {
1907                            r.app.execServicesFg = true;
1908                            break;
1909                        }
1910                    }
1911                }
1912                if (inDestroying) {
1913                    if (DEBUG_SERVICE) Slog.v(TAG,
1914                            "doneExecuting remove destroying " + r);
1915                    mDestroyingServices.remove(r);
1916                    r.bindings.clear();
1917                }
1918                mAm.updateOomAdjLocked(r.app);
1919            }
1920            r.executeFg = false;
1921            if (r.tracker != null) {
1922                r.tracker.setExecuting(false, mAm.mProcessStats.getMemFactorLocked(),
1923                        SystemClock.uptimeMillis());
1924                if (finishing) {
1925                    r.tracker.clearCurrentOwner(r, false);
1926                    r.tracker = null;
1927                }
1928            }
1929            if (finishing) {
1930                if (r.app != null && !r.app.persistent) {
1931                    r.app.services.remove(r);
1932                }
1933                r.app = null;
1934            }
1935        }
1936    }
1937
1938    boolean attachApplicationLocked(ProcessRecord proc, String processName)
1939            throws RemoteException {
1940        boolean didSomething = false;
1941        // Collect any services that are waiting for this process to come up.
1942        if (mPendingServices.size() > 0) {
1943            ServiceRecord sr = null;
1944            try {
1945                for (int i=0; i<mPendingServices.size(); i++) {
1946                    sr = mPendingServices.get(i);
1947                    if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid
1948                            || !processName.equals(sr.processName))) {
1949                        continue;
1950                    }
1951
1952                    mPendingServices.remove(i);
1953                    i--;
1954                    proc.addPackage(sr.appInfo.packageName, sr.appInfo.versionCode,
1955                            mAm.mProcessStats);
1956                    realStartServiceLocked(sr, proc, sr.createdFromFg);
1957                    didSomething = true;
1958                }
1959            } catch (RemoteException e) {
1960                Slog.w(TAG, "Exception in new application when starting service "
1961                        + sr.shortName, e);
1962                throw e;
1963            }
1964        }
1965        // Also, if there are any services that are waiting to restart and
1966        // would run in this process, now is a good time to start them.  It would
1967        // be weird to bring up the process but arbitrarily not let the services
1968        // run at this point just because their restart time hasn't come up.
1969        if (mRestartingServices.size() > 0) {
1970            ServiceRecord sr = null;
1971            for (int i=0; i<mRestartingServices.size(); i++) {
1972                sr = mRestartingServices.get(i);
1973                if (proc != sr.isolatedProc && (proc.uid != sr.appInfo.uid
1974                        || !processName.equals(sr.processName))) {
1975                    continue;
1976                }
1977                mAm.mHandler.removeCallbacks(sr.restarter);
1978                mAm.mHandler.post(sr.restarter);
1979            }
1980        }
1981        return didSomething;
1982    }
1983
1984    void processStartTimedOutLocked(ProcessRecord proc) {
1985        for (int i=0; i<mPendingServices.size(); i++) {
1986            ServiceRecord sr = mPendingServices.get(i);
1987            if ((proc.uid == sr.appInfo.uid
1988                    && proc.processName.equals(sr.processName))
1989                    || sr.isolatedProc == proc) {
1990                Slog.w(TAG, "Forcing bringing down service: " + sr);
1991                sr.isolatedProc = null;
1992                mPendingServices.remove(i);
1993                i--;
1994                bringDownServiceLocked(sr);
1995            }
1996        }
1997    }
1998
1999    private boolean collectForceStopServicesLocked(String name, int userId,
2000            boolean evenPersistent, boolean doit,
2001            ArrayMap<ComponentName, ServiceRecord> services,
2002            ArrayList<ServiceRecord> result) {
2003        boolean didSomething = false;
2004        for (int i=0; i<services.size(); i++) {
2005            ServiceRecord service = services.valueAt(i);
2006            if ((name == null || service.packageName.equals(name))
2007                    && (service.app == null || evenPersistent || !service.app.persistent)) {
2008                if (!doit) {
2009                    return true;
2010                }
2011                didSomething = true;
2012                Slog.i(TAG, "  Force stopping service " + service);
2013                if (service.app != null) {
2014                    service.app.removed = true;
2015                    if (!service.app.persistent) {
2016                        service.app.services.remove(service);
2017                    }
2018                }
2019                service.app = null;
2020                service.isolatedProc = null;
2021                result.add(service);
2022            }
2023        }
2024        return didSomething;
2025    }
2026
2027    boolean forceStopLocked(String name, int userId, boolean evenPersistent, boolean doit) {
2028        boolean didSomething = false;
2029        ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
2030        if (userId == UserHandle.USER_ALL) {
2031            for (int i=0; i<mServiceMap.size(); i++) {
2032                didSomething |= collectForceStopServicesLocked(name, userId, evenPersistent,
2033                        doit, mServiceMap.valueAt(i).mServicesByName, services);
2034                if (!doit && didSomething) {
2035                    return true;
2036                }
2037            }
2038        } else {
2039            ServiceMap smap = mServiceMap.get(userId);
2040            if (smap != null) {
2041                ArrayMap<ComponentName, ServiceRecord> items = smap.mServicesByName;
2042                didSomething = collectForceStopServicesLocked(name, userId, evenPersistent,
2043                        doit, items, services);
2044            }
2045        }
2046
2047        int N = services.size();
2048        for (int i=0; i<N; i++) {
2049            bringDownServiceLocked(services.get(i));
2050        }
2051        return didSomething;
2052    }
2053
2054    void cleanUpRemovedTaskLocked(TaskRecord tr, ComponentName component, Intent baseIntent) {
2055        ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
2056        ArrayMap<ComponentName, ServiceRecord> alls = getServices(tr.userId);
2057        for (int i=0; i<alls.size(); i++) {
2058            ServiceRecord sr = alls.valueAt(i);
2059            if (sr.packageName.equals(component.getPackageName())) {
2060                services.add(sr);
2061            }
2062        }
2063
2064        // Take care of any running services associated with the app.
2065        for (int i=0; i<services.size(); i++) {
2066            ServiceRecord sr = services.get(i);
2067            if (sr.startRequested) {
2068                if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) {
2069                    Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task");
2070                    stopServiceLocked(sr);
2071                } else {
2072                    sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true,
2073                            sr.makeNextStartId(), baseIntent, null));
2074                    if (sr.app != null && sr.app.thread != null) {
2075                        // We always run in the foreground, since this is called as
2076                        // part of the "remove task" UI operation.
2077                        sendServiceArgsLocked(sr, true, false);
2078                    }
2079                }
2080            }
2081        }
2082    }
2083
2084    final void killServicesLocked(ProcessRecord app, boolean allowRestart) {
2085        // Report disconnected services.
2086        if (false) {
2087            // XXX we are letting the client link to the service for
2088            // death notifications.
2089            if (app.services.size() > 0) {
2090                Iterator<ServiceRecord> it = app.services.iterator();
2091                while (it.hasNext()) {
2092                    ServiceRecord r = it.next();
2093                    for (int conni=r.connections.size()-1; conni>=0; conni--) {
2094                        ArrayList<ConnectionRecord> cl = r.connections.valueAt(conni);
2095                        for (int i=0; i<cl.size(); i++) {
2096                            ConnectionRecord c = cl.get(i);
2097                            if (c.binding.client != app) {
2098                                try {
2099                                    //c.conn.connected(r.className, null);
2100                                } catch (Exception e) {
2101                                    // todo: this should be asynchronous!
2102                                    Slog.w(TAG, "Exception thrown disconnected servce "
2103                                          + r.shortName
2104                                          + " from app " + app.processName, e);
2105                                }
2106                            }
2107                        }
2108                    }
2109                }
2110            }
2111        }
2112
2113        // First clear app state from services.
2114        for (int i=app.services.size()-1; i>=0; i--) {
2115            ServiceRecord sr = app.services.valueAt(i);
2116            synchronized (sr.stats.getBatteryStats()) {
2117                sr.stats.stopLaunchedLocked();
2118            }
2119            if (sr.app != app && sr.app != null && !sr.app.persistent) {
2120                sr.app.services.remove(sr);
2121            }
2122            sr.app = null;
2123            sr.isolatedProc = null;
2124            sr.executeNesting = 0;
2125            sr.forceClearTracker();
2126            if (mDestroyingServices.remove(sr)) {
2127                if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove destroying " + sr);
2128            }
2129
2130            final int numClients = sr.bindings.size();
2131            for (int bindingi=numClients-1; bindingi>=0; bindingi--) {
2132                IntentBindRecord b = sr.bindings.valueAt(bindingi);
2133                if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b
2134                        + ": shouldUnbind=" + b.hasBound);
2135                b.binder = null;
2136                b.requested = b.received = b.hasBound = false;
2137                // If this binding is coming from a cached process and is asking to keep
2138                // the service created, then we'll kill the cached process as well -- we
2139                // don't want to be thrashing around restarting processes that are only
2140                // there to be cached.
2141                for (int appi=b.apps.size()-1; appi>=0; appi--) {
2142                    final ProcessRecord proc = b.apps.keyAt(appi);
2143                    // If the process is already gone, skip it.
2144                    if (proc.killedByAm || proc.thread == null) {
2145                        continue;
2146                    }
2147                    // Only do this for processes that have an auto-create binding;
2148                    // otherwise the binding can be left, because it won't cause the
2149                    // service to restart.
2150                    final AppBindRecord abind = b.apps.valueAt(appi);
2151                    boolean hasCreate = false;
2152                    for (int conni=abind.connections.size()-1; conni>=0; conni--) {
2153                        ConnectionRecord conn = abind.connections.valueAt(conni);
2154                        if ((conn.flags&(Context.BIND_AUTO_CREATE|Context.BIND_ALLOW_OOM_MANAGEMENT
2155                                |Context.BIND_WAIVE_PRIORITY)) == Context.BIND_AUTO_CREATE) {
2156                            hasCreate = true;
2157                            break;
2158                        }
2159                    }
2160                    if (!hasCreate) {
2161                        continue;
2162                    }
2163                    // XXX turned off for now until we have more time to get a better policy.
2164                    if (false && proc != null && !proc.persistent && proc.thread != null
2165                            && proc.pid != 0 && proc.pid != ActivityManagerService.MY_PID
2166                            && proc.setProcState >= ActivityManager.PROCESS_STATE_LAST_ACTIVITY) {
2167                        proc.kill("bound to service " + sr.name.flattenToShortString()
2168                                + " in dying proc " + (app != null ? app.processName : "??"), true);
2169                    }
2170                }
2171            }
2172        }
2173
2174        // Clean up any connections this application has to other services.
2175        for (int i=app.connections.size()-1; i>=0; i--) {
2176            ConnectionRecord r = app.connections.valueAt(i);
2177            removeConnectionLocked(r, app, null);
2178        }
2179        updateServiceConnectionActivitiesLocked(app);
2180        app.connections.clear();
2181
2182        ServiceMap smap = getServiceMap(app.userId);
2183
2184        // Now do remaining service cleanup.
2185        for (int i=app.services.size()-1; i>=0; i--) {
2186            ServiceRecord sr = app.services.valueAt(i);
2187
2188            // Unless the process is persistent, this process record is going away,
2189            // so make sure the service is cleaned out of it.
2190            if (!app.persistent) {
2191                app.services.removeAt(i);
2192            }
2193
2194            // Sanity check: if the service listed for the app is not one
2195            // we actually are maintaining, just let it drop.
2196            final ServiceRecord curRec = smap.mServicesByName.get(sr.name);
2197            if (curRec != sr) {
2198                if (curRec != null) {
2199                    Slog.wtf(TAG, "Service " + sr + " in process " + app
2200                            + " not same as in map: " + curRec);
2201                }
2202                continue;
2203            }
2204
2205            // Any services running in the application may need to be placed
2206            // back in the pending list.
2207            if (allowRestart && sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags
2208                    &ApplicationInfo.FLAG_PERSISTENT) == 0) {
2209                Slog.w(TAG, "Service crashed " + sr.crashCount
2210                        + " times, stopping: " + sr);
2211                EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH,
2212                        sr.userId, sr.crashCount, sr.shortName, app.pid);
2213                bringDownServiceLocked(sr);
2214            } else if (!allowRestart) {
2215                bringDownServiceLocked(sr);
2216            } else {
2217                boolean canceled = scheduleServiceRestartLocked(sr, true);
2218
2219                // Should the service remain running?  Note that in the
2220                // extreme case of so many attempts to deliver a command
2221                // that it failed we also will stop it here.
2222                if (sr.startRequested && (sr.stopIfKilled || canceled)) {
2223                    if (sr.pendingStarts.size() == 0) {
2224                        sr.startRequested = false;
2225                        if (sr.tracker != null) {
2226                            sr.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(),
2227                                    SystemClock.uptimeMillis());
2228                        }
2229                        if (!sr.hasAutoCreateConnections()) {
2230                            // Whoops, no reason to restart!
2231                            bringDownServiceLocked(sr);
2232                        }
2233                    }
2234                }
2235            }
2236        }
2237
2238        if (!allowRestart) {
2239            app.services.clear();
2240
2241            // Make sure there are no more restarting services for this process.
2242            for (int i=mRestartingServices.size()-1; i>=0; i--) {
2243                ServiceRecord r = mRestartingServices.get(i);
2244                if (r.processName.equals(app.processName) &&
2245                        r.serviceInfo.applicationInfo.uid == app.info.uid) {
2246                    mRestartingServices.remove(i);
2247                    clearRestartingIfNeededLocked(r);
2248                }
2249            }
2250            for (int i=mPendingServices.size()-1; i>=0; i--) {
2251                ServiceRecord r = mPendingServices.get(i);
2252                if (r.processName.equals(app.processName) &&
2253                        r.serviceInfo.applicationInfo.uid == app.info.uid) {
2254                    mPendingServices.remove(i);
2255                }
2256            }
2257        }
2258
2259        // Make sure we have no more records on the stopping list.
2260        int i = mDestroyingServices.size();
2261        while (i > 0) {
2262            i--;
2263            ServiceRecord sr = mDestroyingServices.get(i);
2264            if (sr.app == app) {
2265                sr.forceClearTracker();
2266                mDestroyingServices.remove(i);
2267                if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove destroying " + sr);
2268            }
2269        }
2270
2271        app.executingServices.clear();
2272    }
2273
2274    ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) {
2275        ActivityManager.RunningServiceInfo info =
2276            new ActivityManager.RunningServiceInfo();
2277        info.service = r.name;
2278        if (r.app != null) {
2279            info.pid = r.app.pid;
2280        }
2281        info.uid = r.appInfo.uid;
2282        info.process = r.processName;
2283        info.foreground = r.isForeground;
2284        info.activeSince = r.createTime;
2285        info.started = r.startRequested;
2286        info.clientCount = r.connections.size();
2287        info.crashCount = r.crashCount;
2288        info.lastActivityTime = r.lastActivity;
2289        if (r.isForeground) {
2290            info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND;
2291        }
2292        if (r.startRequested) {
2293            info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED;
2294        }
2295        if (r.app != null && r.app.pid == ActivityManagerService.MY_PID) {
2296            info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS;
2297        }
2298        if (r.app != null && r.app.persistent) {
2299            info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS;
2300        }
2301
2302        for (int conni=r.connections.size()-1; conni>=0; conni--) {
2303            ArrayList<ConnectionRecord> connl = r.connections.valueAt(conni);
2304            for (int i=0; i<connl.size(); i++) {
2305                ConnectionRecord conn = connl.get(i);
2306                if (conn.clientLabel != 0) {
2307                    info.clientPackage = conn.binding.client.info.packageName;
2308                    info.clientLabel = conn.clientLabel;
2309                    return info;
2310                }
2311            }
2312        }
2313        return info;
2314    }
2315
2316    List<ActivityManager.RunningServiceInfo> getRunningServiceInfoLocked(int maxNum,
2317            int flags) {
2318        ArrayList<ActivityManager.RunningServiceInfo> res
2319                = new ArrayList<ActivityManager.RunningServiceInfo>();
2320
2321        final int uid = Binder.getCallingUid();
2322        final long ident = Binder.clearCallingIdentity();
2323        try {
2324            if (ActivityManager.checkUidPermission(
2325                    android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2326                    uid) == PackageManager.PERMISSION_GRANTED) {
2327                int[] users = mAm.getUsersLocked();
2328                for (int ui=0; ui<users.length && res.size() < maxNum; ui++) {
2329                    ArrayMap<ComponentName, ServiceRecord> alls = getServices(users[ui]);
2330                    for (int i=0; i<alls.size() && res.size() < maxNum; i++) {
2331                        ServiceRecord sr = alls.valueAt(i);
2332                        res.add(makeRunningServiceInfoLocked(sr));
2333                    }
2334                }
2335
2336                for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) {
2337                    ServiceRecord r = mRestartingServices.get(i);
2338                    ActivityManager.RunningServiceInfo info =
2339                            makeRunningServiceInfoLocked(r);
2340                    info.restarting = r.nextRestartTime;
2341                    res.add(info);
2342                }
2343            } else {
2344                int userId = UserHandle.getUserId(uid);
2345                ArrayMap<ComponentName, ServiceRecord> alls = getServices(userId);
2346                for (int i=0; i<alls.size() && res.size() < maxNum; i++) {
2347                    ServiceRecord sr = alls.valueAt(i);
2348                    res.add(makeRunningServiceInfoLocked(sr));
2349                }
2350
2351                for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) {
2352                    ServiceRecord r = mRestartingServices.get(i);
2353                    if (r.userId == userId) {
2354                        ActivityManager.RunningServiceInfo info =
2355                                makeRunningServiceInfoLocked(r);
2356                        info.restarting = r.nextRestartTime;
2357                        res.add(info);
2358                    }
2359                }
2360            }
2361        } finally {
2362            Binder.restoreCallingIdentity(ident);
2363        }
2364
2365        return res;
2366    }
2367
2368    public PendingIntent getRunningServiceControlPanelLocked(ComponentName name) {
2369        int userId = UserHandle.getUserId(Binder.getCallingUid());
2370        ServiceRecord r = getServiceByName(name, userId);
2371        if (r != null) {
2372            for (int conni=r.connections.size()-1; conni>=0; conni--) {
2373                ArrayList<ConnectionRecord> conn = r.connections.valueAt(conni);
2374                for (int i=0; i<conn.size(); i++) {
2375                    if (conn.get(i).clientIntent != null) {
2376                        return conn.get(i).clientIntent;
2377                    }
2378                }
2379            }
2380        }
2381        return null;
2382    }
2383
2384    void serviceTimeout(ProcessRecord proc) {
2385        String anrMessage = null;
2386
2387        synchronized(mAm) {
2388            if (proc.executingServices.size() == 0 || proc.thread == null) {
2389                return;
2390            }
2391            final long now = SystemClock.uptimeMillis();
2392            final long maxTime =  now -
2393                    (proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);
2394            ServiceRecord timeout = null;
2395            long nextTime = 0;
2396            for (int i=proc.executingServices.size()-1; i>=0; i--) {
2397                ServiceRecord sr = proc.executingServices.valueAt(i);
2398                if (sr.executingStart < maxTime) {
2399                    timeout = sr;
2400                    break;
2401                }
2402                if (sr.executingStart > nextTime) {
2403                    nextTime = sr.executingStart;
2404                }
2405            }
2406            if (timeout != null && mAm.mLruProcesses.contains(proc)) {
2407                Slog.w(TAG, "Timeout executing service: " + timeout);
2408                StringWriter sw = new StringWriter();
2409                PrintWriter pw = new FastPrintWriter(sw, false, 1024);
2410                pw.println(timeout);
2411                timeout.dump(pw, "    ");
2412                pw.close();
2413                mLastAnrDump = sw.toString();
2414                mAm.mHandler.removeCallbacks(mLastAnrDumpClearer);
2415                mAm.mHandler.postDelayed(mLastAnrDumpClearer, LAST_ANR_LIFETIME_DURATION_MSECS);
2416                anrMessage = "executing service " + timeout.shortName;
2417            } else {
2418                Message msg = mAm.mHandler.obtainMessage(
2419                        ActivityManagerService.SERVICE_TIMEOUT_MSG);
2420                msg.obj = proc;
2421                mAm.mHandler.sendMessageAtTime(msg, proc.execServicesFg
2422                        ? (nextTime+SERVICE_TIMEOUT) : (nextTime + SERVICE_BACKGROUND_TIMEOUT));
2423            }
2424        }
2425
2426        if (anrMessage != null) {
2427            mAm.appNotResponding(proc, null, null, false, anrMessage);
2428        }
2429    }
2430
2431    void scheduleServiceTimeoutLocked(ProcessRecord proc) {
2432        if (proc.executingServices.size() == 0 || proc.thread == null) {
2433            return;
2434        }
2435        long now = SystemClock.uptimeMillis();
2436        Message msg = mAm.mHandler.obtainMessage(
2437                ActivityManagerService.SERVICE_TIMEOUT_MSG);
2438        msg.obj = proc;
2439        mAm.mHandler.sendMessageAtTime(msg,
2440                proc.execServicesFg ? (now+SERVICE_TIMEOUT) : (now+ SERVICE_BACKGROUND_TIMEOUT));
2441    }
2442
2443    /**
2444     * Prints a list of ServiceRecords (dumpsys activity services)
2445     */
2446    void dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
2447            int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
2448        boolean needSep = false;
2449        boolean printedAnything = false;
2450
2451        ItemMatcher matcher = new ItemMatcher();
2452        matcher.build(args, opti);
2453
2454        pw.println("ACTIVITY MANAGER SERVICES (dumpsys activity services)");
2455        try {
2456            if (mLastAnrDump != null) {
2457                pw.println("  Last ANR service:");
2458                pw.print(mLastAnrDump);
2459                pw.println();
2460            }
2461            int[] users = mAm.getUsersLocked();
2462            for (int user : users) {
2463                ServiceMap smap = getServiceMap(user);
2464                boolean printed = false;
2465                if (smap.mServicesByName.size() > 0) {
2466                    long nowReal = SystemClock.elapsedRealtime();
2467                    needSep = false;
2468                    for (int si=0; si<smap.mServicesByName.size(); si++) {
2469                        ServiceRecord r = smap.mServicesByName.valueAt(si);
2470                        if (!matcher.match(r, r.name)) {
2471                            continue;
2472                        }
2473                        if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2474                            continue;
2475                        }
2476                        if (!printed) {
2477                            if (printedAnything) {
2478                                pw.println();
2479                            }
2480                            pw.println("  User " + user + " active services:");
2481                            printed = true;
2482                        }
2483                        printedAnything = true;
2484                        if (needSep) {
2485                            pw.println();
2486                        }
2487                        pw.print("  * ");
2488                        pw.println(r);
2489                        if (dumpAll) {
2490                            r.dump(pw, "    ");
2491                            needSep = true;
2492                        } else {
2493                            pw.print("    app=");
2494                            pw.println(r.app);
2495                            pw.print("    created=");
2496                            TimeUtils.formatDuration(r.createTime, nowReal, pw);
2497                            pw.print(" started=");
2498                            pw.print(r.startRequested);
2499                            pw.print(" connections=");
2500                            pw.println(r.connections.size());
2501                            if (r.connections.size() > 0) {
2502                                pw.println("    Connections:");
2503                                for (int conni=0; conni<r.connections.size(); conni++) {
2504                                    ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni);
2505                                    for (int i = 0; i < clist.size(); i++) {
2506                                        ConnectionRecord conn = clist.get(i);
2507                                        pw.print("      ");
2508                                        pw.print(conn.binding.intent.intent.getIntent()
2509                                                .toShortString(false, false, false, false));
2510                                        pw.print(" -> ");
2511                                        ProcessRecord proc = conn.binding.client;
2512                                        pw.println(proc != null ? proc.toShortString() : "null");
2513                                    }
2514                                }
2515                            }
2516                        }
2517                        if (dumpClient && r.app != null && r.app.thread != null) {
2518                            pw.println("    Client:");
2519                            pw.flush();
2520                            try {
2521                                TransferPipe tp = new TransferPipe();
2522                                try {
2523                                    r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(),
2524                                            r, args);
2525                                    tp.setBufferPrefix("      ");
2526                                    // Short timeout, since blocking here can
2527                                    // deadlock with the application.
2528                                    tp.go(fd, 2000);
2529                                } finally {
2530                                    tp.kill();
2531                                }
2532                            } catch (IOException e) {
2533                                pw.println("      Failure while dumping the service: " + e);
2534                            } catch (RemoteException e) {
2535                                pw.println("      Got a RemoteException while dumping the service");
2536                            }
2537                            needSep = true;
2538                        }
2539                    }
2540                    needSep |= printed;
2541                }
2542                printed = false;
2543                for (int si=0, SN=smap.mDelayedStartList.size(); si<SN; si++) {
2544                    ServiceRecord r = smap.mDelayedStartList.get(si);
2545                    if (!matcher.match(r, r.name)) {
2546                        continue;
2547                    }
2548                    if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2549                        continue;
2550                    }
2551                    if (!printed) {
2552                        if (printedAnything) {
2553                            pw.println();
2554                        }
2555                        pw.println("  User " + user + " delayed start services:");
2556                        printed = true;
2557                    }
2558                    printedAnything = true;
2559                    pw.print("  * Delayed start "); pw.println(r);
2560                }
2561                printed = false;
2562                for (int si=0, SN=smap.mStartingBackground.size(); si<SN; si++) {
2563                    ServiceRecord r = smap.mStartingBackground.get(si);
2564                    if (!matcher.match(r, r.name)) {
2565                        continue;
2566                    }
2567                    if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2568                        continue;
2569                    }
2570                    if (!printed) {
2571                        if (printedAnything) {
2572                            pw.println();
2573                        }
2574                        pw.println("  User " + user + " starting in background:");
2575                        printed = true;
2576                    }
2577                    printedAnything = true;
2578                    pw.print("  * Starting bg "); pw.println(r);
2579                }
2580            }
2581        } catch (Exception e) {
2582            Slog.w(TAG, "Exception in dumpServicesLocked", e);
2583        }
2584
2585        if (mPendingServices.size() > 0) {
2586            boolean printed = false;
2587            for (int i=0; i<mPendingServices.size(); i++) {
2588                ServiceRecord r = mPendingServices.get(i);
2589                if (!matcher.match(r, r.name)) {
2590                    continue;
2591                }
2592                if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2593                    continue;
2594                }
2595                printedAnything = true;
2596                if (!printed) {
2597                    if (needSep) pw.println();
2598                    needSep = true;
2599                    pw.println("  Pending services:");
2600                    printed = true;
2601                }
2602                pw.print("  * Pending "); pw.println(r);
2603                r.dump(pw, "    ");
2604            }
2605            needSep = true;
2606        }
2607
2608        if (mRestartingServices.size() > 0) {
2609            boolean printed = false;
2610            for (int i=0; i<mRestartingServices.size(); i++) {
2611                ServiceRecord r = mRestartingServices.get(i);
2612                if (!matcher.match(r, r.name)) {
2613                    continue;
2614                }
2615                if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2616                    continue;
2617                }
2618                printedAnything = true;
2619                if (!printed) {
2620                    if (needSep) pw.println();
2621                    needSep = true;
2622                    pw.println("  Restarting services:");
2623                    printed = true;
2624                }
2625                pw.print("  * Restarting "); pw.println(r);
2626                r.dump(pw, "    ");
2627            }
2628            needSep = true;
2629        }
2630
2631        if (mDestroyingServices.size() > 0) {
2632            boolean printed = false;
2633            for (int i=0; i< mDestroyingServices.size(); i++) {
2634                ServiceRecord r = mDestroyingServices.get(i);
2635                if (!matcher.match(r, r.name)) {
2636                    continue;
2637                }
2638                if (dumpPackage != null && !dumpPackage.equals(r.appInfo.packageName)) {
2639                    continue;
2640                }
2641                printedAnything = true;
2642                if (!printed) {
2643                    if (needSep) pw.println();
2644                    needSep = true;
2645                    pw.println("  Destroying services:");
2646                    printed = true;
2647                }
2648                pw.print("  * Destroy "); pw.println(r);
2649                r.dump(pw, "    ");
2650            }
2651            needSep = true;
2652        }
2653
2654        if (dumpAll) {
2655            boolean printed = false;
2656            for (int ic=0; ic<mServiceConnections.size(); ic++) {
2657                ArrayList<ConnectionRecord> r = mServiceConnections.valueAt(ic);
2658                for (int i=0; i<r.size(); i++) {
2659                    ConnectionRecord cr = r.get(i);
2660                    if (!matcher.match(cr.binding.service, cr.binding.service.name)) {
2661                        continue;
2662                    }
2663                    if (dumpPackage != null && (cr.binding.client == null
2664                            || !dumpPackage.equals(cr.binding.client.info.packageName))) {
2665                        continue;
2666                    }
2667                    printedAnything = true;
2668                    if (!printed) {
2669                        if (needSep) pw.println();
2670                        needSep = true;
2671                        pw.println("  Connection bindings to services:");
2672                        printed = true;
2673                    }
2674                    pw.print("  * "); pw.println(cr);
2675                    cr.dump(pw, "    ");
2676                }
2677            }
2678        }
2679
2680        if (!printedAnything) {
2681            pw.println("  (nothing)");
2682        }
2683    }
2684
2685    /**
2686     * There are three ways to call this:
2687     *  - no service specified: dump all the services
2688     *  - a flattened component name that matched an existing service was specified as the
2689     *    first arg: dump that one service
2690     *  - the first arg isn't the flattened component name of an existing service:
2691     *    dump all services whose component contains the first arg as a substring
2692     */
2693    protected boolean dumpService(FileDescriptor fd, PrintWriter pw, String name, String[] args,
2694            int opti, boolean dumpAll) {
2695        ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
2696
2697        synchronized (mAm) {
2698            int[] users = mAm.getUsersLocked();
2699            if ("all".equals(name)) {
2700                for (int user : users) {
2701                    ServiceMap smap = mServiceMap.get(user);
2702                    if (smap == null) {
2703                        continue;
2704                    }
2705                    ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName;
2706                    for (int i=0; i<alls.size(); i++) {
2707                        ServiceRecord r1 = alls.valueAt(i);
2708                        services.add(r1);
2709                    }
2710                }
2711            } else {
2712                ComponentName componentName = name != null
2713                        ? ComponentName.unflattenFromString(name) : null;
2714                int objectId = 0;
2715                if (componentName == null) {
2716                    // Not a '/' separated full component name; maybe an object ID?
2717                    try {
2718                        objectId = Integer.parseInt(name, 16);
2719                        name = null;
2720                        componentName = null;
2721                    } catch (RuntimeException e) {
2722                    }
2723                }
2724
2725                for (int user : users) {
2726                    ServiceMap smap = mServiceMap.get(user);
2727                    if (smap == null) {
2728                        continue;
2729                    }
2730                    ArrayMap<ComponentName, ServiceRecord> alls = smap.mServicesByName;
2731                    for (int i=0; i<alls.size(); i++) {
2732                        ServiceRecord r1 = alls.valueAt(i);
2733                        if (componentName != null) {
2734                            if (r1.name.equals(componentName)) {
2735                                services.add(r1);
2736                            }
2737                        } else if (name != null) {
2738                            if (r1.name.flattenToString().contains(name)) {
2739                                services.add(r1);
2740                            }
2741                        } else if (System.identityHashCode(r1) == objectId) {
2742                            services.add(r1);
2743                        }
2744                    }
2745                }
2746            }
2747        }
2748
2749        if (services.size() <= 0) {
2750            return false;
2751        }
2752
2753        boolean needSep = false;
2754        for (int i=0; i<services.size(); i++) {
2755            if (needSep) {
2756                pw.println();
2757            }
2758            needSep = true;
2759            dumpService("", fd, pw, services.get(i), args, dumpAll);
2760        }
2761        return true;
2762    }
2763
2764    /**
2765     * Invokes IApplicationThread.dumpService() on the thread of the specified service if
2766     * there is a thread associated with the service.
2767     */
2768    private void dumpService(String prefix, FileDescriptor fd, PrintWriter pw,
2769            final ServiceRecord r, String[] args, boolean dumpAll) {
2770        String innerPrefix = prefix + "  ";
2771        synchronized (mAm) {
2772            pw.print(prefix); pw.print("SERVICE ");
2773                    pw.print(r.shortName); pw.print(" ");
2774                    pw.print(Integer.toHexString(System.identityHashCode(r)));
2775                    pw.print(" pid=");
2776                    if (r.app != null) pw.println(r.app.pid);
2777                    else pw.println("(not running)");
2778            if (dumpAll) {
2779                r.dump(pw, innerPrefix);
2780            }
2781        }
2782        if (r.app != null && r.app.thread != null) {
2783            pw.print(prefix); pw.println("  Client:");
2784            pw.flush();
2785            try {
2786                TransferPipe tp = new TransferPipe();
2787                try {
2788                    r.app.thread.dumpService(tp.getWriteFd().getFileDescriptor(), r, args);
2789                    tp.setBufferPrefix(prefix + "    ");
2790                    tp.go(fd);
2791                } finally {
2792                    tp.kill();
2793                }
2794            } catch (IOException e) {
2795                pw.println(prefix + "    Failure while dumping the service: " + e);
2796            } catch (RemoteException e) {
2797                pw.println(prefix + "    Got a RemoteException while dumping the service");
2798            }
2799        }
2800    }
2801
2802}
2803