如何在Android中使用unix管道

发布于 2024-11-08 11:41:38 字数 1359 浏览 0 评论 0原文

我需要从 Android 中的应用程序向 C 程序发送一些数据,我考虑使用管道。我读到 Java 可以访问现有管道(并将它们打开为如果它是一个普通文件),但我无法在我的应用程序中做这样的事情。当我尝试时,应用程序只是阻塞,直到出现消息等待关闭,而不在 logcat 上编写任何特殊内容。

我在 Android 邮件列表上发现了帖子关于这个主题,但不是很清楚,它指的是我手机上不存在的文件夹。

此外,我知道不可能在 sdcard 上创建管道,但是当我尝试在 /data 中这样做时,我认为我遇到了根本问题......你知道是否可以访问该管道管道(我尝试进出应用程序文件夹但没有成功)?

我用 mkfifo 创建了管道,并且权限似乎可以由任何用户打开。

prw-rw-rw- root     root              2010-11-18 04:53 video_pipe

我尝试添加 X 权限(谁知道......)这是我得到的:

# chmod u+x video_pipe 
Bad mode

阻止的代码是 相机初始化PATH 只是管道的路径):

recorder.setOutputFile(PATH);

这是整个源代码:https://github.com/rbochet/Simple-Camera-App/commits/piped< /a> (提交 22dba257f6)

I need to send some data to a C program from my app in Android, and I think about using pipes. I read that Java can access to existing pipes (and open them as if it's a normal file), but I'm unable to do such a thing in my application. When I try, the app just block until the message wait close appears, without writing anything special on logcat.

I found a thread on android mailing lists about this subject, but it was not very clear, and it refers to a folder that does not exist on my phone.

Furthermore, I know it's not possible to make pipes on the sdcard, but when I try to do so in/data, I think I have root issues… Do you know if it is possible to access to that pipe (I try in and out of the app folder without success)?

I made the pipe with mkfifo, and the permissions seems OK to be open by any user.

prw-rw-rw- root     root              2010-11-18 04:53 video_pipe

I tried to add the X permission (who knows...) Here is what I have back:

# chmod u+x video_pipe 
Bad mode

The code that blocks is the camera initialisation (PATH is just the path to the pipe):

recorder.setOutputFile(PATH);

Here is the whole source : https://github.com/rbochet/Simple-Camera-App/commits/piped (commit 22dba257f6)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-11-15 11:41:38

好吧,我尝试用现有的最愚蠢的应用程序来解决这个问题。您可以在 github 上找到这个要点

到目前为止,我发现:

  • 管道工作的唯一地方是应用程序文件夹(即/data/data/package.full.name/
  • 如果您想将数据传递给另一个程序,您可以最好将其作为应用程序的子项启动,以确保它们位于同一组中,从而对该文件夹具有相同的授权。如果不能,您也许可以使用组(在 /data/data/ 上执行 ls -l -a 并查看组名称)。

不要忘记:除非有人在另一边聆听,否则您无法真正在管道中写入。因此,如果你测试我在 github 上发布的文件,你将得到那种 logcat 结果。

I/ActivityManager(  220): Start proc fr.stackr.android.upt for activity fr.stackr.android.upt/.UnixPipeActivity: pid=1359 uid=10048 gids={}
I/UPIPE   ( 1359): Attempt started
W/ActivityManager(  220): Launch timeout has expired, giving up wake lock!
W/ActivityManager(  220): Activity idle timeout for HistoryRecord{4643c8b8 fr.stackr.android.upt/.UnixPipeActivity}

在这里,系统暂停了,因为什么也没发生……然后我在手机上运行 cat v_pipe

V/UPIPE   ( 1359): SEND :: Try to write the first paragraph ....
V/UPIPE   ( 1359): SEND :: Bip
V/UPIPE   ( 1359): Flushing...
V/UPIPE   ( 1359): SEND :: Bip post flush
V/UPIPE   ( 1359): Closing…
I/UPIPE   ( 1359): Attempt ended

这样就完成了。

关闭:当我关闭OutputStreamWriter时,监听端(即cat)结束。
如果我注释该行,cat 仍将等待输入。

冲洗:如果你打算在不调用 close 的情况下获得某些东西,这似乎很重要。

回车:使用\n

Ok, I tried to fix the problem with the most stupid app that exists. You can find this one as a gist on github.

So far, I discover this :

  • The only place where the pipe works is the app folder (ie /data/data/package.full.name/)
  • If you want to pass data to another program, you had better to launch it as a child of your app to ensure they are in the same group and thus have the same authorisation for the folder. If you can't, you might be able to play with the groups (do ls -l -a on /data/data/ and have a look to the group name).

DO NOT FORGET : You can't actually write in the pipe until someone is listening at the other side. So if you test the file I posted on github, you will have that kind of logcat result.

I/ActivityManager(  220): Start proc fr.stackr.android.upt for activity fr.stackr.android.upt/.UnixPipeActivity: pid=1359 uid=10048 gids={}
I/UPIPE   ( 1359): Attempt started
W/ActivityManager(  220): Launch timeout has expired, giving up wake lock!
W/ActivityManager(  220): Activity idle timeout for HistoryRecord{4643c8b8 fr.stackr.android.upt/.UnixPipeActivity}

Here, the system pause because nothing happens… Then I run cat v_pipe on the phone.

V/UPIPE   ( 1359): SEND :: Try to write the first paragraph ....
V/UPIPE   ( 1359): SEND :: Bip
V/UPIPE   ( 1359): Flushing...
V/UPIPE   ( 1359): SEND :: Bip post flush
V/UPIPE   ( 1359): Closing…
I/UPIPE   ( 1359): Attempt ended

That's done.

closing : when I close the OutputStreamWriter, the listening side (ie cat) ends.
If I commment the line, cat will still wait for input.

flushing : seems to be important if you intent to get something without calling close.

Carriage Return : Use \n.

烛影斜 2024-11-15 11:41:38

我认为你可以使用 ParcelFileDescriptor.createPipe()

它将返回一个用于读写的管道数组。欲了解更多信息,请访问开发者网站。

I think you can use ParcelFileDescriptor.createPipe()

It will return an array of pipe for read and write. For more information, visit the developers website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文