Date: Wed, 4 Dec 2019 15:02:34 +0200 From: Konstantin Belousov <kostikbel@gmail.com> To: Xin Li <delphij@FreeBSD.org> Cc: cem@freebsd.org, src-committers <src-committers@freebsd.org>, svn-src-all <svn-src-all@freebsd.org>, svn-src-head <svn-src-head@freebsd.org> Subject: Re: svn commit: r355318 - head/sbin/newfs_msdos Message-ID: <20191204130234.GO10580@kib.kiev.ua> In-Reply-To: <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org> References: <201912030703.xB373P5N043316@repo.freebsd.org> <CAG6CVpXXURYpSoPhGQO0%2BiLGDLyPamuHvAdcBx6gVSoMUFp65g@mail.gmail.com> <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Dec 04, 2019 at 12:51:44AM -0800, Xin Li wrote: > On 12/3/19 14:02, Conrad Meyer wrote: > > Hi Xin Li, > > > > Is there a reason to prefer exit() over returning from main? I have > > No, this should be case-by-case (and also assumes you are using C and > not C++). > > The two are actually subtly different (return means teardown main's > stack first, then implicitly call exit from C runtime, while exit() > would terminate immediately). This is even more subtle. If libthr is loaded into the process, then exit() also causes stack unwinding. > > If the command is meant to be used as a built-in module of something > else, like the case of kill(1) builtin of sh(1), then one must not use > exit() and also need to pay special attention to not exit() implicitly, > because the caller will not fork() prior to calling the aliased main() > function for performance reasons. > > For other cases, using exit() might be a good idea, because it's easier > to find the exit points especially if one is following sysexits(3) > values. Another reason is that if one allocates memory in main but not > free them (these shouldn't be free'ed because the kernel would unmap all > pages upon exit), return would mean these memory would be leaked: these > are legitimate issues when main() would be called by someone else. With > an explicit exit(), these memory are never leaked because stack frame of > main() remains valid before the final _exit(2) call. > > > not surveyed the source tree, but I suspect most programs in base exit > > by returning from main rather than explicit exit(3).> Thanks, > > Conrad > > > > On Mon, Dec 2, 2019 at 11:03 PM Xin LI <delphij@freebsd.org> wrote: > >> > >> Author: delphij > >> Date: Tue Dec 3 07:03:25 2019 > >> New Revision: 355318 > >> URL: https://svnweb.freebsd.org/changeset/base/355318 > >> > >> Log: > >> Explicitly exit() instead of return in main(). > >> > >> MFC after: 2 weeks > >> > >> Modified: > >> head/sbin/newfs_msdos/newfs_msdos.c > >> > >> Modified: head/sbin/newfs_msdos/newfs_msdos.c > >> ============================================================================== > >> --- head/sbin/newfs_msdos/newfs_msdos.c Tue Dec 3 07:01:28 2019 (r355317) > >> +++ head/sbin/newfs_msdos/newfs_msdos.c Tue Dec 3 07:03:25 2019 (r355318) > >> @@ -189,7 +189,7 @@ main(int argc, char *argv[]) > >> err(1, NULL); > >> } > >> dtype = *argv; > >> - return !!mkfs_msdos(fname, dtype, &o); > >> + exit(!!mkfs_msdos(fname, dtype, &o)); > >> } > >> > >> /* > >
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20191204130234.GO10580>