LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-14-2009, 01:10 PM   #1
nonchip
LQ Newbie
 
Registered: Aug 2009
Posts: 4

Rep: Reputation: 0
emulating files or char devices


Hi, i need to make a program, which "emulates" a file (char device) to another program (which would run as child process).
the data coming from stdin should go to that file (if the child process reads from that file) and the output of the child written into that file should go to stdout.

like this:
$ echo "input" | emufile testfile.txt bash -c "base64 testfile.txt > testfile.txt"
aW5wdXQK

or, to simulate a char device (this example would forward the webcam device over network via ssh):
$ mkfifo fifo1 fifo2
$ ssh -c "cat /dev/video0" host > fifo1 &
$ ssh -c "cat > /dev/video0" host < fifo2 &
$ cat fifo1 | emufile /dev/video0 cheese | cat fifo2


so, what i try to do is something like padsp (which simulates a OSS (/dev/dsp) device in an PulseAudio environment), but not for a specific file, and i also have to "forward" stdio from/to that file.
i looked at the padsp sources, and it seems that it defines custom fopen/fwrite/fclose/etc... syscalls, and somehow uses them as wrappers for his child process.

if anyone knows howto do that, or knows an tutorial/howto on simulating files to child processes, please tell me.
 
Old 08-14-2009, 02:08 PM   #2
mpez0
LQ Newbie
 
Registered: Jul 2009
Distribution: fedora
Posts: 2

Rep: Reputation: 0
Not sure I understand the question, but I think that pseudo-ttys will do much of what you want. Check the pty(7) man page for capabilities and usage.
 
Old 08-14-2009, 02:29 PM   #3
nonchip
LQ Newbie
 
Registered: Aug 2009
Posts: 4

Original Poster
Rep: Reputation: 0
can make use a pseudotty called /dev/video0 and pipe stdin of an process to it, and stdout of it to another process? if yes, then it could work...
but i thought of making a real "virtual file", which is not on the harddisk or ramdisk, but only exists in an wrapper function which overwrites the stdio of the child process. i mean if using pseudo-tty is so simple, why should padsp then replace syscalls?

oh, i have an idea: could it be possible to compile a modified version of iostream.h into a lib, and then just call my "child process" with LD_PRELOAD or LD_LIBRARY_PATH (or how that environment variable is called), so it uses my modifies lib for file access?

i just got another idea using the Unix98PTY: i could create an PTY, then create a symlink to the name the file should be called, run the program, wait for the program quitting, remove the symlink and then close the PTY.
do you think this can be done in a bash script? because i like bash scripting more than coding C/C++ for such "small" tasks.

Last edited by nonchip; 08-14-2009 at 02:48 PM.
 
Old 08-14-2009, 04:33 PM   #4
nonchip
LQ Newbie
 
Registered: Aug 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ok, update: i have the following code now:
Code:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char** argv) {
  if(argc > 2){
    int         fdm,          //master  FD
                fds;          //slave   FD
    char        *slavename;   //slave   filename

    int         fdm2,         //master2 FD
                fds2;         //slave2  FD
    char        *slavename2;  //slave2  filename
    extern char *ptsname();

    int         fdio= open("/dev/stdio", O_RDWR);

    int pid=fork();
    if(pid==0) { //fdm -> fdm2
      fdm = open("/dev/ptmx", O_NOCTTY);
      grantpt(fdm);
      unlockpt(fdm);
      slavename = ptsname(fdm);
      char *buf_ln_1=malloc(strlen("ln -s ")+strlen(slavename)+strlen(" ")+strlen(argv[1])+1);
      strcpy(buf_ln_1,"ln -s ");strcat(buf_ln_1,slavename);strcat(buf_ln_1," ");strcat(buf_ln_1,argv[1]);
      system(buf_ln_1);
      char *buf=malloc(2);
      while(0!=read(fdm,buf,1)){
        write(fdm2,buf,1);
        write(fdio,buf,1); //debug
      }
      close(fdm);
    }else{ //fdm2 -> fdm
      fdm2 = open("/dev/ptmx", O_NOCTTY);
      grantpt(fdm2);
      unlockpt(fdm2);
      slavename2 = ptsname(fdm2);
      char *buf_ln_2=malloc(strlen("ln -s ")+strlen(slavename2)+strlen(" ")+strlen(argv[2])+1);
      strcpy(buf_ln_2,"ln -s ");strcat(buf_ln_2,slavename2);strcat(buf_ln_2," ");strcat(buf_ln_2,argv[2]);
      system(buf_ln_2);
      char *buf=malloc(2);
      while(0!=read(fdm2,buf,1)){
        write(fdm,buf,1);
        write(fdio,buf,1); //debug
      }
      close(fdm2);
    }
    char *buf_ln_3=malloc(strlen("rm ")+strlen(argv[1])+strlen(argv[2])+1);
    system(buf_ln_3);
  }
}
if you compile it, run like this: "a.out virtualFile1 virtualFile2" (when a.out is the binary).

it does create 2 PTYs, and symlinks to virtualFile1 and virtualFile2.
but what i want (in the while loops) is that it "connects" the two PTYs (so that input from pty1 goes to pty2's output and input from pty2 will be put out on pty1). it does'nt work.
i tried "cat > virtualFile1" on one xterm while doing "cat virtualFile2" in the other one, then typed into the first one, but the input didn't appear in the second one or in the debug lines
 
Old 08-15-2009, 12:17 AM   #5
nonchip
LQ Newbie
 
Registered: Aug 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ok, i just saw there IS a program doing exactly what i want, creating chardevices (PTYs) and connecting them: http://www.ant.uni-bremen.de/whomes/...nullmodem.html

i use this one now, that's better than my quick&dirty code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem decompressing block/char devices linux_chris Linux - Software 4 02-04-2009 07:50 PM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
invalid conversion from `const char*' to `char*' deepinlife Programming 22 08-05-2006 10:49 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
files size to char[] purefan Programming 6 04-03-2005 09:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration