Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68d6fd5a authored by Jared Duke's avatar Jared Duke
Browse files

Remove SerialService JNI layer

This JNI layer is only used to proxy a basic `open` syscall, which we
can now access directly from `Os`. Simplify the implementation to use
this directly, preserving null return semantics for the
ParcelFileDescriptor when the load fails.

Bug: 375264322
Test: SerialChat + presubmit
Flag: EXEMPT refactor
Change-Id: I35296ea33d04ac1372dc8890fc2c6b58cbe7bb28
parent 7ba00f0c
Loading
Loading
Loading
Loading
+19 −2
Original line number Original line Diff line number Diff line
@@ -18,22 +18,30 @@ package com.android.server;


import android.annotation.EnforcePermission;
import android.annotation.EnforcePermission;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Context;
import android.hardware.ISerialManager;
import android.hardware.ISerialManager;
import android.hardware.SerialManagerInternal;
import android.hardware.SerialManagerInternal;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.PermissionEnforcer;
import android.os.PermissionEnforcer;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Slog;


import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;


import java.io.File;
import java.io.File;
import java.io.FileDescriptor;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashMap;
import java.util.function.Supplier;
import java.util.function.Supplier;


@android.ravenwood.annotation.RavenwoodKeepWholeClass
@android.ravenwood.annotation.RavenwoodKeepWholeClass
public class SerialService extends ISerialManager.Stub {
public class SerialService extends ISerialManager.Stub {
    private static final String TAG = "SerialService";

    private final Context mContext;
    private final Context mContext;


    @GuardedBy("mSerialPorts")
    @GuardedBy("mSerialPorts")
@@ -50,7 +58,7 @@ public class SerialService extends ISerialManager.Stub {
            final String[] serialPorts = getSerialPorts(context);
            final String[] serialPorts = getSerialPorts(context);
            for (String serialPort : serialPorts) {
            for (String serialPort : serialPorts) {
                mSerialPorts.put(serialPort, () -> {
                mSerialPorts.put(serialPort, () -> {
                    return native_open(serialPort);
                    return tryOpen(serialPort);
                });
                });
            }
            }
        }
        }
@@ -135,5 +143,14 @@ public class SerialService extends ISerialManager.Stub {
        }
        }
    };
    };


    private native ParcelFileDescriptor native_open(String path);
    private static @Nullable ParcelFileDescriptor tryOpen(String path) {
        try {
            FileDescriptor fd = Os.open(path, OsConstants.O_RDWR | OsConstants.O_NOCTTY, 0);
            return new ParcelFileDescriptor(fd);
        } catch (ErrnoException e) {
            Slog.e(TAG, "Could not open: " + path, e);
            // We return null to preserve API semantics from earlier implementation variants.
            return null;
        }
    }
}
}
+0 −1
Original line number Original line Diff line number Diff line
@@ -55,7 +55,6 @@ cc_library_static {
        "com_android_server_powerstats_PowerStatsService.cpp",
        "com_android_server_powerstats_PowerStatsService.cpp",
        "com_android_server_power_stats_CpuPowerStatsCollector.cpp",
        "com_android_server_power_stats_CpuPowerStatsCollector.cpp",
        "com_android_server_hint_HintManagerService.cpp",
        "com_android_server_hint_HintManagerService.cpp",
        "com_android_server_SerialService.cpp",
        "com_android_server_soundtrigger_middleware_AudioSessionProviderImpl.cpp",
        "com_android_server_soundtrigger_middleware_AudioSessionProviderImpl.cpp",
        "com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker.cpp",
        "com_android_server_soundtrigger_middleware_ExternalCaptureStateTracker.cpp",
        "com_android_server_stats_pull_StatsPullAtomService.cpp",
        "com_android_server_stats_pull_StatsPullAtomService.cpp",
+0 −83
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "SerialServiceJNI"
#include "utils/Log.h"

#include "jni.h"
#include <nativehelper/JNIPlatformHelp.h>
#include "android_runtime/AndroidRuntime.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace android
{

static struct parcel_file_descriptor_offsets_t
{
    jclass mClass;
    jmethodID mConstructor;
} gParcelFileDescriptorOffsets;

static jobject android_server_SerialService_open(JNIEnv *env, jobject /* thiz */, jstring path)
{
    const char *pathStr = env->GetStringUTFChars(path, NULL);

    int fd = open(pathStr, O_RDWR | O_NOCTTY);
    if (fd < 0) {
        ALOGE("could not open %s", pathStr);
        env->ReleaseStringUTFChars(path, pathStr);
        return NULL;
    }
    env->ReleaseStringUTFChars(path, pathStr);

    jobject fileDescriptor = jniCreateFileDescriptor(env, fd);
    if (fileDescriptor == NULL) {
        close(fd);
        return NULL;
    }
    return env->NewObject(gParcelFileDescriptorOffsets.mClass,
        gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
}


static const JNINativeMethod method_table[] = {
    { "native_open",                "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
                                    (void*)android_server_SerialService_open },
};

int register_android_server_SerialService(JNIEnv *env)
{
    jclass clazz = env->FindClass("com/android/server/SerialService");
    if (clazz == NULL) {
        ALOGE("Can't find com/android/server/SerialService");
        return -1;
    }

    clazz = env->FindClass("android/os/ParcelFileDescriptor");
    LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
    gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
    gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
    LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
                 "Unable to find constructor for android.os.ParcelFileDescriptor");

    return jniRegisterNativeMethods(env, "com/android/server/SerialService",
            method_table, NELEM(method_table));
}

};
+0 −2
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@ int register_android_server_PowerStatsService(JNIEnv* env);
int register_android_server_power_stats_CpuPowerStatsCollector(JNIEnv* env);
int register_android_server_power_stats_CpuPowerStatsCollector(JNIEnv* env);
int register_android_server_HintManagerService(JNIEnv* env);
int register_android_server_HintManagerService(JNIEnv* env);
int register_android_server_storage_AppFuse(JNIEnv* env);
int register_android_server_storage_AppFuse(JNIEnv* env);
int register_android_server_SerialService(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_SystemServer(JNIEnv* env);
int register_android_server_UsbAlsaJackDetector(JNIEnv* env);
int register_android_server_UsbAlsaJackDetector(JNIEnv* env);
int register_android_server_UsbAlsaMidiDevice(JNIEnv* env);
int register_android_server_UsbAlsaMidiDevice(JNIEnv* env);
@@ -93,7 +92,6 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
    register_android_server_PowerStatsService(env);
    register_android_server_PowerStatsService(env);
    register_android_server_power_stats_CpuPowerStatsCollector(env);
    register_android_server_power_stats_CpuPowerStatsCollector(env);
    register_android_server_HintManagerService(env);
    register_android_server_HintManagerService(env);
    register_android_server_SerialService(env);
    register_android_server_InputManager(env);
    register_android_server_InputManager(env);
    register_android_server_LightsService(env);
    register_android_server_LightsService(env);
    register_android_server_UsbDeviceManager(env);
    register_android_server_UsbDeviceManager(env);