187116512SMatthew Dillon /*
287116512SMatthew Dillon * Copyright (c) 2014 The DragonFly Project. All rights reserved.
387116512SMatthew Dillon *
487116512SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
587116512SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
687116512SMatthew Dillon *
787116512SMatthew Dillon * Redistribution and use in source and binary forms, with or without
887116512SMatthew Dillon * modification, are permitted provided that the following conditions
987116512SMatthew Dillon * are met:
1087116512SMatthew Dillon *
1187116512SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
1287116512SMatthew Dillon * notice, this list of conditions and the following disclaimer.
1387116512SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
1487116512SMatthew Dillon * notice, this list of conditions and the following disclaimer in
1587116512SMatthew Dillon * the documentation and/or other materials provided with the
1687116512SMatthew Dillon * distribution.
1787116512SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
1887116512SMatthew Dillon * contributors may be used to endorse or promote products derived
1987116512SMatthew Dillon * from this software without specific, prior written permission.
2087116512SMatthew Dillon *
2187116512SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2287116512SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2387116512SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2487116512SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2587116512SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2687116512SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2787116512SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2887116512SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2987116512SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3087116512SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3187116512SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3287116512SMatthew Dillon * SUCH DAMAGE.
3387116512SMatthew Dillon */
3487116512SMatthew Dillon
3587116512SMatthew Dillon #include <sys/cdefs.h>
3687116512SMatthew Dillon #include <sys/types.h>
3787116512SMatthew Dillon #include <sys/syscall.h>
3887116512SMatthew Dillon #include <sys/upmap.h>
3987116512SMatthew Dillon #include <sys/time.h>
4087116512SMatthew Dillon #include <machine/cpufunc.h>
4187116512SMatthew Dillon #include <errno.h>
4287116512SMatthew Dillon #include <time.h>
4387116512SMatthew Dillon #include <stdio.h>
4487116512SMatthew Dillon #include <unistd.h>
4587116512SMatthew Dillon /*#include "un-namespace.h"*/
4687116512SMatthew Dillon #include "libc_private.h"
4787116512SMatthew Dillon #include "upmap.h"
4887116512SMatthew Dillon
4987116512SMatthew Dillon extern pid_t __sys_getpid(void);
5087116512SMatthew Dillon int __getpid(void);
5187116512SMatthew Dillon
5287116512SMatthew Dillon static int fast_getpid_state;
5387116512SMatthew Dillon static int fast_getpid_count;
5487116512SMatthew Dillon static pid_t *pidp;
5587116512SMatthew Dillon static int *invforkp;
5687116512SMatthew Dillon
5787116512SMatthew Dillon /*
5887116512SMatthew Dillon * Optimize getpid() if it is called a lot. We have to be careful when
5987116512SMatthew Dillon * getpid() is called in a vfork child prior to exec as the shared area
6087116512SMatthew Dillon * is still the parent's shared area.
6187116512SMatthew Dillon */
6287116512SMatthew Dillon pid_t
__getpid(void)6387116512SMatthew Dillon __getpid(void)
6487116512SMatthew Dillon {
6587116512SMatthew Dillon if (fast_getpid_state == 0 && fast_getpid_count++ >= 10) {
6687116512SMatthew Dillon __upmap_map(&pidp, &fast_getpid_state, UPTYPE_PID);
6787116512SMatthew Dillon __upmap_map(&invforkp, &fast_getpid_state, UPTYPE_INVFORK);
6887116512SMatthew Dillon __upmap_map(NULL, &fast_getpid_state, 0);
6987116512SMatthew Dillon }
7087116512SMatthew Dillon if (fast_getpid_state > 0 && *invforkp == 0)
7187116512SMatthew Dillon return(*pidp);
7287116512SMatthew Dillon else
7387116512SMatthew Dillon return(__sys_getpid());
7487116512SMatthew Dillon }
7587116512SMatthew Dillon
76*0c4f129dSzrj __weak_reference(__getpid, getpid);
77*0c4f129dSzrj
7887116512SMatthew Dillon #if 0 /* DEBUG */
7987116512SMatthew Dillon pid_t xxx_getpid(void);
8087116512SMatthew Dillon
8187116512SMatthew Dillon pid_t
8287116512SMatthew Dillon xxx_getpid(void)
8387116512SMatthew Dillon {
8487116512SMatthew Dillon return(__sys_getpid());
8587116512SMatthew Dillon }
8687116512SMatthew Dillon #endif
87