patch: fix for pr java.lang/339

patch: fix for pr java.lang/339 this is the mail archive of the java-patches@sources.redhat.com mailing list for the java project. index nav: [date index] [subject index] [author index] [thread index] message nav: [date prev] [date next] [thread prev] [thread next] patch: fix for pr java.lang/339 to: java patch list <java-patches at sourceware dot cygnus dot com> subject: patch: fix for pr java.lang/339 from: tom tromey <tromey at cygnus dot com> date: 06 sep 2000 12:18:03 -0600 reply-to: tromey at cygnus dot com i'm checking this in. it fixes pr java.lang/339 by making a failed exec() in process report back to the parent correctly. it also fixes some potential resource leaks. 2000-09-06 tom tromey <tromey@cygnus.com> fix for pr java.lang/339: * java/lang/natposixprocess.cc (fail): new function. (cleanup): new function. (startprocess): use them. create pipe so child can communicate exec failure back to parent. tom index: java/lang/natposixprocess.cc =================================================================== rcs file: /cvs/java/libgcj/libjava/java/lang/natposixprocess.cc,v retrieving revision 1.6 diff -u -r1.6 natposixprocess.cc --- natposixprocess.cc 2000/03/07 19:55:26 1.6 +++ natposixprocess.cc 2000/09/06 18:09:14 @@ -1,6 +1,6 @@ // natposixprocess.cc - native side of posix process code. -/* copyright (c) 1998, 1999 free software foundation +/* copyright (c) 1998, 1999, 2000 free software foundation this file is part of libgcj. @@ -34,6 +34,7 @@ #include <java/io/fileinputstream.h> #include <java/io/fileoutputstream.h> #include <java/io/ioexception.h> +#include <java/lang/outofmemoryerror.h> extern char **environ; @@ -100,6 +101,55 @@ return buf; } +static void +cleanup (char **args, char **env) +{ + if (args != null) + { + for (int i = 0; args[i] != null; ++i) + _jv_free (args[i]); + _jv_free (args); + } + if (env != null) + { + for (int i = 0; env[i] != null; ++i) + _jv_free (env[i]); + _jv_free (env); + } +} + +static void +fail (int error_value, char **args, char **env, + int *one = null, int *two = null, + int *three = null, int *four = null, + java::lang::throwable *t = null) +{ + cleanup (args, env); + if (one != null) + { + close (one[0]); + close (one[1]); + } + if (two != null) + { + close (two[0]); + close (two[1]); + } + if (three != null) + { + close (three[0]); + close (three[1]); + } + if (four != null) + { + close (four[0]); + close (four[1]); + } + if (t == null) + t = new java::io::ioexception (jvnewstringlatin1 (strerror (error_value))); + throw t; +} + void java::lang::concreteprocess::startprocess (jstringarray progarray, jstringarray envp) @@ -109,57 +159,76 @@ hasexited = false; if (! progarray) - _jv_throw (new nullpointerexception); + throw new nullpointerexception; // transform arrays to native form. - // fixme: we use malloc here. we shouldn't. if an exception is - // thrown we will leak memory. char **args = (char **) _jv_malloc ((progarray->length + 1) * sizeof (char *)); char **env = null; - // fixme: gc will fail here if _jv_malloc throws an exception. - // that's because we have to manually free the contents, but we + // initialize so we can gracefully recover. jstring *elts = elements (progarray); - for (int i = 0; i < progarray->length; ++i) - args[i] = new_string (elts[i]); - args[progarray->length] = null; + for (int i = 0; i <= progarray->length; ++i) + args[i] = null; - if (envp) + try { - env = (char **) _jv_malloc ((envp->length + 1) * sizeof (char *)); - elts = elements (envp); - for (int i = 0; i < envp->length; ++i) - env[i] = new_string (elts[i]); - env[envp->length] = null; - } + for (int i = 0; i < progarray->length; ++i) + args[i] = new_string (elts[i]); + args[progarray->length] = null; - // create pipes for i/o. - int inp[2], outp[2], errp[2]; + if (envp) + { + env = (char **) _jv_malloc ((envp->length + 1) * sizeof (char *)); + elts = elements (envp); - if (pipe (inp) - || pipe (outp) - || pipe (errp)) + // initialize so we can gracefully recover. + for (int i = 0; i <= envp->length; ++i) + env[i] = null; + + for (int i = 0; i < envp->length; ++i) + env[i] = new_string (elts[i]); + env[envp->length] = null; + } + } + catch (java::lang::outofmemoryerror *oome) { - ioerror: - // fixme. - _jv_free (args); - if (env) - _jv_free (env); - _jv_throw (new ioexception (jvnewstringlatin1 (strerror (errno)))); + fail (0, args, env, null, null, null, null, oome); + throw oome; } + // create pipes for i/o. msgp is for communicating exec() status. + int inp[2], outp[2], errp[2], msgp[2]; + + if (pipe (inp)) + fail (errno, args, env); + if (pipe (outp)) + fail (errno, args, env, inp); + if (pipe (errp)) + fail (errno, args, env, inp, outp); + if (pipe (msgp)) + fail (errno, args, env, inp, outp, errp); + if (fcntl (msgp[1], f_setfd, fd_cloexec)) + fail (errno, args, env, inp, outp, errp, msgp); + // we create the streams before forking. otherwise if we had an // error while creating the streams we would have run the child with // no way to communicate with it. - errorstream = new fileinputstream (new filedescriptor (errp[0])); - inputstream = new fileinputstream (new filedescriptor (inp[0])); - outputstream = new fileoutputstream (new filedescriptor (outp[1])); + try + { + errorstream = new fileinputstream (new filedescriptor (errp[0])); + inputstream = new fileinputstream (new filedescriptor (inp[0])); + outputstream = new fileoutputstream (new filedescriptor (outp[1])); + } + catch (java::lang::throwable *t) + { + fail (0, args, env, inp, outp, errp, msgp, t); + } // we don't use vfork() because that would cause the local // environment to be set by the child. if ((pid = (jlong) fork ()) == -1) - goto ioerror; + fail (errno, args, env, inp, outp, errp, msgp); if (pid == 0) { @@ -190,10 +259,13 @@ close (errp[1]); close (outp[0]); close (outp[1]); + close (msgp[0]); execvp (args[0], args); - // fixme: should throw an ioexception if execvp() fails. not trivial, - // because _jv_throw won't work from child process + + // send the parent notification that the exec failed. + char c = errno; + write (msgp[1], &c, 1); _exit (127); } @@ -202,6 +274,17 @@ close (outp[0]); close (inp[1]); close (errp[1]); + close (msgp[1]); + + char c; + int r = read (msgp[0], &c, 1); + if (r == -1) + fail (errno, args, env, inp, outp, errp, msgp); + else if (r != 0) + fail (c, args, env, inp, outp, errp, msgp); + + close (msgp[0]); + cleanup (args, env); fcntl (outp[1], f_setfd, 1); fcntl (inp[0], f_setfd, 1); index nav: [date index] [subject index] [author index] [thread index] message nav: [date prev] [date next] [thread prev] [thread next]

Acceuil

suivante

patch: fix for pr java.lang/339  Tom Tromey - Patch: FYI: Fix for PR 4859  Multi-Fix N.V. - Professional Signmaking Films signmaking vinyl ...  Flickr: FIX MY PIC  The Fix (Heroes) - Wikipedia, the free encyclopedia  Daily Funny Fix / Media  Dent Fix Equipment  HTML FIX IT.COM: A site with free scripts, advanced scripts ...  Blog de jan-fix - jan-fix.skyblog.com - Skyrock.com  Decapitation Victim Tries To Fix Eyes - Health News Story - KGTV ...  Robert M. Ball - A Social Security Fix For 2008 - washingtonpost.com  Télécharger 433 eros fix up jamendo mp3 vbr 192k 2006 02 07 www ...  Lèvres Fix'Intense Tenue 10 h Brillance Ambre - Rouge à Lèvres ...  Mac Rumors: Apple Acknowledges iMac Freezing Issue, Fix In Works  Adobe - Photoshop : For Macintosh : Mac OS X Keyboard Shortcut Fix  Annonces.com : Pc fix *neuf* acer acerpower fh - celeron d 360 ...  XML.com: Using XSLT to Fix Swing  Blog de giangi-fix - gian - Skyrock.com  coding... and other random stuff - home of Bingo Caller  Balai FIX-O-MAT Le balai - Webmarchand.com  How to fix the Firefox memory leak (Firefox hack) « Internet Duct Tape  FixTheLogo.com: Sports  cherche board + fix pas cher ->plus2sport.com  Traduction de la liste des fix 7.0.2 - Football Manager 2007 sur ...   MG WinSock XP Fix 1.2  Welkom op de Homepage van C-Fix  Hard Drive Data Recovery Software - Hard Drive Repair Is Easy With ...  SPACE.com -- Astronauts Ready for Space Station Fix  serious fix 4.1  Lispmeister.com  Free advice on how to fix your bicycle  Video: Your daily Tesla fix! - AutoblogGreen  Traduction Garbage Fix Me Now lyrics - musique traduite  Futurist: To fix education, think Web 2.0 Tech News on ZDNet  Eric's Archived Thoughts: Framework Fix  Clean PC Software :: Registry Fix It!, Error Scan and Fix, Free ...  : : RegistrySmart : :  NASA delays spacewalk to fix solar wing International Reuters  NT4.0 too flawed to fix – official The Register  Encyclopédie méthodique, ou par ordre de matières - Résultats Google Recherche de Livres  Credit Repair by Credit Clean Help Repair Bad Debts  :: SUSHI FIX RESTAURANTS :: WELCOME  Fix Blogger Sitemap XML Warnings Inside Google Webmaster at ...  fix tps  Fix APO ? - forum Snowboard  Deeper level of interest in climate fix csmonitor.com  Aural Fix Communique Online  SIGN FIX sur SOCIETE.COM  EAZ-FIX, Instantly undo your PC problems  'Fix' is finished :: CHICAGO SUN-TIMES :: Robert Feder  fix - Synonyms from Thesaurus.com  How to fix the GST mistake  Teardrop Parts  Linux.com :: DRAM Pricing: The Fix Is In  Journal de physique, de chimie, d'histoire naturelle et des arts ... - Résultats Google Recherche de Livres  Gaufrey: Chanson de Geste - Résultats Google Recherche de Livres  [Profil de fix] OverBlog - Le blog des blogs  [Profil de Fix] OverBlog - Le blog des blogs  Contes de Guillaume Vadé - Résultats Google Recherche de Livres  Sur le compte rendu au Roi en 1781: Nouveaux éclaircissemens - Résultats Google Recherche de Livres  Renaus de Montauban: oder Die Haimonskinder, altfranzösisches Gedicht - Résultats Google Recherche de Livres