Sponsored Content
Top Forums Programming Forcing a write to a file without newline? Post 302924845 by madd-games on Wednesday 12th of November 2014 03:56:55 PM
Old 11-12-2014
Forcing a write to a file without newline?

Hello, I am writing a program which runs with root privileges, and it creates a child with lowered privileges and has to redirect it's stdout and stderr to a file and then run bash.

The problem is, whenever I read this file, I want to see all of the current output, even when the program is still running, but when the child writes anything to stdout or stderr, it does not appear in the file until a newline character is written, and then the whole line is actually output.

Here's the code (i tried things like O_RSYNC, O_DSYNC etc to hopefully cause it not to buffer the lines):

Code:
		pid_t pid = fork();
		if (pid == 0)
		{
			//if (setgid(uid) == -1)
			//{
			//	cerr << "could not set group; exiting for security reasons." << endl;
			//	return 1;
			//};
			//if (setuid(uid) == -1)
			//{
			//	cerr << "could not drop privileges; exiting for security reasons." << endl;
			//	return 1;
			//};
			int fd = open(outputFile.c_str(), O_RDWR | O_CREAT | O_RSYNC | O_DSYNC | O_TRUNC, 0644);
			dup2(fd, 1);
			dup2(fd, 2);
			close(fd);
			cout << "terminal-server starting";
			if (execl("/bin/bash", "bash", NULL) == -1)
			{
				cerr << "execl error" << endl;
				return 1;
			};
		}
		else
		{
			int code;
			waitpid(pid, &code, 0);
			cerr << "The shell has terminated with code " << code << "." << endl;
			break;
		};

The line 'cout << "terminal-server is starting" ' does not cause anything to be written to the file at all, ut if I add "<< endl;" to the end (to output a newline), the line is written. But bash writes the command prompt and waits for input, and the command prompt does not appear in the file.

I hope I explained the problem clearly enough. Is there a way to stop it from buffering the output? Thanks.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

forcing su on a user

This is for 3 os's, AIX, Solaris, and AIX, didnt want to post three seperate times on the same subject, anyways, I want to force the user MQM to su, i.e. not be able to rlogin/telnet to the box as user MQM, only login as there ID(chris for example) and su to MQM, does anyone know how to do this,... (4 Replies)
Discussion started by: csaunders
4 Replies

2. Solaris

forcing users to su

Is there a way in solaris 9 to prevent a user to login via ssh, telnet, rlogin, and only be able to su as that user, for example have DBA joe blow login as jblow, and then su to oracle BUT not vice versa have DBA joe blow login as oralce (6 Replies)
Discussion started by: csaunders
6 Replies

3. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

4. Shell Programming and Scripting

SED to convert ~ in a file to newline

Hi, I have a .txt file which has a tilde(~) in it. All that I want is to break into a newline whenever there is an occurence of '~'. I have tried SED to do that but I could not succeed. I would appreciate if I can get a shell script(ksh) for this problem real quick. Thanks in advance. ... (5 Replies)
Discussion started by: ntekupal
5 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. UNIX for Dummies Questions & Answers

Match EOF on newline in a file

Hi, i have a file where the end-of-file might be at the end of of a valid text line or on a new line case a) p q r s t u <eof> case b) p q r s t u <eof> case c) p q r s t u <no data, only carriage return> <eof> I have a requirement where <eof> line should not be read if it's... (3 Replies)
Discussion started by: ysrini
3 Replies

7. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the line..no need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

8. Shell Programming and Scripting

Newline characters in fields of a file

My source file is pipe delimeted file with 53 fields.In 33 rd column i am getting mutlple new line characters,dule to that record is breaking into multiple records. Note : here record delimter also \n sample Source file with 6 fields : 1234|abc| \nabcd \n bvd \n cde \n |678|890|900\n ... (6 Replies)
Discussion started by: lakshmi001
6 Replies

9. Shell Programming and Scripting

Newline in the file

I have requirement to remove the /n ( newline ) characters from the file. When I open file in VI .. I want to see newline char how to display newline char .. or where can I see the content with newline char visible? (3 Replies)
Discussion started by: freakabhi
3 Replies

10. Shell Programming and Scripting

No newline at end of file

Hello all, I have maybe a simple Problem - but I do not know to handle it. All what I want, is to write a line to file without a newline at the end. It works with "echo -n" for all lines, but not for the last one. At the end of the file is always a "0a" (hex) My small script: ... (10 Replies)
Discussion started by: API
10 Replies
IO::Async::ChildManager(3pm)				User Contributed Perl Documentation			      IO::Async::ChildManager(3pm)

NAME
"IO::Async::ChildManager" - facilitates the execution of child processes SYNOPSIS
This object is used indirectly via an "IO::Async::Loop": use IO::Async::Loop; use POSIX qw( WEXITSTATUS ); my $loop = IO::Async::Loop->new; ... $loop->run_child( command => "/bin/ps", on_finish => sub { my ( $pid, $exitcode, $stdout, $stderr ) = @_; my $status = WEXITSTATUS( $exitcode ); print "ps [PID $pid] exited with status $status "; }, ); $loop->open_child( command => [ "/bin/ping", "-c4", "some.host" ], stdout => { on_read => sub { my ( $stream, $buffref, $eof ) = @_; while( $$buffref =~ s/^(.*) // ) { print "PING wrote: $1 "; } return 0; }, }, on_finish => sub { my ( $pid, $exitcode ) = @_; my $status = WEXITSTATUS( $exitcode ); ... }, ); my ( $pipeRd, $pipeWr ) = IO::Async::OS->pipepair; $loop->spawn_child( command => "/usr/bin/my-command", setup => [ stdin => [ "open", "<", "/dev/null" ], stdout => $pipeWr, stderr => [ "open", ">>", "/var/log/mycmd.log" ], chdir => "/", ] on_exit => sub { my ( $pid, $exitcode ) = @_; my $status = WEXITSTATUS( $exitcode ); print "Command exited with status $status "; }, ); $loop->spawn_child( code => sub { do_something; # executes in a child process return 1; }, on_exit => sub { my ( $pid, $exitcode, $dollarbang, $dollarat ) = @_; my $status = WEXITSTATUS( $exitcode ); print "Child process exited with status $status "; print " OS error was $dollarbang, exception was $dollarat "; }, ); DESCRIPTION
This module extends the functionality of the containing "IO::Async::Loop" to manage the execution of child processes. It acts as a central point to store PID values of currently-running children, and to call the appropriate continuation handler code when the process terminates. It provides useful wrapper methods that set up filehandles and other child process details, and to capture the child process's STDOUT and STDERR streams. METHODS
When active, the following methods are available on the containing "Loop" object. $pid = $loop->spawn_child( %params ) This method creates a new child process to run a given code block or command. The %params hash takes the following keys: command => ARRAY or STRING Either a reference to an array containing the command and its arguments, or a plain string containing the command. This value is passed into perl's "exec" function. code => CODE A block of code to execute in the child process. It will be called in scalar context inside an "eval" block. setup => ARRAY A reference to an array which gives file descriptors to set up in the child process before running the code or command. See below. on_exit => CODE A continuation to be called when the child processes exits. It will be invoked in the following way: $on_exit->( $pid, $exitcode, $dollarbang, $dollarat ) The second argument is passed the plain perl $? value. To use that usefully, see "WEXITSTATUS" and others from "POSIX". Exactly one of the "command" or "code" keys must be specified. If the "command" key is used, the given array or string is executed using the "exec" function. If the "code" key is used, the return value will be used as the exit(2) code from the child if it returns (or 255 if it returned "undef" or thows an exception). Case | WEXITSTATUS($exitcode) | $dollarbang | $dollarat --------------+------------------------+-------------+---------- exec succeeds | exit code from program | 0 | "" exec fails | 255 | $! | "" $code returns | return value | $! | "" $code dies | 255 | $! | $@ It is usually more convenient to use the "open_child" method in simple cases where an external program is being started in order to interact with it via file IO, or even "run_child" when only the final result is required, rather than interaction while it is running. "setup" array This array gives a list of file descriptor operations to perform in the child process after it has been fork(2)ed from the parent, before running the code or command. It consists of name/value pairs which are ordered; the operations are performed in the order given. fdn => ARRAY Gives an operation on file descriptor n. The first element of the array defines the operation to be performed: [ 'close' ] The file descriptor will be closed. [ 'dup', $io ] The file descriptor will be dup2(2)ed from the given IO handle. [ 'open', $mode, $file ] The file descriptor will be opened from the named file in the given mode. The $mode string should be in the form usually given to the "open" function; such as '<' or '>>'. [ 'keep' ] The file descriptor will not be closed; it will be left as-is. A non-reference value may be passed as a shortcut, where it would contain the name of the operation with no arguments (i.e. for the "close" and "keep" operations). IO => ARRAY Shortcut for passing "fdn", where n is the fileno of the IO reference. In this case, the key must be a reference that implements the "fileno" method. This is mostly useful for $handle => 'keep' fdn => IO A shortcut for the "dup" case given above. stdin => ... stdout => ... stderr => ... Shortcuts for "fd0", "fd1" and "fd2" respectively. env => HASH A reference to a hash to set as the child process's environment. nice => INT Change the child process's scheduling priority using "POSIX::nice". chdir => STRING Change the child process's working directory using "chdir". setuid => INT setgid => INT Change the child process's effective UID or GID. setgroups => ARRAY Change the child process's groups list, to those groups whose numbers are given in the ARRAY reference. On most systems, only the privileged superuser change user or group IDs. "IO::Async" will NOT check before detaching the child process whether this is the case. If setting both the primary GID and the supplementary groups list, it is suggested to set the primary GID first. Moreover, some operating systems may require that the supplementary groups list contains the primary GID. If no directions for what to do with "stdin", "stdout" and "stderr" are given, a default of "keep" is implied. All other file descriptors will be closed, unless a "keep" operation is given for them. If "setuid" is used, be sure to place it after any other operations that might require superuser privileges, such as "setgid" or opening special files. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::ChildManager(3pm)
All times are GMT -4. The time now is 09:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy