Does this mean I suck?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Does this mean I suck?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Does this mean I suck?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM

I can't make my current session the controlling terminal. Here is what
I've attempted so far.

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

#define LOG "/home/cdalten/telfile"


int got_signal = 0;
char signal_msg[64];

/*Some of the code is Lew's */
static void log_tel(int signo)
{
++got_signal;
sprintf(signal_msg,"got signal");
}


int main(void) {

pid_t pid;
int fd;

fd = open("/dev/tty",O_RDWR, 0);
if(fd == -1)
fprintf(stderr,"can't acquire terminal");

if( (pid = fork() ) < 0) {
fprintf(stderr, "Unable to fork process \n");
}


else if(pid == 0) {

if(setsid() < 0)
fprintf(stderr, "setsid error \n");

if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
fprintf(stderr, "error \n");

if(signal(SIGHUP, log_tel) == SIG_ERR) {
fprintf(stderr, "can't trap signal \n");
}


for(;;)
{
freopen(LOG, "w", stdout);
if(got_signal)printf("Got signal - msg is %s\n",signal_msg);
fclose(stdout);
pause();
}

} /*child*/

return 0;
}

And the ouput
$error

Under ps
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
cdalten  12305  0.0  0.1  1232  316 ?        Ss   08:14   0:00 ./grex






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM

K-mart Cashier wrote:
> 	if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
> 	    fprintf(stderr, "error \n");


> And the ouput
> $error


With a bit of google:

----------
TIOCSCTTY     int arg

Make the given tty the controlling tty of the current process.
The current process must be a session leader and not have a
controlling tty already.

If this tty is already the controlling tty of a different session group
then the ioctl fails with EPERM, unless the caller is root and arg
equals 1, in which case the tty is stolen, and all processes that had it
as controlling tty lose it.
----------

You don't print out errno, so the type of error is
not displayed, but I think your proble is as described above.

Rainer





[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM


Rainer Temme wrote:
> K-mart Cashier wrote: 
>
> 
>
>
> With a bit of google:
>
> ----------
> TIOCSCTTY     int arg
>
> Make the given tty the controlling tty of the current process.
> The current process must be a session leader and not have a
> controlling tty already.
>
> If this tty is already the controlling tty of a different session group
> then the ioctl fails with EPERM, unless the caller is root and arg
> equals 1, in which case the tty is stolen, and all processes that had it
> as controlling tty lose it.
> ----------
>
> You don't print out errno, so the type of error is
> not displayed, but I think your proble is as described above.
>
> Rainer

Linux doesn't seem have EPERM.






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM

K-mart Cashier wrote:

> Linux doesn't seem have EPERM.

How about printing errno anyway?

Rainer





[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM


Rainer Temme wrote:
> K-mart Cashier wrote:
> 
>
> How about printing errno anyway?
>
> Rainer

I added the lines

#include <errno.h>

extern int errno;

then
if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
printf("Error number: %d\n", errno);


I got the following:
$./grex
$Error number: 1






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM

K-mart Cashier wrote:
> Rainer Temme wrote: 
>
> I added the lines
>
> #include <errno.h>
>
> extern int errno;
>
> then
>  if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
>             printf("Error number: %d\n", errno);
>
>
> I got the following:
> $./grex
> $Error number: 1

I don't know what the heck error code 1 means. I tried looking in the
header files and found nothing.

So I tried this approach

if( (ioctl(fd, TIOCSCTTY, NULL)) < 0) {
/*printf("Error number: %d\n", errno);*/
perror(argv[0]);
exit(1);
}

and I got the following
$gcc -g grex.c -o grex
$./grex
$./grex: Operation not permitted






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
Bill Pursell


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-16-06 06:25 PM


K-mart Cashier wrote:
> Rainer Temme wrote: 
>
> I added the lines
>
> #include <errno.h>
>
> extern int errno;
>
> then
>  if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
>             printf("Error number: %d\n", errno);
>
>
> I got the following:
> $./grex
> $Error number: 1

What you want is :

if (  ioctl(...) < 0)
perror(NULL);

In general, all of your error messages should be
replaced with a call to perror.  eg: to open a FILE *, use
if ( (fp = fopen( filename, "r")) == NULL) {
perror(filename);
/* handle_error */
}


Also, after you #include <errno.h>, you shouldn't
declare errno.

And, as an aside, in your original code, the global
that you modify in the signal handler shouldn't
be declared as an int.  It should either be a
volatile int, or preferrably, a sig_atomic_t.
This alerts the compiler to the fact that it may
be modified in an unexpected fashion and
prevents annoying bugs caused by compiler
optimizations.






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
Rainer Temme


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-17-06 12:21 AM

K-mart Cashier wrote:
 
[vbcol=seagreen] 
[vbcol=seagreen] 
[vbcol=seagreen]
>             perror(argv[0]);
> $./grex: Operation not permitted

And we learn from this, that Linux indeed has EPERM ...
and now back to what I wrote in my first answer ...
the reason of EPERM are ...

Rainer





[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
Giorgos Keramidas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-17-06 12:21 AM

On 16 Aug 2006 09:47:24 -0700, "K-mart Cashier" <cdalten@gmail.com> wrote:
> Rainer Temme wrote: 
>
> I added the lines
>
> #include <errno.h>
>
> extern int errno;

You shouldn't declare 'errno' yourself.  This is the job of the
<errno.h> header.

> then
>  if( (ioctl(fd, TIOCSCTTY, NULL)) < 0)
>             printf("Error number: %d\n", errno);
>
>
> I got the following:
> $./grex
> $Error number: 1

Which is EPERM, as expected 






[ Post a follow-up to this message ]



    Re: Does this mean I suck?  
K-mart Cashier


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-17-06 06:28 AM


Rainer Temme wrote:
> K-mart Cashier wrote:
> 
> 
> 
> 
>
> And we learn from this, that Linux indeed has EPERM ...
> and now back to what I wrote in my first answer ...
> the reason of EPERM are ...
>
> Rainer

I thought when I called fork, the child got a unique PID to ensure that
it wasn't a process leader. Then when setsid() in the child process, it
makes the child process a session leader. In other words, I should have
gotten EPERM. Apparently I'm missing something.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:29 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register