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