예제 #1
0
static int create_service_thread(void (*func)(int, void *), void *cookie)
{
    stinfo *sti;
    adb_thread_t t;
    int s[2];

    if(adb_socketpair(s)) {
        printf("cannot create service socket pair\n");
        XLOGW("cannot create service socket pair\n");
        return -1;
    }

    sti = malloc(sizeof(stinfo));
    if(sti == 0) fatal("cannot allocate stinfo");
    sti->func = func;
    sti->cookie = cookie;
    sti->fd = s[1];

    if(adb_thread_create( &t, service_bootstrap_func, sti)){
        free(sti);
        adb_close(s[0]);
        adb_close(s[1]);
        printf("cannot create service thread\n");
        XLOGW("cannot create service thread\n");
        return -1;
    }

    D("service thread started, %d:%d\n",s[0], s[1]);
    return s[0];
}
예제 #2
0
JNIEXPORT jboolean Java_com_mediatek_engineermode_modem_SleepMode_setSleepMode(
		JNIEnv* env, jobject clazz, jint dwReserved) {
	XLOGW("%d", dwReserved);

	unsigned int Message[4] = { 0xFFFFFFFF, UEM_CCCI_EM_REQ_SET_SLEEP_MODE,
			CCCI_UEM_TX, dwReserved };

	int file = -1;
	file = open("/dev/ccci_uem_tx", O_WRONLY);
	if (-1 == file) {
		return 0;
	}

	XLOGW("open file successful");
	write(file, Message, sizeof(Message));
	XLOGW("write file successful");
	XLOGW("After set SleepMode: %d %d %d %d", Message[0], Message[1],
			Message[2], Message[3]);
	close(file);

	int fileRead = -1;
	fileRead = open("/dev/ccci_uem_rx", O_RDONLY);
	if (-1 == fileRead) {
		XLOGW("open ccci_uem_rx failed");
		return -1;
	}
	XLOGW("open ccci_uem_rx successful");
	XLOGW("Source: %d %d %d %d", Message[0], Message[1], Message[2], Message[3]);
	read(fileRead, Message, sizeof(Message));
	XLOGW("read file successful");
	XLOGW("Recv: %d %d %d %d", Message[0], Message[1], Message[2], Message[3]);
	close(fileRead);

	return 1;
}
예제 #3
0
JNIEXPORT jint Java_com_mediatek_engineermode_modem_SleepMode_getDCM(
		JNIEnv* env, jobject clazz) {
	XLOGW("getDCM");
	unsigned int Message[4] = { 0xFFFFFFFF, UEM_CCCI_EM_REQ_GET_DCM_MODE,
			CCCI_UEM_TX, 0 };
	// set read cmd
	int file = -1;
	file = open("/dev/ccci_uem_tx", O_WRONLY);
	if (-1 == file) {
		XLOGW("open ccci_uem_tx failed");
		return -1;
	}

	XLOGW("open file successful");
	write(file, Message, sizeof(Message));
	XLOGW("write file successful");
	close(file);

	int fileRead = -1;
	fileRead = open("/dev/ccci_uem_rx", O_RDONLY);
	if (-1 == fileRead) {
		XLOGW("open ccci_uem_rx failed");
		return -1;
	}
	XLOGW("open ccci_uem_rx successful");
	XLOGW("Source: %d %d %d %d", Message[0], Message[1], Message[2], Message[3]);
	read(fileRead, Message, sizeof(Message));
	XLOGW("read file successful");
	XLOGW("Recv: %d %d %d %d", Message[0], Message[1], Message[2], Message[3]);
	close(fileRead);

	return Message[3];
}
예제 #4
0
static int create_subproc_thread(const char *name, const subproc_mode mode)
{
    adb_thread_t t;
    int ret_fd;
    pid_t pid = -1;

    long mem_free = 0;
    read_meminfo(&mem_free);
    XLOGW("read_meminfo() mem_free=%ld\n", mem_free);

    const char *arg0, *arg1;
    if (name == 0 || *name == 0) {
        arg0 = "-"; arg1 = 0;
    } else {
        arg0 = "-c"; arg1 = name;
    }

    switch (mode) {
    case SUBPROC_PTY:
        ret_fd = create_subproc_pty(SHELL_COMMAND, arg0, arg1, &pid);
        break;
    case SUBPROC_RAW:
        ret_fd = create_subproc_raw(SHELL_COMMAND, arg0, arg1, &pid);
        break;
    default:
        fprintf(stderr, "invalid subproc_mode %d\n", mode);
        return -1;
    }
    D("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);
    XLOGW("create_subproc ret_fd=%d pid=%d\n", ret_fd, pid);

    if ( sti == 0 )
    {
    sti = malloc(sizeof(stinfo));
    if(sti == 0) fatal("cannot allocate stinfo");
    sti->func = subproc_waiter_service;
    sti->cookie = (void*) (uintptr_t) pid;
    sti->fd = ret_fd;

    if (adb_thread_create(&t, service_bootstrap_func, sti)) {
        XLOGW("adb_thread_create() errno=%d\n", errno);
        free(sti);
        adb_close(ret_fd);
        fprintf(stderr, "cannot create service thread\n");
        return -1;
    }
    }

    D("service thread started, fd=%d pid=%d\n", ret_fd, pid);
    return ret_fd;
}
예제 #5
0
static void subproc_waiter_service(int fd, void *cookie)
{
    pid_t pid = (pid_t) (uintptr_t) cookie;

    D("entered. fd=%d of pid=%d\n", fd, pid);
    for (;;) {
        int status;
        pid_t p = waitpid(-1/*pid*/, &status, 0);
        //if (p == pid) {
            D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
            if (WIFSIGNALED(status)) {
                D("*** Killed by signal %d\n", WTERMSIG(status));
                //break;
            } else if (!WIFEXITED(status)) {
                D("*** Didn't exit!!. status %d\n", status);
                //break;
            } else if (WEXITSTATUS(status) >= 0) {
                D("*** Exit code %d\n", WEXITSTATUS(status));
                //break;
            }
         //}
        if ( p == -1 )
            usleep(500000);  // poll every 0.5 sec
    }
    D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
    XLOGW("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
    if (SHELL_EXIT_NOTIFY_FD >=0) {
      int res;
      res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
      D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
        SHELL_EXIT_NOTIFY_FD, pid, res, errno);
    }
}
예제 #6
0
status_t unregisterMva(mHalRegisterLoopMemoryObj_t *mva) {
    if (NULL == mva) {
        XLOGW("[%s] NULL mva object, cannot unregister it", __func__);
        return INVALID_OPERATION;
    }

    // lock here for manipulate MVA and MDP
    Mutex::Autolock l(gMvaLock);

    assert((NULL != libmhalmdp) && (NULL != unregisterLoopMemory));

    status_t ret = NO_ERROR;
    if (0 <= unregisterLoopMemory(MHAL_MLM_CLIENT_SFTEX, mva)) {
        gMvaCnt -= 1;
        delete mva;
        XLOGI("    unregister MVA success, srcImgYAddr:%p, Aux MVA count:%d", mva->calc_addr->y, gMvaCnt);
        ret = NO_ERROR;
    } else {
        XLOGE("    unregister MVA FAILED, srcImgYAddr:%p", mva->calc_addr->y);
        ret = INVALID_OPERATION;
    }

    // if all MVA unregistered, think as no MDP functions required
    if (0 == gMvaCnt) {
        deinitMDPLocked();
    }

    return ret;
}
예제 #7
0
mHalRegisterLoopMemoryObj_t *registerMva(sp<GraphicBuffer> gb) {
    if ((HAL_PIXEL_FORMAT_YV12 != gb->format) && (HAL_PIXEL_FORMAT_RGBA_8888 != gb->format)) {
        XLOGW("[%s] only for YV12/RGBA_8888 now", __func__);
        return NULL;
    }

    void *ptr;
    mHalRegisterLoopMemory_t para;
    mHalRegisterLoopMemoryObj_t *ret;

    gb->lock(LOCK_FOR_MDP, &ptr);          // get VA
    gb->unlock();

    if (HAL_PIXEL_FORMAT_YV12 == gb->format) {
        para.mhal_color = MHAL_FORMAT_IMG_YV12;
        para.buffer_size = gb->stride * gb->height * 3 / 2;
    } else if (HAL_PIXEL_FORMAT_RGBA_8888 == gb->format) {
        para.mhal_color = MHAL_FORMAT_ABGR_8888;
        para.buffer_size = gb->stride * gb->height * 4;
    }
    para.mem_type = MHAL_MEM_TYPE_OUTPUT;
    para.addr     = (uint32_t)ptr;
    para.img_size = mHalMdpSize(gb->stride, gb->height);
    para.img_roi  = mHalMdpRect(0, 0, gb->stride, gb->height);
    para.rotate   = MHAL_BITBLT_ROT_0;

    // lock here for manipulate MVA and MDP
    Mutex::Autolock l(gMvaLock);

    // if first time to use MDP/MVA, init these utilities
    if (0 == gMvaCnt) {
        initMDPLocked();
    }
    assert((NULL != libmhalmdp) && (NULL != registerLoopMemory));

    ret = (new mHalRegisterLoopMemoryObj_t());
    if (0 <= registerLoopMemory(MHAL_MLM_CLIENT_SFTEX, &para, ret)) {
        gMvaCnt += 1;
        XLOGI("    register MVA success, srcImgYAddr:%p, Aux MVA count:%d", ret->calc_addr->y, gMvaCnt);
    } else {
        delete ret;
        ret = NULL;
        XLOGW("    register MVA FAILED, maybe M4U permission deny");
    }

    return ret;
}
예제 #8
0
bool SFWatchDog::isSFThreadHang(nsecs_t* pStopTime) {
    Mutex::Autolock _l(mLock);

    const nsecs_t now = systemTime();
    if (mShowLog) {
        XLOGI("[SF-WD] Threshold: %" PRId64 " ns NodeList size: %d", mThreshold * 1000 * 1000, mNodeList.size());
        for (uint32_t i = 0; i < mNodeList.size(); i++) {
            XLOGI("[SF-WD]   [%s] last transaction: %" PRId64 ", now: %" PRId64 ".",
                    mNodeList[i]->mName.string(),
                    mNodeList[i]->mStartTransactionTime, now);
        }
    }

    *pStopTime = INT64_MAX;
    for (uint32_t i = 0; i < mNodeList.size(); i++) {
        if ((mNodeList[i]->mStartTransactionTime != 0 || mThreshold == 0) &&
            mThreshold * 1000 * 1000 < now - mNodeList[i]->mStartTransactionTime) {
            // we could set mThreshold as zero for debugging
            *pStopTime = *pStopTime < now - mNodeList[i]->mStartTransactionTime ?
                *pStopTime : now - mNodeList[i]->mStartTransactionTime;
        }
    }

    if (*pStopTime != INT64_MAX) { // maybe hang
        XLOGW("[SF-WD] ============================================");
        XLOGW("[SF-WD] detect SF maybe hang, Threshold: %" PRId64 " ns StopTime: %" PRId64 " ns, List(size: %d):", mThreshold * 1000 * 1000,
                *pStopTime, mNodeList.size());
        for (uint32_t i = 0; i < mNodeList.size(); i++) {
            if (mNodeList[i]->mStartTransactionTime == 0) {
                XLOGI("[SF-WD]   [%s] wait event", mNodeList[i]->mName.string());
            } else {
                XLOGW("[SF-WD]   [%s] stopTime= %" PRId64 "ns", mNodeList[i]->mName.string(), now - mNodeList[i]->mStartTransactionTime);
            }
        }

        if (mThreshold == 0) {
            *pStopTime = 1;
        }
        return true;
    } else {
        *pStopTime = 1;
        return false;
    }
}
예제 #9
0
bool SurfaceFlinger::mustRecompose(size_t dpy) const {
    for (size_t i=0 ; i<mDisplays.size() ; i++) {
        const int32_t id = mDisplays[i]->getHwcDisplayId();
        if (id == dpy) return mDisplays[i]->mustRecompose();
    }

    // this should never happen, probably an invalid display?
    XLOGW("dpy(%zu) is not in display devices list", dpy);
    return true;;
}
예제 #10
0
asocket *create_local_service_socket(const char *name)
{
    asocket *s;
    int fd;

    XLOGW("create_local_service_socket() name=%s\n", name);

#if !ADB_HOST
    if (!strcmp(name,"jdwp")) {
        return create_jdwp_service_socket();
    }
    if (!strcmp(name,"track-jdwp")) {
        return create_jdwp_tracker_service_socket();
    }
#endif
    fd = service_to_fd(name);
    if(fd < 0){
        XLOGW("create_local_service_socket fail");
        return 0;
    }

    s = create_local_socket(fd);
    XLOGW("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
    D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);

#if !ADB_HOST
    char value[PROPERTY_VALUE_MAX];
    property_get("ro.debuggable", value, "");

    if ((!strncmp(name, "root:", 5) && getuid() != 0 && (strcmp(value, "1") == 0))
        || !strncmp(name, "usb:", 4)
        || !strncmp(name, "tcpip:", 6)) {
        D("LS(%d): enabling exit_on_close\n", s->id);
        s->exit_on_close = 1;
    }
#endif

    return s;
}
예제 #11
0
void RenderEngine::getHwInverseMatrix(const sp<const DisplayDevice>& hw, mat4& inv) {
    // get inversed width/height
    const float iw = 2.0 / hw->getWidth();
    const float ih = 2.0 / hw->getHeight();

    // map to required matrix
    // since in display case, we need only orientation, but not free-form
    // so just use pre-calculated cases in switch

    // setup fixed part first
    inv[0][2] =  0;    inv[0][3] =  0;
    inv[1][2] =  0;    inv[1][3] =  0;
    inv[2][2] = -2;    inv[2][3] =  0;
    inv[3][2] = -1;    inv[3][3] =  1;

    // switch map to set x/y matrix entries
    switch (hw->getHwOrientation()) {
        case DisplayState::eOrientationDefault:
            inv[0][0] =  iw;    inv[0][1] =   0;
            inv[1][0] =   0;    inv[1][1] = -ih;
            inv[2][0] =   0;    inv[2][1] =   0;
            inv[3][0] =  -1;    inv[3][1] =   1;
            break;
        case DisplayState::eOrientation90:
            inv[0][0] =   0;    inv[0][1] = -iw;
            inv[1][0] = -ih;    inv[1][1] =   0;
            inv[2][0] =   0;    inv[2][1] =   0;
            inv[3][0] =   1;    inv[3][1] =   1;
            break;
        case DisplayState::eOrientation180:
            inv[0][0] = -iw;    inv[0][1] =   0;
            inv[1][0] =   0;    inv[1][1] =  ih;
            inv[2][0] =   0;    inv[2][1] =   0;
            inv[3][0] =   1;    inv[3][1] =  -1;
            break;
        case DisplayState::eOrientation270:
            inv[0][0] =   0;    inv[0][1] =  iw;
            inv[1][0] =  ih;    inv[1][1] =   0;
            inv[2][0] =   0;    inv[2][1] =   0;
            inv[3][0] =  -1;    inv[3][1] =  -1;
            break;
        default:
            XLOGW("[%s] unknown orientation:%d, set to default",
                __func__, hw->getHwOrientation());
            inv[0][0] =  iw;    inv[0][1] =   0;
            inv[1][0] =   0;    inv[1][1] = -ih;
            inv[2][0] =   0;    inv[2][1] =   0;
            inv[3][0] =  -1;    inv[3][1] =   1;
    }
}
예제 #12
0
static void init_subproc_child()
{
    setsid();

    // Set OOM score adjustment to prevent killing
    int fd = adb_open("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
    if (fd >= 0) {
        adb_write(fd, "0", 1);
        adb_close(fd);
    } else {
       D("adb: unable to update oom_score_adj\n");
       XLOGW("adb: unable to update oom_score_adj\n");
    }
}
예제 #13
0
static int create_subproc_thread(const char *name)
{
    adb_thread_t t;
    int ret_fd;
    pid_t pid;

    long mem_free = 0;
    read_meminfo(&mem_free);
    D("read_meminfo() mem_free=%d\n", mem_free);
    XLOGV("read_meminfo() mem_free=%d\n", mem_free);

    if(name) {
        ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
    } else {
        ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
    }
    D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
    XLOGV("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);

    if ( sti == 0 )
    {
    sti = malloc(sizeof(stinfo));
    if(sti == 0) fatal("cannot allocate stinfo");
    sti->func = subproc_waiter_service;
    sti->cookie = (void*)pid;
    sti->fd = ret_fd;

        int nRet = adb_thread_create( &t, service_bootstrap_func, sti);

        if(nRet)
        {
            D("adb_thread_create() nRet=%d errno=%d\n", nRet, errno);
            XLOGW("adb_thread_create() nRet=%d errno=%d\n", nRet, errno);
        free(sti);
            sti = 0;
        adb_close(ret_fd);
        printf("cannot create service thread\n");
        return -1;
    }
    }

    D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
    return ret_fd;
}
예제 #14
0
void init_usb_transport(atransport *t, usb_handle *h, int state)
{
    D("transport: usb\n");
    XLOGW("transport: usb\n");
    t->close = remote_close;
    t->kick = remote_kick;
    t->read_from_remote = remote_read;
    t->write_to_remote = remote_write;
    t->sync_token = 1;
    t->connection_state = state;
    t->type = kTransportUsb;
    t->usb = h;

#if ADB_HOST
    HOST = 1;
#else
    HOST = 0;
#endif
}
예제 #15
0
static void local_socket_close_locked(asocket *s)
{
    XLOGW("entered. LS(%d) fd=%d\n", s->id, s->fd);
    D("entered. LS(%d) fd=%d\n", s->id, s->fd);
    if(s->peer) {
        D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n",
          s->id, s->peer->id, s->peer->fd);
        s->peer->peer = 0;
        // tweak to avoid deadlock
        if (s->peer->close == local_socket_close) {
            local_socket_close_locked(s->peer);
        } else {
            s->peer->close(s->peer);
        }
        s->peer = 0;
    }

        /* If we are already closing, or if there are no
        ** pending packets, destroy immediately
        */
    if (s->closing || s->pkt_first == NULL) {
        int   id = s->id;
        local_socket_destroy(s);
        D("LS(%d): closed\n", id);
        return;
    }

        /* otherwise, put on the closing list
        */
    D("LS(%d): closing\n", s->id);
    s->closing = 1;
    fdevent_del(&s->fde, FDE_READ);
    remove_socket(s);
    D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
    insert_local_socket(s, &local_socket_closing_list);
}
예제 #16
0
bool SFWatchDog::threadLoop() {
    XLOGV("[%s]", __func__);

    {
        Mutex::Autolock _l(mScreenLock);
    }

    nsecs_t stopTime = 1;
    if (isSFThreadHang(&stopTime)) {
        char cmds[256];
        static uint32_t rtt_ct = SW_WATCHDOG_RTTCOUNT;
        if (rtt_ct > 0) {
            rtt_ct --;
        } else {
            XLOGD("[SF-WD] swap rtt dump file");

            // swap rtt dump file
            snprintf(cmds, sizeof(cmds), "mv %s.txt %s_1.txt", RTT_DUMP, RTT_DUMP);
            system(cmds);

            rtt_ct = SW_WATCHDOG_RTTCOUNT;
        }

        // append SurfaceFlinger rtt information to rtt file
        char filename[100];
        snprintf(filename, sizeof(filename), "%s.txt", RTT_DUMP);
        int fd = open(filename, O_CREAT | O_WRONLY | O_NOFOLLOW, 0666);  /* -rw-rw-rw- */
        if (fd < 0) {
            ALOGE("Can't open %s: %s\n", filename, strerror(errno));
            return true;
        }

        if (lseek(fd, 0, SEEK_END) < 0) {
            fprintf(stderr, "lseek: %s\n", strerror(errno));
        } else {
            dump_backtrace_to_file(getpid(), fd);
        }

        close(fd);

        XLOGD("[SF-WD] dump rtt file: %s.txt", RTT_DUMP);
        XLOGW("[SF-WD] ============================================");
    } else {
        stopTime = 1;
    }

    getProperty();

    char value[PROPERTY_VALUE_MAX];
    snprintf(value, sizeof(value), "%" PRId64 " ", stopTime);
    if (stopTime < 0 || stopTime >= 2147483247) {
        volatile nsecs_t tmpStopTime = stopTime;
        XLOGD("[SF-WD] tmpStopTime=(%" PRId64 ", %" PRId64 ")", tmpStopTime, stopTime);
        abort();
    }

    uint32_t ret = property_set("service.sf.status", value);
    if (mUpdateCount) {
        if (mShowLog)
            XLOGV("[SF-WD] mUpdateCount: %d", mUpdateCount);
#if 0
        aee_ioctl_wdt_kick(WDT_SETBY_SF);
#endif
        mUpdateCount = 0;
    }
    //else {
    //	XLOGV("[SF-WD] mUpdateCount not update!!!!!: %d", mUpdateCount);
    //	aee_ioctl_wdt_kick(WDT_SETBY_SF_NEED_NOT_UPDATE);
    //}
    usleep(mTimer * 1000);
    return true;
}
예제 #17
0
static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
{
#ifdef HAVE_WIN32_PROC
    D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
    fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
    return -1;
#else /* !HAVE_WIN32_PROC */
    char *devname;
    int ptm;

    ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
    if(ptm < 0){
        printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
        return -1;
    }
    fcntl(ptm, F_SETFD, FD_CLOEXEC);

    if(grantpt(ptm) || unlockpt(ptm) ||
       ((devname = (char*) ptsname(ptm)) == 0)){
        printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
        adb_close(ptm);
        return -1;
    }

    *pid = fork();
    if(*pid < 0) {
        printf("- fork failed: pid(%d) %s -\n", *pid, strerror(errno));
        XLOGV("- fork failed: pid(%d) %s -\n", *pid, strerror(errno));
        adb_close(ptm);
        return -1;
    }

    if(*pid == 0){
        int pts;

        setsid();

        pts = unix_open(devname, O_RDWR);
        if(pts < 0) {
            fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
            XLOGV("child failed to open pseudo-term slave: %s\n", devname);
            exit(-1);
        }

        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);

        adb_close(pts);
        adb_close(ptm);

        // set OOM adjustment to zero
        char text[64];
        snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
        int fd = adb_open(text, O_WRONLY);
        if (fd >= 0) {
            adb_write(fd, "0", 1);
            adb_close(fd);
        } else {
           D("adb: unable to open %s\n", text);
           XLOGW("adb: unable to open %s\n", text);
        }

        timer_t timerid;
        struct sigevent sev;
        /* Create the timer */
        sev.sigev_notify = SIGEV_SIGNAL;
        sev.sigev_signo = SIGALRM;
        sev.sigev_value.sival_ptr = &timerid;
        if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1)
        {
            D("adb: Call timer_create failed!\n");
            XLOGV("adb: Call timer_create failed!\n");
            exit(-1);
        }

        struct itimerspec its;
        /* Start the timer */
        its.it_value.tv_sec = 1;
        its.it_value.tv_nsec = 0;
        its.it_interval.tv_sec = 3;
        its.it_interval.tv_nsec = 0;

        if (timer_settime(timerid, 0, &its, NULL) == -1)
        {
            D("adb: Call timer_settime failed!\n");
            XLOGV("adb: Call timer_settime failed!\n");
            exit(-1);
        }

        sigset_t mask;
        struct sigaction sa;
        /* Establish handler for timer signal */
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = catcher;
        sigemptyset(&sa.sa_mask);
        if (sigaction(SIGALRM, &sa, NULL) == -1)
        {
            D("adb: Call sigaction failed!\n");
            XLOGV("adb: Call sigaction failed!\n");
            exit(-1);
        }

        int nRet = execl(cmd, cmd, arg0, arg1, NULL);

        fprintf(stderr, "- exec '%s' failed: %s (%d) nRet(%d) -\n",
                cmd, strerror(errno), errno, nRet);
        XLOGV("- exec '%s' failed: %s (%d) nRet(%d) -\n",
                cmd, strerror(errno), errno, nRet);
        exit(-1);
    } else {
        // Don't set child's OOM adjustment to zero.
        // Let the child do it itself, as sometimes the parent starts
        // running before the child has a /proc/pid/oom_adj.
        // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
        return ptm;
    }
#endif /* !HAVE_WIN32_PROC */
}
예제 #18
0
void handle_packet(apacket *p, atransport *t)
{
    asocket *s;

    D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0],
            ((char*) (&(p->msg.command)))[1],
            ((char*) (&(p->msg.command)))[2],
            ((char*) (&(p->msg.command)))[3]);
    print_packet("recv", p);

    switch(p->msg.command){
    case A_SYNC:
        XLOGD("%s: %s %08x %08x %04x \n",
            "recv", "SYNC", p->msg.arg0, p->msg.arg1, p->msg.data_length);
        if(p->msg.arg0){
            send_packet(p, t);
            if(HOST) send_connect(t);
        } else {
            t->connection_state = CS_OFFLINE;
            handle_offline(t);
            send_packet(p, t);
        }
        return;

    case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
            /* XXX verify version, etc */
       XLOGD("%s: %s %08x %08x %04x %s\n",
            "recv", "CNXN", p->msg.arg0, p->msg.arg1, p->msg.data_length, p->data);
        if(t->connection_state != CS_OFFLINE) {
            t->connection_state = CS_OFFLINE;
            handle_offline(t);
        }

        parse_banner((char*) p->data, t);

        if (HOST || !auth_enabled) {
            handle_online(t);
            if(!HOST) send_connect(t);
        } else {
            send_auth_request(t);
        }
        break;

    case A_AUTH:
        if (p->msg.arg0 == ADB_AUTH_TOKEN) {
            t->key = adb_auth_nextkey(t->key);
            if (t->key) {
                send_auth_response(p->data, p->msg.data_length, t);
            } else {
                /* No more private keys to try, send the public key */
                send_auth_publickey(t);
            }
        } else if (p->msg.arg0 == ADB_AUTH_SIGNATURE) {
            if (adb_auth_verify(t->token, p->data, p->msg.data_length)) {
                adb_auth_verified(t);
                t->failed_auth_attempts = 0;
            } else {
                if (t->failed_auth_attempts++ > 10)
                    adb_sleep_ms(1000);
                send_auth_request(t);
            }
        } else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
            adb_auth_confirm_key(p->data, p->msg.data_length, t);
        }
        break;

    case A_OPEN: /* OPEN(local-id, 0, "destination") */
        if (t->online) {
            char *name = (char*) p->data;
            name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0;
            s = create_local_service_socket(name);
            if(s == 0) {
                send_close(0, p->msg.arg0, t);
            } else {
                s->peer = create_remote_socket(p->msg.arg0, t);
                s->peer->peer = s;
                send_ready(s->id, s->peer->id, t);
                s->ready(s);
            }
        }
        break;

    case A_OKAY: /* READY(local-id, remote-id, "") */
        if (t->online) {
            if((s = find_local_socket(p->msg.arg1))) {
                if(s->peer == 0) {
                    s->peer = create_remote_socket(p->msg.arg0, t);
                    s->peer->peer = s;
                }
                s->ready(s);
            }
        }
        break;

    case A_CLSE: /* CLOSE(local-id, remote-id, "") */
        if (t->online) {
            if((s = find_local_socket(p->msg.arg1))) {
                s->close(s);
            }
        }
        break;

    case A_WRTE:
        if (t->online) {
            if((s = find_local_socket(p->msg.arg1))) {
                unsigned rid = p->msg.arg0;
                p->len = p->msg.data_length;

                if(s->enqueue(s, p) == 0) {
                    D("Enqueue the socket\n");
                    send_ready(s->id, rid, t);
                }
                return;
            }
        }
        break;

    default:
        printf("handle_packet: what is %08x?!\n", p->msg.command);
        XLOGW("handle_packet: what is %08x?!\n", p->msg.command);
    }

    put_apacket(p);
}
예제 #19
0
bool BackupBufPusher::push(const sp<BackupBuffer>& in) {
    if ((in == NULL) || (in->mGraphicBuffer == NULL)) {
        XLOGW("[%s] input buffer is NULL", __func__);
        return false;
    }

    sp<BackupBuffer>& buffer = editHead();

    // check property of GraphicBuffer, realloc if needed
    bool needCreate = false;
    if ((buffer == NULL) || (buffer->mGraphicBuffer == NULL)) {
        needCreate = true;
        XLOGD("[%s] buffer head is NULL, create it", __func__);
    } else {
        if ((buffer->mGraphicBuffer->width != in->mGraphicBuffer->width) ||
                (buffer->mGraphicBuffer->height != in->mGraphicBuffer->height) ||
                (buffer->mGraphicBuffer->format != in->mGraphicBuffer->format)) {
            needCreate = true;
            XLOGD("[%s] geometry changed, backup=(%d, %d, %d) ==> active=(%d, %d, %d)",
                __func__, buffer->mGraphicBuffer->width, buffer->mGraphicBuffer->height,
                buffer->mGraphicBuffer->format, in->mGraphicBuffer->width,
                in->mGraphicBuffer->height, in->mGraphicBuffer->format);
        }
    }

    if (needCreate) {
        sp<GraphicBuffer> newGraphicBuffer = new GraphicBuffer(
                                            in->mGraphicBuffer->width, in->mGraphicBuffer->height,
                                            in->mGraphicBuffer->format, in->mGraphicBuffer->usage);
        if (newGraphicBuffer == NULL) {
            XLOGE("[%s] alloc GraphicBuffer failed", __func__);
            return false;
        }

        if (buffer == NULL) {
            buffer = new BackupBuffer();
            if (buffer == NULL) {
                XLOGE("[%s] alloc BackupBuffer failed", __func__);
                return false;
            }
        }

        buffer->mGraphicBuffer = newGraphicBuffer;
    }

    float bpp = 0.0f;
    int width = in->mGraphicBuffer->width;
    int height = in->mGraphicBuffer->height;
    int format = in->mGraphicBuffer->format;
    int usage = in->mGraphicBuffer->usage;
    int stride = in->mGraphicBuffer->stride;
    status_t err;

    switch (format) {
        case PIXEL_FORMAT_RGBA_8888:
        case PIXEL_FORMAT_BGRA_8888:
        case PIXEL_FORMAT_RGBX_8888:
        case 0x1ff:
            // tricky format for SGX_COLOR_FORMAT_BGRX_8888 in fact
            bpp = 4.0;
            break;
        case PIXEL_FORMAT_RGB_565:
            bpp = 2.0;
            break;
        case HAL_PIXEL_FORMAT_I420:
            bpp = 1.5;
            break;
        case HAL_PIXEL_FORMAT_YV12:
            bpp = 1.5;
            break;
        default:
            XLOGE("[%s] cannot dump format:%d", __func__, format);
            break;
    }
    
    // backup
    void *src;
    void *dst;
    err = in->mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &src);
    if (err != NO_ERROR) {
        XLOGE("[%s] lock GraphicBuffer failed", __func__);
        return false;
    }

    err = buffer->mGraphicBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_OFTEN, &dst);
    if (err != NO_ERROR) {
        in->mGraphicBuffer->unlock();
        XLOGE("[%s] lock backup buffer failed", __func__);
        return false;
    }

    memcpy(dst, src, stride * height * bpp);

    buffer->mGraphicBuffer->unlock();
    in->mGraphicBuffer->unlock();

    // update timestamp
    buffer->mTimeStamp = in->mTimeStamp;

    return true;
}
예제 #20
0
int service_to_fd(const char *name)
{
    int ret = -1;

#if !ADB_HOST
    XLOGW("service_to_fd() name=%s\n", name);
#endif

    if(!strncmp(name, "tcp:", 4)) {
        int port = atoi(name + 4);
        name = strchr(name + 4, ':');
        if(name == 0) {
            ret = socket_loopback_client(port, SOCK_STREAM);
            if (ret >= 0)
                disable_tcp_nagle(ret);
        } else {
#if ADB_HOST
            ret = socket_network_client(name + 1, port, SOCK_STREAM);
#else
            return -1;
#endif
        }
#ifndef HAVE_WINSOCK   /* winsock doesn't implement unix domain sockets */
    } else if(!strncmp(name, "local:", 6)) {
        ret = socket_local_client(name + 6,
                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
    } else if(!strncmp(name, "localreserved:", 14)) {
        ret = socket_local_client(name + 14,
                ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
    } else if(!strncmp(name, "localabstract:", 14)) {
        ret = socket_local_client(name + 14,
                ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
    } else if(!strncmp(name, "localfilesystem:", 16)) {
        ret = socket_local_client(name + 16,
                ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
#endif
#if !ADB_HOST
    } else if(!strncmp("dev:", name, 4)) {
        ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
    } else if(!strncmp(name, "framebuffer:", 12)) {
        ret = create_service_thread(framebuffer_service, 0);
    } else if (!strncmp(name, "jdwp:", 5)) {
        ret = create_jdwp_connection_fd(atoi(name+5));
    } else if(!HOST && !strncmp(name, "shell:", 6)) {
        if(!strncmp(name, "shell:perf_test", 15)) {
            perf_test = ~perf_test;
            XLOGW("service_to_fd() perf_test(%d)\n", perf_test);
            ret = -1;
        } else {
            ret = create_subproc_thread(name + 6, SUBPROC_PTY);
        }
    } else if(!HOST && !strncmp(name, "exec:", 5)) {
        ret = create_subproc_thread(name + 5, SUBPROC_RAW);
    } else if(!strncmp(name, "sync:", 5)) {
        ret = create_service_thread(file_sync_service, NULL);
    } else if(!strncmp(name, "remount:", 8)) {
        ret = create_service_thread(remount_service, NULL);
    } else if(!strncmp(name, "reboot:", 7)) {
        void* arg = strdup(name + 7);
        if (arg == NULL) return -1;
        ret = create_service_thread(reboot_service, arg);
    } else if(!strncmp(name, "root:", 5)) {
        ret = create_service_thread(restart_root_service, NULL);
    } else if(!strncmp(name, "backup:", 7)) {
        char* arg = strdup(name + 7);
        if (arg == NULL) return -1;
        char* c = arg;
        for (; *c != '\0'; c++) {
            if (*c == ':')
                *c = ' ';
        }
        char* cmd;
        if (asprintf(&cmd, "/system/bin/bu backup %s", arg) != -1) {
            ret = create_subproc_thread(cmd, SUBPROC_RAW);
            free(cmd);
        }
        free(arg);
    } else if(!strncmp(name, "restore:", 8)) {
        ret = create_subproc_thread("/system/bin/bu restore", SUBPROC_RAW);
    } else if(!strncmp(name, "tcpip:", 6)) {
        int port;
        if (sscanf(name + 6, "%d", &port) == 0) {
            port = 0;
        }
        ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
    } else if(!strncmp(name, "usb:", 4)) {
        ret = create_service_thread(restart_usb_service, NULL);
    } else if (!strncmp(name, "reverse:", 8)) {
        char* cookie = strdup(name + 8);
        if (cookie == NULL) {
            ret = -1;
        } else {
            ret = create_service_thread(reverse_service, cookie);
            if (ret < 0) {
                free(cookie);
            }
        }
#endif
    }
    if (ret >= 0) {
        close_on_exec(ret);
    }
    return ret;
}
예제 #21
0
// conversion function should format by format, chip by chip
// currently input is MTK_I420, and output is IMG_YV12/ABGR
status_t SurfaceTexture::convertToAuxSlotLocked(bool isForce) {
    // check invalid buffer
    if (BufferQueue::INVALID_BUFFER_SLOT == mCurrentTexture) {
        mAuxSlotConvert = false;
        return INVALID_OPERATION;
    }

    ATRACE_CALL();

    // 1) normal BufferQueue needs conversion now
    // 2) SurfaceTextureLayer neesd conversion after HWC
    bool isNeedConversionNow =
        (BufferQueue::TYPE_BufferQueue == mBufferQueue->getType()) ||
        ((true == isForce) && (BufferQueue::TYPE_SurfaceTextureLayer == mBufferQueue->getType()));

    //if ((true == isNeedConversionNow) && (BufferQueue::NO_CONNECTED_API != getConnectedApi())) {
    if (true == isNeedConversionNow) {
        XLOGI("do convertToAuxSlot...");

        Slot &src = mSlots[mCurrentTexture];
        AuxSlot &dst = *mBackAuxSlot;

        // fence sync here for buffer not used by G3D
        EGLSyncKHR fence = mFrontAuxSlot->eglSlot.mEglFence;
        if (fence != EGL_NO_SYNC_KHR) {
            EGLint result = eglClientWaitSyncKHR(mEglDisplay, fence, 0, 1000000000);
            if (result == EGL_FALSE) {
                XLOGW("[%s] FAILED waiting for front fence: %#x, tearing risk", __func__, eglGetError());
            } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
                XLOGW("[%s] TIMEOUT waiting for front fence, tearing risk", __func__);
            }
            eglDestroySyncKHR(mEglDisplay, fence);
            mFrontAuxSlot->eglSlot.mEglFence = EGL_NO_SYNC_KHR;
        }

#ifdef USE_MDP
        uint32_t hal_out_fmt;
        uint32_t mdp_in_fmt;
        uint32_t mdp_out_fmt;

        //if (NATIVE_WINDOW_API_CAMERA == getConnectedApi()) {
        hal_out_fmt = HAL_PIXEL_FORMAT_RGBA_8888;
        // camera path needs RGBA for MDP resource
        mdp_out_fmt = MHAL_FORMAT_ABGR_8888;
        //} else {
        //    hal_out_fmt = HAL_PIXEL_FORMAT_YV12;
        //    mdp_out_fmt = MHAL_FORMAT_IMG_YV12;
        //}
        // !!! only convert for I420 now !!!
        mdp_in_fmt = MHAL_FORMAT_YUV_420;

        // source graphic buffer
        sp<GraphicBuffer> sg = src.mGraphicBuffer;

        // destination graphic buffer
        sp<GraphicBuffer> dg = dst.slot.mGraphicBuffer;

        // free if current aux slot exist and not fit
        if ((EGL_NO_IMAGE_KHR != dst.eglSlot.mEglImage && dg != NULL) &&
                ((sg->width != dg->width) || (sg->height != dg->height) || (hal_out_fmt != (uint32_t)dg->format))) {

            XLOGI("[%s] free old aux slot ", __func__);
            XLOGI("    src[w:%d, h:%d, f:0x%x] dst[w:%d, h:%d, f:0x%x] required format:0x%x",
                  sg->width, sg->height, sg->format,
                  dg->width, dg->height, dg->format,
                  hal_out_fmt);

            freeAuxSlotLocked(dst);
        }

        // create aux buffer if current is NULL
        if ((EGL_NO_IMAGE_KHR == dst.eglSlot.mEglImage) && (dst.slot.mGraphicBuffer == NULL)) {
            XLOGI("[%s] create dst buffer and image", __func__);

            XLOGI("    before create new aux buffer: %p", __func__, dg.get());
            dg = dst.slot.mGraphicBuffer = new GraphicBuffer(sg->width,
                    sg->height,
                    hal_out_fmt,
                    sg->usage);
            if (dg == NULL) {
                XLOGE("    create aux GraphicBuffer FAILED", __func__);
                freeAuxSlotLocked(dst);
                return BAD_VALUE;
            } else {
                XLOGI("    create aux GraphicBuffer: %p", __func__, dg.get());
            }

            dst.eglSlot.mEglImage = createImage(mEglDisplay, dg);
            if (EGL_NO_IMAGE_KHR == dst.eglSlot.mEglImage) {
                XLOGE("[%s] create aux eglImage FAILED", __func__);
                freeAuxSlotLocked(dst);
                return BAD_VALUE;
            }

            XLOGI("[%s] create aux slot success", __func__);
            XLOGI("    src[w:%d, h:%d, f:0x%x], dst[w:%d, h:%d, f:0x%x]",
                  sg->width, sg->height, sg->format,
                  dg->width, dg->height, dg->format);

            dst.mMva = registerMva(dg);
        }

        status_t lockret;
        uint8_t *src_yp, *dst_yp;

        lockret = sg->lock(LOCK_FOR_MDP, (void**)&src_yp);
        if (NO_ERROR != lockret) {
            XLOGE("[%s] buffer lock fail: %s", __func__, strerror(lockret));
            return INVALID_OPERATION;
        }
        lockret = dg->lock(LOCK_FOR_MDP, (void**)&dst_yp);
        if (NO_ERROR != lockret) {
            XLOGE("[%s] buffer lock fail: %s", __func__, strerror(lockret));
            return INVALID_OPERATION;
        }
        {
            mHalBltParam_t bltParam;
            memset(&bltParam, 0, sizeof(bltParam));

            bltParam.srcAddr    = (MUINT32)src_yp;
            bltParam.srcX       = 0;
            bltParam.srcY       = 0;
            bltParam.srcW       = sg->width;
            // !!! I420 content is forced 16 align !!!
            bltParam.srcWStride = ALIGN(sg->width, 16);
            bltParam.srcH       = sg->height;
            bltParam.srcHStride = sg->height;
            bltParam.srcFormat  = mdp_in_fmt;

            bltParam.dstAddr   = (MUINT32)dst_yp;
            bltParam.dstW      = dg->width;
            // already 32 align
            bltParam.pitch     = dg->stride;
            bltParam.dstH      = dg->height;
            bltParam.dstFormat = mdp_out_fmt;

#ifdef MTK_75DISPLAY_ENHANCEMENT_SUPPORT
            bltParam.doImageProcess = (NATIVE_WINDOW_API_MEDIA == getConnectedApi()) ? 1 : 0;
#endif

            // mdp bitblt and check
            if (MHAL_NO_ERROR != ipcBitBlt(&bltParam)) {
                if (1 == bltParam.doImageProcess) {
                    XLOGW("[%s] bitblt FAILED with PQ, disable and try again", __func__);
                    bltParam.doImageProcess = 0;
                    if (MHAL_NO_ERROR != ipcBitBlt(&bltParam)) {
                        XLOGE("[%s] bitblt FAILED, unlock buffer and return", __func__);
                        dst.slot.mGraphicBuffer->unlock();
                        src.mGraphicBuffer->unlock();
                        return INVALID_OPERATION;
                    }
                } else {
                    XLOGE("[%s] bitblt FAILED, unlock buffer and return", __func__);
                    dst.slot.mGraphicBuffer->unlock();
                    src.mGraphicBuffer->unlock();
                    return INVALID_OPERATION;
                }
            } else {
                // for drawing debug line
                if (true == mLine) {
                    int _stride = bltParam.pitch;
                    uint8_t *_ptr = (uint8_t*)bltParam.dstAddr;
                    static uint32_t offset = bltParam.dstH / 4;
                    //ST_XLOGI("!!!!! draw line, ptr: %p, offset: %d, stride: %d, height: %d", _ptr, offset, _stride, bltParam.dstH);
                    if (NULL != _ptr) {
                        memset((void*)(_ptr + offset * _stride * 3 / 2), 0xFF, _stride * 20 * 3 / 2);
                    }
                    offset += 20;
                    if (offset >= bltParam.dstH * 3 / 4)
                        offset = bltParam.dstH / 4;
                }
            }
        }
        dg->unlock();
        sg->unlock();

#else // ! USE_MDP
        status_t err = swConversionLocked(src, dst);
        if (NO_ERROR != err)
            return err;
#endif // USE_MDP

        mAuxSlotConvert = false;
        mAuxSlotDirty = true;
    }

    return NO_ERROR;
}
예제 #22
0
static int local_socket_enqueue(asocket *s, apacket *p)
{
    D("LS(%d): enqueue %d\n", s->id, p->len);

    p->ptr = p->data;

        /* if there is already data queue'd, we will receive
        ** events when it's time to write.  just add this to
        ** the tail
        */
    if(s->pkt_first) {
        goto enqueue;
    }

    //check if in the array
    bool bFound = false;
    int i = 0;
    for(i = 0; i < FD_ARRAY_SIZE; ++i){
        if(fd_write_array[i] == s->fd){
            //Do not need to update array
            bFound = true;
            break;
        }
    }

    if(bFound == false){
        if(fd_write_idx > FD_ARRAY_SIZE - 1){
            //Out of range, so we have to print fd directly
            XLOGD("Too many write adb socket fd=%d, ", s->fd);
        }else{
            fd_write_array[fd_write_idx] = s->fd;
            fd_write_idx++;
        }
    }

        /* write as much as we can, until we
        ** would block or there is an error/eof
        */
    while(p->len > 0) {
        int r = adb_write(s->fd, p->ptr, p->len);
        if(r > 0) {
            p->len -= r;
            p->ptr += r;
            write_data += r;
            continue;
        }
        if((r == 0) || (errno != EAGAIN)) {
            D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
            XLOGW( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
            s->close(s);
            return 1; /* not ready (error) */
        } else {
            break;
        }
    }

    clock_t this_write_time = clock();
    double cpu_time_used = ((double) (this_write_time - last_write_time)) / CLOCKS_PER_SEC;
    if( cpu_time_used > 10.0 )
    {
        char output_log[512];
        char tmp[16];
        sprintf(output_log, "%s", "adb socket write list ");
        int i = 0;
        for(i = 0; i < fd_write_idx; ++ i){
            sprintf(tmp, "(%d) ", fd_write_array[i]);
            strcat(output_log, tmp);
            //XLOGD("adb socket write fd=%d, ", fd_write_array[i]);
            fd_write_array[i] = 0;
        }
        XLOGD("%s\n", output_log);
        XLOGD("write_data=%d\n", write_data);
        last_write_time = this_write_time;
        write_data = 0;
        fd_write_idx = 0;
    }

    if(p->len == 0) {
        put_apacket(p);
        return 0; /* ready for more data */
    }

enqueue:
    p->next = 0;
    if(s->pkt_first) {
        s->pkt_last->next = p;
    } else {
        s->pkt_first = p;
    }
    s->pkt_last = p;

        /* make sure we are notified when we can drain the queue */
    fdevent_add(&s->fde, FDE_WRITE);

    return 1; /* not ready (backlog) */
}
예제 #23
0
static int create_subproc_pty(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
{
    D("create_subproc_pty(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
#ifdef HAVE_WIN32_PROC
    fprintf(stderr, "error: create_subproc_pty not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
    return -1;
#else /* !HAVE_WIN32_PROC */
    int ptm;

    ptm = unix_open("/dev/ptmx", O_RDWR | O_CLOEXEC); // | O_NOCTTY);
    if(ptm < 0){
        printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
        return -1;
    }

    char devname[64];
    if(grantpt(ptm) || unlockpt(ptm) || ptsname_r(ptm, devname, sizeof(devname)) != 0) {
        printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
        adb_close(ptm);
        return -1;
    }

    *pid = fork();
    if(*pid < 0) {
        printf("- fork failed: %s -\n", strerror(errno));
        XLOGW("- fork failed: pid(%d) %s -\n", *pid, strerror(errno));
        adb_close(ptm);
        return -1;
    }

    if (*pid == 0) {
        init_subproc_child();

        int pts = unix_open(devname, O_RDWR | O_CLOEXEC);
        if (pts < 0) {
            fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
            exit(-1);
        }

        dup2(pts, STDIN_FILENO);
        dup2(pts, STDOUT_FILENO);
        dup2(pts, STDERR_FILENO);

        adb_close(pts);
        adb_close(ptm);

        timer_t timerid;
        struct sigevent sev;
        /* Create the timer */
        sev.sigev_notify = SIGEV_SIGNAL;
        sev.sigev_signo = SIGALRM;
        sev.sigev_value.sival_ptr = &timerid;
        if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1)
        {
            D("adb: Call timer_create failed!\n");
            XLOGV("adb: Call timer_create failed!\n");
            exit(-1);
        }

        struct itimerspec its;
        /* Start the timer */
        its.it_value.tv_sec = 1;
        its.it_value.tv_nsec = 0;
        its.it_interval.tv_sec = 3;
        its.it_interval.tv_nsec = 0;

        if (timer_settime(timerid, 0, &its, NULL) == -1)
        {
            D("adb: Call timer_settime failed!\n");
            XLOGV("adb: Call timer_settime failed!\n");
            exit(-1);
        }

        sigset_t mask;
        struct sigaction sa;
        /* Establish handler for timer signal */
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = catcher;
        sigemptyset(&sa.sa_mask);
        if (sigaction(SIGALRM, &sa, NULL) == -1)
        {
            D("adb: Call sigaction failed!\n");
            XLOGV("adb: Call sigaction failed!\n");
            exit(-1);
        }

        int nRet = execl(cmd, cmd, arg0, arg1, NULL);
        fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
                cmd, strerror(errno), errno);
        XLOGV("- exec '%s' failed: %s (%d) nRet(%d) -\n",
                cmd, strerror(errno), errno, nRet);
        exit(-1);
    } else {
        return ptm;
    }
#endif /* !HAVE_WIN32_PROC */
}
예제 #24
0
void catcher()
{
    D("Enter catcher : wait too long in create_subprocess\n");
    XLOGW("Enter catcher : wait too long in create_subprocess\n");
    exit(-1);
}
예제 #25
0
static void local_socket_event_func(int fd, unsigned ev, void *_s)
{
    asocket *s = _s;

    D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev);

    /* put the FDE_WRITE processing before the FDE_READ
    ** in order to simplify the code.
    */
    if(ev & FDE_WRITE){
        //check if in the array
        bool bFound = false;
        int i = 0;
        for(i = 0; i < FD_ARRAY_SIZE; ++i){
            if(fd_write_array[i] == fd){
                //Do not need to update array
                bFound = true;
                break;
            }
        }

        if(bFound == false){
            if(fd_write_idx > FD_ARRAY_SIZE - 1){
                //Out of range, so we have to print fd directly
                XLOGD("Too many write adb socket fd=%d, ", fd);
            }else{
                fd_write_array[fd_write_idx] = fd;
                fd_write_idx++;
            }
        }

        apacket *p;

        while((p = s->pkt_first) != 0) {
            while(p->len > 0) {
                int r = adb_write(fd, p->ptr, p->len);
                if(r > 0) {
                    p->ptr += r;
                    p->len -= r;
                    write_data += r;
                    continue;
                }
                if(r < 0) {
                    /* returning here is ok because FDE_READ will
                    ** be processed in the next iteration loop
                    */
                    if(errno == EAGAIN) return;
                    if(errno == EINTR) continue;
                }
                D(" closing after write because r=%d and errno is %d\n", r, errno);
                XLOGW(" closing after write because r=%d and errno is %d\n", r, errno);
                s->close(s);
                return;
            }

            clock_t this_write_time = clock();
            double cpu_time_used = ((double) (this_write_time - last_write_time)) / CLOCKS_PER_SEC;
            if( cpu_time_used > 10.0 )
            {
                char output_log[512];
                char tmp[16];
                sprintf(output_log, "%s", "adb socket write list ");
                int i = 0;
                for(i = 0; i < fd_write_idx; ++ i){
                    sprintf(tmp, "(%d) ", fd_write_array[i]);
                    strcat(output_log, tmp);
                    //XLOGD("adb socket write fd=%d, ", fd_write_array[i]);
                    fd_write_array[i] = 0;
                }
                XLOGD("%s\n", output_log);
                XLOGD("write_data=%d\n", write_data);
                last_write_time = this_write_time;
                write_data = 0;
                fd_write_idx = 0;
            }
            if(p->len == 0) {
                s->pkt_first = p->next;
                if(s->pkt_first == 0) s->pkt_last = 0;
                put_apacket(p);
            }
        }

            /* if we sent the last packet of a closing socket,
            ** we can now destroy it.
            */
        if (s->closing) {
            D(" closing because 'closing' is set after write\n");
            XLOGW(" closing because 'closing' is set after write\n");
            s->close(s);
            return;
        }

            /* no more packets queued, so we can ignore
            ** writable events again and tell our peer
            ** to resume writing
            */
        fdevent_del(&s->fde, FDE_WRITE);
        s->peer->ready(s->peer);
    }

    if(ev & FDE_READ){
        //check if in the array
        bool bFound = false;
        int i = 0;
        for(i = 0; i < FD_ARRAY_SIZE; ++i){
            if(fd_read_array[i] == fd){
                //Do not need to update array
                bFound = true;
                break;
            }
        }

        if(bFound == false){
            if(fd_read_idx > FD_ARRAY_SIZE - 1){
                //Out of range, so we have to print directly
                XLOGD("Too many read adb socket fd=%d, ", fd);
            }else{
                fd_read_array[fd_read_idx] = fd;
                fd_read_idx++;
            }
        }

        apacket *p = get_apacket();
        unsigned char *x = p->data;
        size_t avail = MAX_PAYLOAD;
        int r;
        int is_eof = 0;

        clock_t time1 = clock();
        while(avail > 0) {
            r = adb_read(fd, x, avail);
            D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail);
            if(r > 0) {
                avail -= r;
                x += r;
                read_data += r;
                continue;
            }
            if(r < 0) {
                if(errno == EAGAIN) break;
                if(errno == EINTR) continue;
            }

                /* r = 0 or unhandled error */
            is_eof = 1;
            break;
        }

        clock_t this_read_time = clock();
        double cpu_time_used = ((double) (this_read_time - last_read_time)) / CLOCKS_PER_SEC;
        if( cpu_time_used > 5.0 )
        {
            char output_log[512];
            char tmp[16];
            sprintf(output_log, "%s", "adb socket read list ");
            int i = 0;
            for(i = 0; i < fd_read_idx; ++ i){
                sprintf(tmp, "(%d) ", fd_read_array[i]);
                strcat(output_log, tmp);
                //XLOGD("adb socket read fd=%d, ", fd_read_array[i]);
                fd_read_array[i] = 0;
            }
            XLOGD("%s\n", output_log);
            XLOGD("read_data=%d\n", read_data);
            last_read_time = this_read_time;
            read_data = 0;
            fd_read_idx = 0;
        }

        D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n",
          s->id, s->fd, r, is_eof, s->fde.force_eof);
        clock_t time2 = clock();
        if ( time2 - time1 > max_read_time ){
            max_read_time = time2 - time1;
        }

        if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
            put_apacket(p);
        } else {
            p->len = MAX_PAYLOAD - avail;

            r = s->peer->enqueue(s->peer, p);
            D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r);
            clock_t time3 = clock();
            if( time3 - time2 > max_send_time){
                max_send_time = time3 - time2;
            }

            if(r < 0) {
                    /* error return means they closed us as a side-effect
                    ** and we must return immediately.
                    **
                    ** note that if we still have buffered packets, the
                    ** socket will be placed on the closing socket list.
                    ** this handler function will be called again
                    ** to process FDE_WRITE events.
                    */
                return;
            }

            if(r > 0) {
                    /* if the remote cannot accept further events,
                    ** we disable notification of READs.  They'll
                    ** be enabled again when we get a call to ready()
                    */
                fdevent_del(&s->fde, FDE_READ);
            }
        }

        if( cpu_time_used > 5.0 ) {
            double read_time = ((double) max_read_time) / CLOCKS_PER_SEC;
            double send_time = ((double) max_send_time) / CLOCKS_PER_SEC;
            XLOGD("max_read_time=%f max_send_time=%f \n", read_time, send_time);
            max_read_time = 0;
            max_send_time = 0;
        }

        /* Don't allow a forced eof if data is still there */
        if((s->fde.force_eof && !r) || is_eof) {
            D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
            XLOGD(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof);
            s->close(s);
        }
    }

    if(ev & FDE_ERROR){
            /* this should be caught be the next read or write
            ** catching it here means we may skip the last few
            ** bytes of readable data.
            */
//        s->close(s);
        D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
        XLOGD("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
        return;
    }
}
예제 #26
0
static int smart_socket_enqueue(asocket *s, apacket *p)
{
    unsigned len;
#if ADB_HOST
    char *service = NULL;
    char* serial = NULL;
    transport_type ttype = kTransportAny;
#endif

    D("SS(%d): enqueue %d\n", s->id, p->len);

    if(s->pkt_first == 0) {
        s->pkt_first = p;
        s->pkt_last = p;
    } else {
        if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
            D("SS(%d): overflow\n", s->id);
            XLOGW("SS(%d): overflow\n", s->id);
            put_apacket(p);
            goto fail;
        }

        memcpy(s->pkt_first->data + s->pkt_first->len,
               p->data, p->len);
        s->pkt_first->len += p->len;
        put_apacket(p);

        p = s->pkt_first;
    }

        /* don't bother if we can't decode the length */
    if(p->len < 4) return 0;

    len = unhex(p->data, 4);
    if((len < 1) ||  (len > 1024)) {
        D("SS(%d): bad size (%d)\n", s->id, len);
        XLOGW("SS(%d): bad size (%d)\n", s->id, len);
        goto fail;
    }

    D("SS(%d): len is %d\n", s->id, len );
        /* can't do anything until we have the full header */
    if((len + 4) > p->len) {
        D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
        return 0;
    }

    p->data[len + 4] = 0;

    D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));

#if ADB_HOST
    service = (char *)p->data + 4;
    if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
        char* serial_end;
        service += strlen("host-serial:");

        // serial number should follow "host:" and could be a host:port string.
        serial_end = skip_host_serial(service);
        if (serial_end) {
            *serial_end = 0; // terminate string
            serial = service;
            service = serial_end + 1;
        }
    } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
        ttype = kTransportUsb;
        service += strlen("host-usb:");
    } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
        ttype = kTransportLocal;
        service += strlen("host-local:");
    } else if (!strncmp(service, "host:", strlen("host:"))) {
        ttype = kTransportAny;
        service += strlen("host:");
    } else {
        service = NULL;
    }

    if (service) {
        asocket *s2;

            /* some requests are handled immediately -- in that
            ** case the handle_host_request() routine has sent
            ** the OKAY or FAIL message and all we have to do
            ** is clean up.
            */
        if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
                /* XXX fail message? */
            D( "SS(%d): handled host service '%s'\n", s->id, service );
            goto fail;
        }
        if (!strncmp(service, "transport", strlen("transport"))) {
            D( "SS(%d): okay transport\n", s->id );
            p->len = 0;
            return 0;
        }

            /* try to find a local service with this name.
            ** if no such service exists, we'll fail out
            ** and tear down here.
            */
        s2 = create_host_service_socket(service, serial);
        if(s2 == 0) {
            D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
            sendfailmsg(s->peer->fd, "unknown host service");
            goto fail;
        }

            /* we've connected to a local host service,
            ** so we make our peer back into a regular
            ** local socket and bind it to the new local
            ** service socket, acknowledge the successful
            ** connection, and close this smart socket now
            ** that its work is done.
            */
        adb_write(s->peer->fd, "OKAY", 4);

        s->peer->ready = local_socket_ready;
        s->peer->close = local_socket_close;
        s->peer->peer = s2;
        s2->peer = s->peer;
        s->peer = 0;
        D( "SS(%d): okay\n", s->id );
        s->close(s);

            /* initial state is "ready" */
        s2->ready(s2);
        return 0;
    }
#else /* !ADB_HOST */
    if (s->transport == NULL) {
        char* error_string = "unknown failure";
        s->transport = acquire_one_transport (CS_ANY,
                kTransportAny, NULL, &error_string);

        if (s->transport == NULL) {
            sendfailmsg(s->peer->fd, error_string);
            goto fail;
        }
    }
#endif

    if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
           /* if there's no remote we fail the connection
            ** right here and terminate it
            */
        sendfailmsg(s->peer->fd, "device offline (x)");
        goto fail;
    }


        /* instrument our peer to pass the success or fail
        ** message back once it connects or closes, then
        ** detach from it, request the connection, and
        ** tear down
        */
    s->peer->ready = local_socket_ready_notify;
    s->peer->close = local_socket_close_notify;
    s->peer->peer = 0;
        /* give him our transport and upref it */
    s->peer->transport = s->transport;

    connect_to_remote(s->peer, (char*) (p->data + 4));
    s->peer = 0;
    s->close(s);
    return 1;

fail:
        /* we're going to close our peer as a side-effect, so
        ** return -1 to signal that state to the local socket
        ** who is enqueueing against us
        */
    XLOGW("SS(%d): smart_socket_enqueue fail\n");
    s->close(s);
    return -1;
}
예제 #27
0
static int remote_read(apacket *p, atransport *t)
{
    debuginfo dbg;
    unsigned char *x;
    unsigned msg_sum;
    unsigned data_sum;
    unsigned count;
    static unsigned dgb_count = 0;
    dbg.command = A_DBUG;
    dbg.headtoken = DBGHEADTOKEN;
    dbg.tailtoken = DBGTAILTOKEN;

    if(usb_read(t->usb, &p->msg, sizeof(amessage))){
        XLOGW("remote usb: read terminated (message)\n");
        D("remote usb: read terminated (message)\n");
        return -1;
    }

    fix_endians(p);

    if(check_header(p)) {
        XLOGW("remote usb: check_header failed\n");
        D("remote usb: check_header failed\n");

        //__ADB_DEBUG__ start
        if(bitdebug_enabled == 1){
            if(usb_read(t->usb, &dbg, sizeof(debuginfo))){
                //XLOGW("remote usb: read terminated (debuginfo)\n");
                //D("remote usb: read terminated (debuginfo)\n");
                //return -1;
            }

            count = sizeof(amessage);
            x = (unsigned char *) &p->msg;
            msg_sum = 0;
            while(count-- > 0){
                msg_sum ^= *x++;
            }

            if(dbg.msg_check != msg_sum)
                XLOGW("usb_adb_read dbg: ERROR msg checksum, cmd = (0x%x), msg_sum = (0x%x),  dbg.msg_check = (0x%x) \n", dbg.command, msg_sum, dbg.msg_check);
            //else
            //    XLOGW("usb_adb_read dbg: msg checksum match \n");
            //__ADB_DEBUG__ end
        }

        return -1;
    }

    if(p->msg.data_length) {
        if(usb_read(t->usb, p->data, p->msg.data_length)){
            XLOGW("remote usb: terminated (data)\n");
            D("remote usb: terminated (data)\n");
            return -1;
        }
    }

    //__ADB_DEBUG__ start
    if(bitdebug_enabled == 1){
        if(usb_read(t->usb, &dbg, sizeof(debuginfo))){
            //XLOGW("remote usb: read terminated (debuginfo)\n");
            //D("remote usb: read terminated (debuginfo)\n");
            //return -1;
        }

        count = sizeof(amessage);
        x = (unsigned char *) &p->msg;
        msg_sum = 0;
        while(count-- > 0){
            msg_sum ^= *x++;
        }

        count = p->msg.data_length;
        x = (unsigned char *) p->data;
        data_sum = 0;
        while(count-- > 0){
            data_sum ^= *x++;
        }

        if(dbg.msg_check != msg_sum)
            XLOGW("usb_adb_read dbg: ERROR msg checksum, cmd = (0x%x), msg_sum = (0x%x),  dbg.msg_check = (0x%x) \n", dbg.command, msg_sum, dbg.msg_check);
        //else
        //    XLOGW("usb_adb_read dbg: msg checksum match \n");

        if(dbg.data_check != data_sum)
            XLOGW("usb_adb_read dbg: ERROR data checksum, cmd = (0x%x), data_sum = (0x%x),  dbg.data_check = (0x%x) \n", dbg.command, data_sum, dbg.data_check);
        //else
        //    XLOGW("usb_adb_read dbg: data checksum match \n");

        if (dgb_count != dbg.count)
          XLOGW("usb_adb_read dbg: Warning: miss count = %d, dbg.count = %d \n", dgb_count, dbg.count);
        dgb_count++;
    }
    else
    {
      if (0 != dgb_count)
        dgb_count = 0;
    }
    //__ADB_DEBUG__ end

    if(check_data(p)) {
        XLOGW("remote usb: check_data failed\n");
        D("remote usb: check_data failed\n");
        return -1;
    }

    return 0;
}
예제 #28
0
static int remote_write(apacket *p, atransport *t)
{
    unsigned size = p->msg.data_length;
    static unsigned dgb_count = 0;

    fix_endians(p);

    if(usb_write(t->usb, &p->msg, sizeof(amessage))) {
        XLOGW("remote usb: 1 - write terminated\n");
        D("remote usb: 1 - write terminated\n");
        return -1;
    }
    //if(p->msg.data_length == 0) return 0;
    if(p->msg.data_length != 0)
    if(usb_write(t->usb, &p->data, size)) {
        XLOGW("remote usb: 2 - write terminated\n");
        D("remote usb: 2 - write terminated\n");
        return -1;
    }

    //__ADB_DEBUG__ start
    if(bitdebug_enabled == 1){
        debuginfo dbg;
        dbg.command = A_DBUG;
        dbg.headtoken = DBGHEADTOKEN;
        dbg.tailtoken = DBGTAILTOKEN;
        unsigned char *x;
        unsigned sum;
        unsigned count;

        count = p->msg.data_length;
        x = (unsigned char *) p->data;
        sum = 0;
        while(count-- > 0){
            sum ^= *x++;
        }
        dbg.data_check = sum;

        count = sizeof(amessage);
        x = (unsigned char *) &p->msg;
        sum = 0;
        while(count-- > 0){
            sum ^= *x++;
        }
        dbg.msg_check = sum;
        dbg.count = dgb_count;
        if(usb_write(t->usb, &dbg, sizeof(debuginfo))) {
            XLOGW("remote usb: 3 - write terminated\n");
            D("remote usb: 3 - write terminated\n");
            return -1;
        }

        //XLOGW("remote_write debuginfo dgb_count = %d \n", dgb_count);
        dgb_count++;
    }
    else{
        //Have run debug before, turn off now
        if(dgb_count != 0){
            debuginfo dbg;
            dbg.command = A_DBUG;
            dbg.headtoken = DBGHEADTOKEN;
            dbg.tailtoken = DBGTAILTOKEN;
            dbg.count = -1;

            if(usb_write(t->usb, &dbg, sizeof(debuginfo))) {
                XLOGW("remote usb: 3 - write terminated\n");
                D("remote usb: 3 - write terminated\n");
                return -1;
            }
            dgb_count = 0;
        }
    }
    //__ADB_DEBUG__ end

    return 0;
}