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