🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.os.linux.programowanie
Article #605

Re: chroot

#605
From: Cezary H. Noweta
Date: Fri, 24 Sep 1999 14:08
76 lines
2400 bytes
Witam,

Filip Ozimek wrote:
> [...] Móglby ktoś podać jakiś
> dzialający przykladzik?

------
  example.c
------
#define _GNU_SOURCE
/* powyzsze dla environ */

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int my_system(const char *cmd)
{
  const char *my_args[] = { cmd, NULL };
  pid_t pid = fork();
  int res, stat;

/* child - uruchamiamy komende */
  if ( !pid ) {
    execve(cmd, (char * const *)my_args, environ);
    return(1000000 + errno);
  }

/* fork sie wywalil */
  else if ( pid < 0 )
    return(2000000 + errno);

/* parent - czekamy na childa */
  while ( !(res = waitpid(pid, &stat, WNOHANG)) ) {
    /* tutaj robimy rozne rzeczy,
       gdy nudzi nam sie w oczekiwaniu
       na childa */
  }

/* waitpid sie powiodl sie */
  if ( res > 0 )
    return(-stat);

/* waitpid sie wywalil - nie powinno do tego dojsc ale... ;) */
  return(3000000 + errno);
}

main()
{
  if ( chroot(".") < 0 )
    fprintf(stderr, "ERROR while chroot()ing\n");

  fprintf(stderr, "my_system() returned: %d\n", my_system("/hello"));
}
------

Po pomyslnym wykonaniu powinienes dostac:

Hello, world!
my_system() returned: -######

w przypadku bledu:

my_system() returned(): #######
my_system() returned(): -######

Zrodla programu /hello chyba nie musze podawac ;-)

  -- pozdrawiam

-- 
Cezary H. Noweta                 90-237 LODZ, ul. J.Matejki 34/38 POLAND
(chn@irys.usun.drugiitrzeciczlon.uni.lodz.pl)    +48 42 6356013, 6356012
University of Lodz - Main Library                            fax 6781678
Technical Details
Message-ID: <37EB85EF.65F31B85@irys.uni.lodz.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!news.swierk.pl!paa.gov.pl!opoka-warszawa!orion.cst.tpsa.pl!netra.daewoo.com.pl!news.task.gda.pl!polsl.gliwice.pl!not-for-mail
References: <7sdfqq$jkt$1@news.pse.pl> <37EA4AD2.FDEDB1F7@irys.usundrugiczlon.lodz.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!mat.uni.torun.pl!uw.edu.pl!news.swierk.pl!paa.gov.pl!opoka-warszawa!orion.cst.tpsa.pl!netra.daewoo.com.pl!news.task.gda.pl!polsl.gliwice.pl!not-for-mail
From: "Cezary H. Noweta" <chn@irys.uni.lodz.pl>
Newsgroups: pl.comp.os.linux.programowanie
Subject: Re: chroot
Date: Fri, 24 Sep 1999 16:08:47 +0200
Organization: Biblioteka Uniwersytecka w Lodzi
Lines: 76
Message-ID: <37EB85EF.65F31B85@irys.uni.lodz.pl>
References: <7sdfqq$jkt$1@news.pse.pl> <37EA4AD2.FDEDB1F7@irys.usundrugiczlon.lodz.pl>
NNTP-Posting-Host: chncc.lib.uni.lodz.pl
Mime-Version: 1.0
X-Mailer: Mozilla 4.61 [en] (Win95; I)
X-Accept-Language: pl,en
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit