免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1906 | 回复: 1
打印 上一主题 下一主题

[Android] 求解超级终端以dialog形式嵌入程序中遇到问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-22 17:18 |只看该作者 |倒序浏览
        我把超级终端以dialog形式嵌入我的demo的时候没有问题,但是嵌入整个项目就出现了问题,最终发现是与打开网络服务的操作有冲突,调用这个方法,就会导致excel命令不能运行。我吧我的超级终端改得跟android的源码基本一致,只是吧excel命令换成了excev,然后他们的参数不一样。最终测出来是进程冲突了,网络服务的进程一直是实时进程,而超级终端也会一直在wait for 终端输入命令后,等待执行命令完全执行完,相当于也是实时进程(这是我的猜测,对linux半窍不通),结果就是只要一开启网络服务,dialog模式的超级终端就运行不起来。卡死在excel命令那里。
     我想求教哪位大虾能帮我指出确实的问题所在,最好是能指点一下我解决的方法,跪谢!

论坛徽章:
0
2 [报告]
发表于 2012-10-22 17:20 |只看该作者
抱歉,在此附上源码
static int create_subprocess(const char *cmd,
    char *const argv[], char *const envp[], int* pProcessId)
{
    char *devname;
    int ptm;
    pid_t pid;
        LOGE("create_subprocess:%s");
    ptm = open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
    if(ptm < 0){
        LOGE("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
        return -1;
    }
    LOGE(" fcntl");
    fcntl(ptm, F_SETFD, FD_CLOEXEC);

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

    if(pid == 0){
        close(ptm);

        int pts;
        LOGE(" setsid");
        setsid();
                LOGE(" pts");
        pts = open(devname, O_RDWR);
        if(pts < 0) exit(-1);
                LOGE(" dup2");
        dup2(pts, 0);
        dup2(pts, 1);
        dup2(pts, 2);
                LOGE(" envp");
        if (envp) {
            for (; *envp; ++envp) {
                putenv(*envp);
            }
        }
                LOGE(" execv");
        //execv(cmd, argv);
        if(execv(cmd, argv)<0){  
            LOGE("Err on execv");  }
            else{
            LOGE("NO Err on execv");
            }        
        exit(-1);
    } else {
        *pProcessId = (int) pid;
        return ptm;
    }
}


static jobject android_os_Exec_createSubProcess(JNIEnv *env, jobject clazz,
    jstring cmd, jobjectArray args, jobjectArray envVars,
    jintArray processIdArray)
{
    const jchar* str = cmd ? env->GetStringCritical(cmd, 0) : 0;
    String8 cmd_8;
         LOGE("android_os_Exec_createSubProcess");
         LOGE("1");
    if (str) {
             LOGE("1");
        cmd_8.set(str, env->GetStringLength(cmd));
        
        env->ReleaseStringCritical(cmd, str);
    }
       LOGE(" size");                              
    jsize size = args ? env->GetArrayLength(args) : 0;
    char **argv = NULL;
    String8 tmp_8;
    if (size > 0) {
      LOGE(" size>0 argv");  
        argv = (char **)malloc((size+1)*sizeof(char *));
        if (!argv) {
            throwOutOfMemoryError(env, "Couldn't allocate argv array");
            return NULL;
        }
        for (int i = 0; i < size; ++i) {
         LOGE(" size>0 args");  
            jstring arg = reinterpret_cast<jstring>(env->GetObjectArrayElement(args, i));
            str = env->GetStringCritical(arg, 0);
            if (!str) {
                throwOutOfMemoryError(env, "Couldn't get argument from array");
                return NULL;
            }
             LOGE(" tmp_8 arg");  
            tmp_8.set(str, env->GetStringLength(arg));
             LOGE(" ReleaseStringCritical");  
            env->ReleaseStringCritical(arg, str);  
              LOGE(" argv[i] = strdup(tmp_8.string())");                              
            argv[i] = strdup(tmp_8.string());
            LOGE(" argv[OK] = strdup(tmp_8.string())");
        }
        argv[size] = NULL;
          LOGE("  argv[size] = NULL;");
    }

    size = envVars ? env->GetArrayLength(envVars) : 0;
     LOGE("  size = envVars ? env->GetArrayLength(envVars) : 0;");
    char **envp = NULL;
    if (size > 0) {
     LOGE("envVars  size > 0");
        envp = (char **)malloc((size+1)*sizeof(char *));
          LOGE(" envp = (char **)malloc((size+1)*sizeof(char *))");
        if (!envp) {
            throwOutOfMemoryError(env, "Couldn't allocate envp array");
            return NULL;
        }
        for (int i = 0; i < size; ++i) {
            jstring var = reinterpret_cast<jstring>(env->GetObjectArrayElement(envVars, i));
            str = env->GetStringCritical(var, 0);
             LOGE(" envVars   str = env->GetStringCritical(var, 0 ");
            if (!str) {
                throwOutOfMemoryError(env, "Couldn't get env var from array");
                return NULL;
            }
            tmp_8.set(str, env->GetStringLength(var));
              LOGE(" envVars  tmp_8.set(str, env->GetStringLength(var)) ");
            env->ReleaseStringCritical(var, str);
              LOGE("   env->ReleaseStringCritical(var, str) ");
            envp[i] = strdup(tmp_8.string());
               LOGE("  envp[i] = strdup(tmp_8.string()); ");
        }
        envp[size] = NULL;
    }

    int procId;
     LOGE(" ptm ");
    int ptm = create_subprocess(cmd_8.string(), argv, envp, &procId);
LOGE("   create_subprocess(cmd_8.string(), argv, envp, &procId) ");
    if (argv) {
      LOGE(" if (argv)  ");
        for (char **tmp = argv; *tmp; ++tmp) {
            free(*tmp);
        }
        free(argv);
           LOGE("  free(argv) ");
    }
    if (envp) {
     LOGE(" envp ");
        for (char **tmp = envp; *tmp; ++tmp) {
            free(*tmp);
        }
        free(envp);
          LOGE(" free(envp) ");
    }

    if (processIdArray) {
     LOGE(" if (processIdArray) ");
        int procIdLen = env->GetArrayLength(processIdArray);
        if (procIdLen > 0) {
            jboolean isCopy;
  LOGE("  if (procIdLen > 0) ");
            int* pProcId = (int*) env->GetPrimitiveArrayCritical(processIdArray, &isCopy);
            if (pProcId) {
             LOGE("  if (pProcId)");
                *pProcId = procId;
                env->ReleasePrimitiveArrayCritical(processIdArray, pProcId, 0);
            }
        }
    }
         LOGE(" result");
    jobject result = env->NewObject(class_fileDescriptor, method_fileDescriptor_init);

        LOGE("11111111");
    if (!result) {
        LOGE("Couldn't create a FileDescriptor.");
    }
    else {
        env->SetIntField(result, field_fileDescriptor_descriptor, ptm);
                LOGE("22222222");
    }
    return result;
       
}


static void android_os_Exec_setPtyWindowSize(JNIEnv *env, jobject clazz,
    jobject fileDescriptor, jint row, jint col, jint xpixel, jint ypixel)
{
    int fd;
    struct winsize sz;
LOGE("android_os_Exec_setPtyWindowSize");
    fd = env->GetIntField(fileDescriptor, field_fileDescriptor_descriptor);

    if (env->ExceptionOccurred() != NULL) {
        return;
    }

    sz.ws_row = row;
    sz.ws_col = col;
    sz.ws_xpixel = xpixel;
    sz.ws_ypixel = ypixel;

    ioctl(fd, TIOCSWINSZ, &sz);
}

static void android_os_Exec_setPtyUTF8Mode(JNIEnv *env, jobject clazz,
    jobject fileDescriptor, jboolean utf8Mode)
{
    int fd;
    struct termios tios;
LOGE("android_os_Exec_setPtyUTF8Mode");
    fd = env->GetIntField(fileDescriptor, field_fileDescriptor_descriptor);

    if (env->ExceptionOccurred() != NULL) {
        return;
    }

    tcgetattr(fd, &tios);
    if (utf8Mode) {
        tios.c_iflag |= IUTF8;
    } else {
        tios.c_iflag &= ~IUTF8;
    }
    tcsetattr(fd, TCSANOW, &tios);
}

static int android_os_Exec_waitFor(JNIEnv *env, jobject clazz,
    jint procId) {
    int status;
        LOGE("android_os_Exec_waitFor");
    waitpid(procId, &status, 0);
    int result = 0;
    if (WIFEXITED(status)) {
        result = WEXITSTATUS(status);
    }
    return result;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP