Home | History | Annotate | Download | only in binder
      1 /*
      2  * Copyright (C) 2009 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 
     17 #include <stdint.h>
     18 #include <utils/Log.h>
     19 #include <binder/IPCThreadState.h>
     20 #include <binder/IServiceManager.h>
     21 #include <binder/Permission.h>
     22 
     23 namespace android {
     24 // ---------------------------------------------------------------------------
     25 
     26 Permission::Permission(char const* name)
     27     : mPermissionName(name), mPid(getpid())
     28 {
     29 }
     30 
     31 Permission::Permission(const String16& name)
     32     : mPermissionName(name), mPid(getpid())
     33 {
     34 }
     35 
     36 Permission::Permission(const Permission& rhs)
     37     : mPermissionName(rhs.mPermissionName),
     38     mGranted(rhs.mGranted),
     39     mPid(rhs.mPid)
     40 {
     41 }
     42 
     43 Permission::~Permission()
     44 {
     45 }
     46 
     47 bool Permission::operator < (const Permission& rhs) const
     48 {
     49     return mPermissionName < rhs.mPermissionName;
     50 }
     51 
     52 bool Permission::checkCalling() const
     53 {
     54     IPCThreadState* ipcState = IPCThreadState::self();
     55     pid_t pid = ipcState->getCallingPid();
     56     uid_t uid = ipcState->getCallingUid();
     57     return doCheckPermission(pid, uid);
     58 }
     59 
     60 bool Permission::check(pid_t pid, uid_t uid) const
     61 {
     62     return doCheckPermission(pid, uid);
     63 }
     64 
     65 bool Permission::doCheckPermission(pid_t pid, uid_t uid) const
     66 {
     67     if ((uid == 0) || (pid == mPid)) {
     68         // root and ourselves is always okay
     69         return true;
     70     } else {
     71         // see if we already granted this permission for this uid
     72         Mutex::Autolock _l(mLock);
     73         if (mGranted.indexOf(uid) >= 0)
     74             return true;
     75     }
     76 
     77     bool granted = checkPermission(mPermissionName, pid, uid);
     78     if (granted) {
     79         Mutex::Autolock _l(mLock);
     80         // no need to check again, the old item will be replaced if it is
     81         // already there.
     82         mGranted.add(uid);
     83     }
     84     return granted;
     85 }
     86 
     87 // ---------------------------------------------------------------------------
     88 }; // namespace android
     89