Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I understand that /proc filesystem reflects the output of various processes. Unfortunately, I have a propietary (romdump) binary that expects the mount table to appear as /proc/mtd, while my Android device appears to output it as /proc/mounts.

I've tried creating a symbolic link, but clearly this only works for actual files or directories. How can I fool this binary to read the output from /proc/mounts instead of /proc/mtd?

share|improve this question
1  
Are you sure? Why would it do that? –  Stephane Chazelas Nov 27 '12 at 23:05
    
Related: Is it possible to create a directory and file inside /proc/sys? where I show how to use unionfs (I don't remember whether that's possible out-of-the-box on Android). –  Gilles Nov 28 '12 at 0:23
add comment

4 Answers

up vote 6 down vote accepted

The easiest way to do it would be to change the binary:

sed s-/proc/mtd-/tmp/mntx- < romdump > romdump.new
ln -s /proc/mounts /tmp/mntx
./romdump.new

The trick here, since you're editing a binary, is to make sure the original string /proc/mtd is the same length as the new string /tmp/mntx, so that you don't change the size or location of anything in the binary.

This is not foolproof—it will not work if the binary builds up the path name in pieces rather than using a fixed string. But it's likely to do the trick.

share|improve this answer
    
Clever way of getting it done. Unfortunately ln is giving "link filed File exists", while /tmp/mntx doesn't. (In fact, I had to create the /tmp directory.) –  Paul Lammertsma Nov 29 '12 at 10:13
    
You could use another path too if /tmp isn't working, like /etc. You also don't need a symlink at all - just copy the contents of /proc/mounts into your new file. –  Jim Paris Nov 29 '12 at 15:11
    
Oh, also, my ln command was backwards, sorry. Fixed! –  Jim Paris Nov 29 '12 at 15:14
    
Silly that I overlooked that, too. Thanks, this fix works like a charm! –  Paul Lammertsma Nov 29 '12 at 16:09
add comment

I don't know of a way of adding things to /proc outside of writing a module (or plain kernel code). Might be some utilities out there though.

If you can build and insert a module, then it's pretty simple: you can just create another symlink (/proc/mounts is a symlink already).

Source (mnt_link.c):

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>

#define MODULE_VERS "0.0"
#define MODULE_NAME "mnt_link"

static int __init init_mnt_link(void)
{
    static struct proc_dir_entry *symlink;
    symlink = proc_symlink("mnt", NULL, "self/mounts");
    if(!symlink)
        return -ENOMEM;
    return 0;
}

static void __exit cleanup_mnt_link(void)
{
    remove_proc_entry("mnt", NULL);
}

module_init(init_mnt_link);
module_exit(cleanup_mnt_link);

MODULE_AUTHOR("U&L");
MODULE_LICENSE("CC-WIKI");
MODULE_DESCRIPTION("Create a /proc/mnt symlink to /proc/self/mounts");

Makefile:

obj-m := mnt_link.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

(This assumes you'll be building for your current Linux system. To build something for Android, you can refer to: How do you create a loadable kernel module for Android?.)

Once you've loaded the module (insmod mnt_link.ko), you should get:

$ ls -l /proc/m*nt*
lrwxrwxrwx 1 root root 11 Nov 27 22:43 /proc/mnt -> self/mounts
lrwxrwxrwx 1 root root 11 Nov 27 22:43 /proc/mounts -> self/mounts

That being said, that utility of yours might very well be expecting something else than this symlink. (Perhaps it depends on another module being loaded to provide some information at that location.)

Use at your own risks.

share|improve this answer
    
Wow, that is a hell of an answer! Thanks for not only giving a resolution, but also giving insight into symlinks and how to write them. –  Paul Lammertsma Nov 29 '12 at 9:56
    
@Mat This module will taint the kernel as CC-WIKI is not a known GPL-compatible license to the kernel (see include/linux/license.h). –  Lekensteyn Oct 18 '13 at 22:05
add comment

The /proc filesystem exists in memory so to speak. See http://www.kernel.org/doc/Documentation/filesystems/proc.txt for more info. Links probably won't work.

You might be able to use LD_PRELOAD to intercept the open() function. See http://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick

You might also be able to edit the binary to change the file name from /proc/mnt to /proc_mnt and symlink that to /proc/mounts .

Another option would be to recompile the kernel and make /proc/mnt an alias for /proc/mounts .

share|improve this answer
add comment

Have you tried a hard link?

ln /proc/mnt /proc/mounts
share|improve this answer
    
I'm afraid this returns with link failed No such file or directory. –  Paul Lammertsma Nov 27 '12 at 21:16
    
try reversing it.. –  Mark Cohen Nov 27 '12 at 21:30
3  
Won't work. You can't create files under /proc except via kernel code. –  Gilles Nov 27 '12 at 23:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.