Sponsored Content
Top Forums Programming terminal device control through a C/C++ program Post 302118624 by ku@ntum on Wednesday 23rd of May 2007 07:19:26 AM
Old 05-23-2007
terminal device control through a C/C++ program

Hi,

I want to fork a new process from a daemon that needs terminal attachment to some ttyN or pts/N device. Here is the code


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>


int main(int argc, char **args)
{
char *con = args[1];

printf("\nStart demonizing ...\n");

int i = fork();
if (i == 0)
{
close (0);
close (1);
close (2);
setsid();

int j = fork();
if (j == 0)
{
setsid();
//int in = open(con, O_RDWR);
int in = open(con, O_RDONLY);
int ou = open(con, O_WRONLY);
dup2(in, fileno(stdin));
dup2(ou, fileno(stdout));
dup2(ou, fileno(stderr));

printf("\n input :");

stdin = fdopen(0, "r");
stdout = fdopen(1, "w");
stderr = fdopen(2, "w");

char tmp[100];
strcpy(tmp, "");
//gets(tmp);
fread(tmp, sizeof(tmp), 1, stdin);
printf("\n output : %s\n", tmp);
}
exit(0);
}
}


The program takes a single command line argument - the name of the terminal device special file, in my case that will be /dev/"pts/1". The problem is that, the inner most spawned child does the output on the pts/1 terminal successfully, but it fails to take input from it.

Anyone can help here.

Last edited by ku@ntum; 05-23-2007 at 08:28 AM..
 

9 More Discussions You Might Find Interesting

1. SCO

Terminal Control Database Entry

Hi, can someone pls help resolves this problem. I have an Openserver 5.0.4 running but had this error after having a power failure problem. error: cannot access termainl control database entry I have a arnetport also install with wyse 60 terminals, non of the users can have access on... (2 Replies)
Discussion started by: kayode
2 Replies

2. Shell Programming and Scripting

Terminal control from rsh

I call "rsh -l username HOSTMANE myscript.sh" from the script on TRU64 OSF1 cluster. The myscript.sh does some logic one the different cluster node and output requested info on my terminal. If I try to use commands to control output (clear, tput etc..) it just does not work. Obviously the rsh... (2 Replies)
Discussion started by: zam
2 Replies

3. UNIX for Advanced & Expert Users

Control process from different terminal (over SSH)

I pressed CTRL Z and suspended the job. then I pressed bg, The process re-started to throw output on the terminal and its not allowing me to access the prompt. its not even accepting CTRL Z. The process has been running for about 2 hours now and I want to suspend it by opening another terminal.... (3 Replies)
Discussion started by: rakeshou
3 Replies

4. Shell Programming and Scripting

Problems installing a program in Terminal

Hey First of all: I'm on a Mac. I'm quite new to this, so forgive me if i sound like a newb :-) I wish to install the Deluge BitTorrent client. First of all I installed Macports and made it run smooth. But then I ran into a problem when I should install Deluge. I can post the log, and maybe... (4 Replies)
Discussion started by: Sixmax
4 Replies

5. OS X (Apple)

Not mounted, no-driver USB device in terminal (how to access?)

hi, i am on a quest to access and even mount if possible a drive on os x. there is no driver for the device, but it lists fine in the system profiler. can i access its location from the terminal? how? here is what i get on the system profiler: Speed: Up to 480 Mb/sec Manufacturer: SAMSUNG ... (3 Replies)
Discussion started by: sontarieh
3 Replies

6. What is on Your Mind?

Any ideas on child-control program

My auntie told me that she has been suspecting that her little kids are looking at some "illegal" sites online. She put a free web blocker on the computer, but they might be finding loopholes. She wanna install some child-control program to see what's going on. There are so many since I searched... (5 Replies)
Discussion started by: Bluerosen
5 Replies

7. Fedora

How to use terminal while keeping a program open?

hi all, I open Matlab program from terminal. However, when I go back to terminal I can't do anything in it , only until Matlab is closed. Can someone please advise me on how I can oversome this problem ? thanks peter (2 Replies)
Discussion started by: peter_071
2 Replies

8. Slackware

X terminal: Redirecting remote sound to my local audio device

