473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Protecting stdin Within A Library

What is the best way to protect stdin within a library?

I am writing a terminal based program that provides plugin
capability using the dlopen() API.

Sequencing program commands (typed) and library input
prompts will not happen if stdin is supplied by pipe
or redirection.

So, I would like to include a statement in the pluggin
header that disables stdin only within the pluggin's scope.
By disable, I expect the statement would result in a
compiler error.

There is also fd 0 to contend with.

Any suggestions would be much appreciated.

Chad

Nov 14 '05 #1
6 2184
In article <11**********************@o13g2000cwo.googlegroups .com>,
cc*****@yahoo.com <cc*****@yahoo.com> wrote:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin
capability using the dlopen() API.
dlopen() is not part of standard C, so this group likely will
not be able to come up with the best answer for your question.

Sequencing program commands (typed) and library input
prompts will not happen if stdin is supplied by pipe
or redirection. So, I would like to include a statement in the pluggin
header that disables stdin only within the pluggin's scope.
By disable, I expect the statement would result in a
compiler error.
stdin is defined by the C89 standard to be a macro [that
is an expression that will give the appropriate FILE*].

This suggests that in theory you could #undef stdin
and #define stdin to be something incompatible that would
generate a compile error if used.

There is also fd 0 to contend with.


File descriptors are not actually part of the C standard either.
They are very common, and have well defined semantics under
the POSIX 1003.1 standard, but they are technically extensions to C.
I am not clear as to what exactly you are trying to protect
against. Are you trying to protect against the plugin opening
(even by way of freopen()) stdin, on the grounds that the prompts
will not happen if the user is not careful enough to use
pty's? Or are you trying to prevent the user from reading
an already established stdin ?

If you are trying to stop the user from using stdin then you
are going to have some difficulties, as a determined user could,
in a typical installation, use something like (stdout-1) or (stderr-2)
[commonly, the expressions for stdin, stdout, stderr are
in terms of addresses of elements of an array that holds all 3,
so simple pointer arithmetic can be used to get a handle on the
other elements.] And users could easily look in their <stdio.h>
and use the expressions directly instead of through the
stdin macro.

If you want users to be able to write plugins that deal with
I/O but you want to protect stdin then you might need to
provide your own fopen() and so on that would end up being what
the user linked against. But that gets into deep questions about
what the symbol resolution order is when you do a dlopen(),
and questions about whether your system has an internal
equivilent to fopen() et al. that you could access from your
replacement routines.
I don't know what it would do to program efficiency, but is
an alternative strategy perhaps in order? When you are calling
those 'sequenced commands' and library routines, would it make
sense for you to have preserved a copy of stdin, and to
restore it into place as the active stdin before calling the
routines? Potentially you could do some optimization about this,
not doing the re-activation unless user-provided code had been
called since the last activation.
--
Warning: potentially contains traces of nuts.
Nov 14 '05 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
stdin is defined by the C89 standard to be a macro [that
is an expression that will give the appropriate FILE*].
No, stdin, stdout, and stderr are

expressions of type "pointer to FILE" that point to the FILE
objects associated, respectively, with the standard error, input,
and output streams.

The wording is identical in C90 and C99. They may be macros (and
commonly are), but they aren't required to be.
This suggests that in theory you could #undef stdin
and #define stdin to be something incompatible that would
generate a compile error if used.


Even if stdin is a macro, that would affect explicit references to
stdin, but it won't necessarily affect functions like getchar() that
implicitly use stdin. If getchar() is a macro that refers to stdin,
redefining stdin will probably break getchar() (which is what you
want); if it's a function, it won't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
stdin is used in a loop in main() to receive input from the user
in the form of command lines.

If the library reads from stdin, they will get the next command
line if stdin has been supplied by pipe or redirection (i.e. not
interactively).

I did not expect a way to temporarily "lock up" stdin within the
library,
but if I can cause a compiler warning it will clue the pluggin author
he is
violating the design intent.

If I redefine stdin as "FILE *stdin = NULL;" in the pluggin header,
will this
create a new local stdin (file scope) separate from the global stdin?

Chad

Nov 14 '05 #4
>I did not expect a way to temporarily "lock up" stdin within the
library,
but if I can cause a compiler warning it will clue the pluggin author
he is
violating the design intent.
I recommend code in the Makefile that greps the source code for "stdin"
and deletes it if that string is found.
If I redefine stdin as "FILE *stdin = NULL;" in the pluggin header,
will this
create a new local stdin (file scope) separate from the global stdin?


No. It might initialize the global stdin. It might try to cause double
initialization of the global stdin (which would cause an error regardless
of whether the plugin author referenced it or not).

static FILE *stdin = NULL;

might work for what you want, but you'd better not include <stdio.h>
in code that uses your header (but you have to include it to get
a definition of FILE). Perhaps you'd be better off with:

static double stdin = 3.14159;

which should give compiler warnings with most uses of stdin.

Gordon L. Burditt
Nov 14 '05 #5
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
stdin is defined by the C89 standard to be a macro [that
is an expression that will give the appropriate FILE*].
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote:
No, stdin, stdout, and stderr are

expressions of type "pointer to FILE" that point to the FILE
objects associated, respectively, with the standard error, input,
and output streams.

The wording is identical in C90 and C99. They may be macros (and
commonly are), but they aren't required to be.


Walter Roberson is correct: they are required to be macros.
(The quote you gave is correct as well: the macros must expand
to such expressions.)

Note that even on systems that have three ordinary variables named
"stdin", "stdout", and "stderr", including <stdio.h> must have the
effect of doing:

#define stdin stdin
#define stdout stdout
#define stderr stderr

and indeed these three lines are found in some <stdio.h> files.
This suggests that in theory you could #undef stdin
and #define stdin to be something incompatible that would
generate a compile error if used.


Even if stdin is a macro, that would affect explicit references to
stdin, but it won't necessarily affect functions like getchar() that
implicitly use stdin. If getchar() is a macro that refers to stdin,
redefining stdin will probably break getchar() (which is what you
want); if it's a function, it won't.


Right. One would have to override all "implicit reference to stdin"
functions as well.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
Chris Torek <no****@torek.net> writes:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
stdin is defined by the C89 standard to be a macro [that
is an expression that will give the appropriate FILE*].


In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote:
No, stdin, stdout, and stderr are

expressions of type "pointer to FILE" that point to the FILE
objects associated, respectively, with the standard error, input,
and output streams.

The wording is identical in C90 and C99. They may be macros (and
commonly are), but they aren't required to be.


Walter Roberson is correct: they are required to be macros.
(The quote you gave is correct as well: the macros must expand
to such expressions.)

Note that even on systems that have three ordinary variables named
"stdin", "stdout", and "stderr", including <stdio.h> must have the
effect of doing:

#define stdin stdin
#define stdout stdout
#define stderr stderr

and indeed these three lines are found in some <stdio.h> files.


You and Walter are right, and I was wrong.

(C99 7.19.1p3 is a single sentence more than a page long. I missed
the fact that the definition of stdin, stdout, and stderr is part of
this sentence, which starts "The macros are ...".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Roland Hall | last post by:
I read Aaron's article: http://www.aspfaq.com/show.asp?id=2276 re: protecting images from linked to by other sites. There is a link at the bottom of that page that references an interesting...
5
by: wallacej | last post by:
Hi Is there a way to protect system files, eg .ini files but still allow access to them from C++ code? As an example I have a settings file called SIMS.INI. This file is often accessed...
10
by: Edlueze | last post by:
I am developing some Data Access Pages (DAP) using Microsoft Access 2003 on Microsoft Windows XP. When I try to open these pages (located on my C: drive), the display of the data access page is...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: Andrew | last post by:
Hello, I have read about stdin and stdout being standard input and output streams, but I am still having trouble visualizing how they really work and what they contain during program execution....
9
by: kernelxu | last post by:
hi, everyone. now, I'am confused on such a problem: function setbuf(stdin, NULL) or setvbuf(stdin, NULL, _IONBF, 0) can set the stadard input stream unbuffered. however, why does my program...
2
by: Jacek | last post by:
Hello! My application has to use external native library writing to stdout and stdin. My goal is to redirect output within running process (no chance to do that in child process - Process class...
4
by: Jean Christophe Avard | last post by:
Hi! I am designing a clipart manager that sells with over 1500+ copyrighted image. I want to protect these image, I read a bit about resource file, would it be the way to go? the image will be about...
0
by: xamman | last post by:
hi there! according to msdn (link at bottom) i should be able to protect a whole class declaratively as above. However i keep getting 'request for principal permissions failed' exceptions. in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.