Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 4 Dec 2019 00:51:44 -0800
From:      Xin Li <delphij@FreeBSD.org>
To:        cem@freebsd.org
Cc:        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:  <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org>
In-Reply-To: <CAG6CVpXXURYpSoPhGQO0%2BiLGDLyPamuHvAdcBx6gVSoMUFp65g@mail.gmail.com>
References:  <201912030703.xB373P5N043316@repo.freebsd.org> <CAG6CVpXXURYpSoPhGQO0%2BiLGDLyPamuHvAdcBx6gVSoMUFp65g@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
--qwmBSWxsECtDu5gif3uuMlPzpNjTHBDvH
Content-Type: multipart/mixed; boundary="fdW2Och1d0YA2mLYR8q5QYqIIuMBz0PBg";
 protected-headers="v1"
From: Xin Li <delphij@FreeBSD.org>
To: cem@freebsd.org
Cc: src-committers <src-committers@freebsd.org>,
 svn-src-all <svn-src-all@freebsd.org>,
 svn-src-head <svn-src-head@freebsd.org>
Message-ID: <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org>
Subject: Re: svn commit: r355318 - head/sbin/newfs_msdos
References: <201912030703.xB373P5N043316@repo.freebsd.org>
 <CAG6CVpXXURYpSoPhGQO0+iLGDLyPamuHvAdcBx6gVSoMUFp65g@mail.gmail.com>
In-Reply-To: <CAG6CVpXXURYpSoPhGQO0+iLGDLyPamuHvAdcBx6gVSoMUFp65g@mail.gmail.com>

--fdW2Och1d0YA2mLYR8q5QYqIIuMBz0PBg
Content-Type: text/plain; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: quoted-printable

On 12/3/19 14:02, Conrad Meyer wrote:
> Hi Xin Li,
>=20
> 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).

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
>=20
> 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
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
>> --- 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 =3D *argv;
>> -    return !!mkfs_msdos(fname, dtype, &o);
>> +    exit(!!mkfs_msdos(fname, dtype, &o));
>>  }
>>
>>  /*



--fdW2Och1d0YA2mLYR8q5QYqIIuMBz0PBg--

--qwmBSWxsECtDu5gif3uuMlPzpNjTHBDvH
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEceNg5NEMZIki80nQQHl/fJX0g08FAl3nc64ACgkQQHl/fJX0
g0/W4A/8DQ6KacOn9C2vF6YDUegcsgVRdq3rtLzEr7eMvTh/tsBcUqdSyqJWwIXK
4p9BrG3rFS9YlnuzrW900a5LMAIh9dX9gNqsbrjMP5D7I/QNm702xReLQqDyCrCR
M+H1/agpl5UNWTZMJtDJ+BW2K1RJfvOdzeXIDmo1g2t1UysplZeI0n4lE4gZADow
CYBMlL02STUfx8QsaH3TAHqAU5cIecl3aTm4uv4lhYBDHIrngJMtQ+R5eCJbZu4g
KLySw9GWqnyL+kZNc+sJH/8rec/cSBclbisodgB0fgW5xAguI+DDbvCQj9HbudJ4
1WjoMMfqsaue/kHyZaME9Ysz5SGfRxY+M0RlhNWkGqyQDuwlET5A1vFMdGfolXWf
RixWG2UnkDfx8mg7ft4Nz3nfth8jiptBL6pLDJ/mySInT+YLR/PjeB+9WuE+q+A2
JuuLzaHHbIh8R6CZN1CX2t3pxWKr2+mzabFL2/j/pTg0x/yljQYJ7IVyyTtj821P
xLyhzrLxa4QdKKwaR0RFZ2/HGTL7JsuV1CEcA3KkN0gDEXPf8wOfDSeMf2Cql8bQ
dGeFmFo0rAaACLas4e4HkVe/1mzQF6kio0KcEMpT8TBZ92FXoMvZfvf6I+atKT32
8s2D+KQ8BDMnA2Wg/mqU0SJaAC6LJL4ezJNLKMZavlXiYZ3sEWM=
=/7bI
-----END PGP SIGNATURE-----

--qwmBSWxsECtDu5gif3uuMlPzpNjTHBDvH--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a>