Hello everybody, I'm testing some aspects of X Terminal implementation and it's going great. I can use remote applications on my local slow workstation at remote's processor speed by redirecting the remote DISPLAY variable to "my_local_ip:0.0"; but i'm having troubles to get remote audio and... (2 Replies)
Discussion started by: semash!
2 Replies

9. Shell Programming and Scripting

Control from UNIX script is not returning to the Parent program

Hi All, My program flow is as below Windows batch -- > Cygwin batch --> zsh script There are multiple Cygwin batch scripts that are called from Windows batch file . But when i am executing the first cygwin batch script the control goes to the zsh file and executes and stoping from... (1 Reply)
Discussion started by: Hypesslearner
1 Replies
STDIO(3S)																 STDIO(3S)

NAME
stdio - standard buffered input/output package SYNOPSIS
#include <stdio.h> FILE *stdin; FILE *stdout; FILE *stderr; DESCRIPTION
The functions described in section 3S constitute a user-level buffering scheme. The in-line macros getc and putc(3S) handle characters quickly. The higher level routines gets, fgets, scanf, fscanf, fread, puts, fputs, printf, fprintf, fwrite all use getc and putc; they can be freely intermixed. A file with associated buffering is called a stream, and is declared to be a pointer to a defined type FILE. Fopen(3S) creates certain descriptive data for a stream and returns a pointer to designate the stream in all further transactions. There are three normally open streams with constant pointers declared in the include file and associated with the standard open files: stdin standard input file stdout standard output file stderr standard error file A constant `pointer' NULL(0) designates no stream at all. An integer constant EOF (-1) is returned upon end of file or error by integer functions that deal with streams. Any routine that uses the standard input/output package must include the header file <stdio.h> of pertinent macro definitions. The func- tions and constants mentioned in sections labeled 3S are declared in the include file and need no further declaration. The constants, and the following `functions' are implemented as macros; redeclaration of these names is perilous: getc, getchar, putc, putchar, feof, ferror, fileno. SEE ALSO
open(2), close(2), read(2), write(2), fread(3S), fseek(3S), f*(3S) DIAGNOSTICS
The value EOF is returned uniformly to indicate that a FILE pointer has not been initialized with fopen, input (output) has been attempted on an output (input) stream, or a FILE pointer designates corrupt or otherwise unintelligible FILE data. For purposes of efficiency, this implementation of the standard library has been changed to line buffer output to a terminal by default and attempts to do this transparently by flushing the output whenever a read(2) from the standard input is necessary. This is almost always transparent, but may cause confusion or malfunctioning of programs which use standard i/o routines but use read(2) themselves to read from the standard input. In cases where a large amount of computation is done after printing part of a line on an output terminal, it is necessary to fflush(3S) the standard output before going off and computing so that the output will appear. BUGS
The standard buffered functions do not interact well with certain other library and system functions, especially vfork and abort. LIST OF FUNCTIONS
Name Appears on Page Description clearerr ferror.3s stream status inquiries fclose fclose.3s close or flush a stream fdopen fopen.3s open a stream feof ferror.3s stream status inquiries ferror ferror.3s stream status inquiries fflush fclose.3s close or flush a stream fgetc getc.3s get character or word from stream fgets gets.3s get a string from a stream fileno ferror.3s stream status inquiries fopen fopen.3s open a stream fprintf printf.3s formatted output conversion fputc putc.3s put character or word on a stream fputs puts.3s put a string on a stream fread fread.3s buffered binary input/output freopen fopen.3s open a stream fscanf scanf.3s formatted input conversion fseek fseek.3s reposition a stream ftell fseek.3s reposition a stream fwrite fread.3s buffered binary input/output getc getc.3s get character or word from stream getchar getc.3s get character or word from stream gets gets.3s get a string from a stream getw getc.3s get character or word from stream printf printf.3s formatted output conversion putc putc.3s put character or word on a stream putchar putc.3s put character or word on a stream puts puts.3s put a string on a stream putw putc.3s put character or word on a stream rewind fseek.3s reposition a stream scanf scanf.3s formatted input conversion setbuf setbuf.3s assign buffering to a stream setbuffer setbuf.3s assign buffering to a stream setlinebuf setbuf.3s assign buffering to a stream sprintf printf.3s formatted output conversion sscanf scanf.3s formatted input conversion ungetc ungetc.3s push character back into input stream 4th Berkeley Distribution May 13, 1986 STDIO(3S)
All times are GMT -4. The time now is 06:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy