Sponsored Content
Top Forums Programming fork/exec clobbers write. I need ideas why... Post 302119220 by frequency8 on Tuesday 29th of May 2007 12:20:12 AM
Old 05-29-2007
neither one worked.

Below is a crude, but working example of what I mean. Please be gentle on the coding errors.

Code:
/*I wrote the original base code. I only know of one person that has taken this code and improved upon it.
*/
#include <sys/types.h>
#include <sys/wait.h>


#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

#define PATH "/home/cda/input"
#define COUNT 20
#define MAXLINE 1024

enum
  {
    /*NINSULTS = sizeof(insults) / sizeof(insults[0]),*/
    PAUSE = 3,
    TIMES = 24
  };


void init(void) 
{
  /*system("mesg -ye -r -xw");*/
  system("mesg n");
}

 static void
 tel(char *user, char *what)
{
  pid_t   pid;
  /*int     status;*/

  pid = fork();
  if (pid < 0) {
    perror("fork failed");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    execlp("tel", "tel", user, what, (char *)0);
    perror("execlp tel failed");
    exit(EXIT_FAILURE);
  }
  /*status = 0;
  wait(&status);
  if (status != 0) {
    fprintf(stderr, "tel failed, bailing.\n");
    exit(EXIT_FAILURE);
    }*/
}


int main(int argc, char *argv[])
{
  char    **users;
  int     i, j, k;
  char **strarray = NULL;
  int strcount = 0;
  char line[MAXLINE];
  FILE *fp;
  char *new;

  int status;

  if (argc < 2) {
    fprintf(stderr, "Usage: telflood user[s].\n");
    return(EXIT_FAILURE);
  }

  /*i need to get around to fixing this*/
  init();

  for(j=0; j < COUNT; j++) {
    if ((fp = fopen(PATH,"r")) == NULL){
      exit(1);
    }

    while((fgets(line, MAXLINE, fp)) != NULL) {
      strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
      strarray[strcount++] = strdup(line);
    }

  }

  /*char *new = malloc(5*strlen(strarray[0]) + (strlen(strarray[0])+1) );*/

  new=malloc(85*strlen(strarray[0]) + (strlen(strarray[0]) + 1) );

  for(k=0; k<80; k++){
    strcat(new, strarray[0]);
  }



  for (i = 1, j = -1; i < TIMES; i++) {
    for (users = argv + 1; *users; users++) {
      /*tel(*users, insults[j = ((j + 1) % NINSULTS)]);*/
      tel(*users, new);
      sleep(PAUSE);

      status = 0;
      wait(&status);
      if (status != 0) {
        fprintf(stderr, "tel failed, bailing.\n");
        exit(EXIT_FAILURE);
      }

      /*sleep(PAUSE);*/
    }
  }



  for(i = 0; i < COUNT; i++)
    free((void *)strarray[i]);
  free((void *)strarray);
  free(new);


  fclose(fp);
  return(EXIT_SUCCESS);
}

[cda@localhost ~]$ more input
la

[cda@localhost ~]$ ./flood cdalten
cda logged on more than once
Telegram to pts/5...Telegram from cda on pts/5 at 21:12 PDT ...
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la
la

SENT
EOF (cda)
 

10 More Discussions You Might Find Interesting

1. Programming

Fork and exec

Hello! I am working on a server where I should have 4 (resident)processes, one of them being "the father" of the others, so I do 3 forks. The problem that I have is that I do an accept (for sockets) in the "father" process and I want to transmit the job to one of the processes "child" with... (3 Replies)
Discussion started by: driki
3 Replies

2. UNIX for Dummies Questions & Answers

FORK/EXEC technique

Hi! Can someone explain me exactly this technique? Why a process (PARENT) creates a copy of itself with FORK (CHILD)? What's the reason of this behaviour? Sorry, but I cannot understand the logic behind it. Thanks. (4 Replies)
Discussion started by: marshmallow
4 Replies

3. Solaris

fork and exec ftp

Hi, I need to find/implement an application that FTPs (puts) all new files in a certain directory to an external storage unit. This application should check for new files every 10 seconds (leaving the FTP connection open in between the 10 seconds). The easiest way would be if there are... (2 Replies)
Discussion started by: KittyJ
2 Replies

4. UNIX for Dummies Questions & Answers

How to write a script by fork() in unix

Hello to UNIX Champs, Can any body help me out to write the script using fork() thru shell scripting.....i am a layman to fork(), so please give me the link or any scripts which will help me out to know the details about fork. (1 Reply)
Discussion started by: manas_ranjan
1 Replies

5. Shell Programming and Scripting

fork and exec

I need to ssh to a remote server and run my script there. This is my script. $ssh = "ssh username@host"; $cmd = "$ssh 'cd <my dir> && < sudo Run_exe>'"; my $pid = fork; if ($pid == 0){ exec $cmd; } When I run this I get: pccons_getchar: got r == 0 (1 Reply)
Discussion started by: looza
1 Replies

6. Programming

How forbid use fork() in exec() program.

Hello World! I am writing code in C++ which have to launch another application X using exec(). I would like to set some limits on it using setrlimit etc... My problem is that i don't know how to forbid using fork() and strlimit by application X. How can i do it? (3 Replies)
Discussion started by: kzi
3 Replies

7. Programming

Fork and then exec problem with signals

Hi All, In my program i am handling SIGHUP signal. In the handler i fork and then exec on child process same binary file which is running. Parent process will die after 10 mins. Now my child process which was exec with same binary file is not receiving SIGHUP signal. Below is the progran code:... (6 Replies)
Discussion started by: sushil_shalin
6 Replies

8. Programming

Newbie question on exec,fork, wait,pipe C

Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine. So, i want to... (1 Reply)
Discussion started by: Cuervo
1 Replies

9. UNIX for Dummies Questions & Answers

fork with exec

What is is difference between 'fork with exec' and 'fork without exec'? How both are related? (1 Reply)
Discussion started by: kkalyan
1 Replies

10. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies
explain_setpgrp(3)					     Library Functions Manual						explain_setpgrp(3)

NAME
explain_setpgrp - explain setpgrp(2) errors SYNOPSIS
#include <libexplain/setpgrp.h> const char *explain_setpgrp(pid_t pid, pid_t pgid); const char *explain_errno_setpgrp(int errnum, pid_t pid, pid_t pgid); void explain_message_setpgrp(char *message, int message_size, pid_t pid, pid_t pgid); void explain_message_errno_setpgrp(char *message, int message_size, int errnum, pid_t pid, pid_t pgid); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the setpgrp(2) system call. Note: the setpgrp(2) function has two implementations. The System V version has no arguments, while the BSD version has two arguments. For simplicity of implementation, the argument list seen here includes the pid and pgid arguments. The System V getpgid() semantics can be obtained by calling setpgrp(0, 0) on systems with the BSD version, and this is the API for libex- plain, even on systems that do not use the BSD API. explain_setpgrp const char *explain_setpgrp(pid_t pid, pid_t pgid); The explain_setpgrp function is used to obtain an explanation of an error returned by the setpgrp(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. pid The original pid, exactly as passed to the setpgrp(2) system call. pgid The original pgid, exactly as passed to the setpgrp(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: if (setpgrp(pid, pgid) < 0) { fprintf(stderr, "%s ", explain_setpgrp(pid, pgid)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_setpgrp_or_die(3) function. explain_errno_setpgrp const char *explain_errno_setpgrp(int errnum, pid_t pid, pid_t pgid); The explain_errno_setpgrp function is used to obtain an explanation of an error returned by the setpgrp(2) system call. The least the mes- sage will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pid The original pid, exactly as passed to the setpgrp(2) system call. pgid The original pgid, exactly as passed to the setpgrp(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: if (setpgrp(pid, pgid) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_setpgrp(err, pid, pgid)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_setpgrp_or_die(3) function. explain_message_setpgrp void explain_message_setpgrp(char *message, int message_size, pid_t pid, pid_t pgid); The explain_message_setpgrp function is used to obtain an explanation of an error returned by the setpgrp(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. pid The original pid, exactly as passed to the setpgrp(2) system call. pgid The original pgid, exactly as passed to the setpgrp(2) system call. Example: This function is intended to be used in a fashion similar to the following example: if (setpgrp(pid, pgid) < 0) { char message[3000]; explain_message_setpgrp(message, sizeof(message), pid, pgid); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_setpgrp_or_die(3) function. explain_message_errno_setpgrp void explain_message_errno_setpgrp(char *message, int message_size, int errnum, pid_t pid, pid_t pgid); The explain_message_errno_setpgrp function is used to obtain an explanation of an error returned by the setpgrp(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pid The original pid, exactly as passed to the setpgrp(2) system call. pgid The original pgid, exactly as passed to the setpgrp(2) system call. Example: This function is intended to be used in a fashion similar to the following example: if (setpgrp(pid, pgid) < 0) { int err = errno; char message[3000]; explain_message_errno_setpgrp(message, sizeof(message), err, pid, pgid); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_setpgrp_or_die(3) function. SEE ALSO
setpgrp(2) set process group explain_setpgrp_or_die(3) set process group and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2011 Peter Miller explain_setpgrp(3)
All times are GMT -4. The time now is 05:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy