]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_proc.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / kern / kern_proc.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
64 */
65 /* HISTORY
66 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
67 * Added current_proc_EXTERNAL() function for the use of kernel
68 * lodable modules.
69 *
70 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
71 * New version based on 3.3NS and 4.4
72 */
73
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/proc_internal.h>
79 #include <sys/acct.h>
80 #include <sys/wait.h>
81 #include <sys/file_internal.h>
82 #include <ufs/ufs/quota.h>
83 #include <sys/uio.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/ioctl.h>
87 #include <sys/tty.h>
88 #include <sys/signalvar.h>
89 #include <sys/syslog.h>
90 #include <sys/kernel_types.h>
91
92 /*
93 * Structure associated with user cacheing.
94 */
95 struct uidinfo {
96 LIST_ENTRY(uidinfo) ui_hash;
97 uid_t ui_uid;
98 long ui_proccnt;
99 };
100 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
101 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
102 u_long uihash; /* size of hash table - 1 */
103
104 /*
105 * Other process lists
106 */
107 struct pidhashhead *pidhashtbl;
108 u_long pidhash;
109 struct pgrphashhead *pgrphashtbl;
110 u_long pgrphash;
111 struct proclist allproc;
112 struct proclist zombproc;
113 extern struct tty cons;
114
115 /* Name to give to core files */
116 __private_extern__ char corefilename[MAXPATHLEN+1] = {"/cores/core.%P"};
117
118 static void orphanpg(struct pgrp *pg);
119
120 /*
121 * Initialize global process hashing structures.
122 */
123 void
124 procinit()
125 {
126
127 LIST_INIT(&allproc);
128 LIST_INIT(&zombproc);
129 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
130 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
131 uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
132 }
133
134 /*
135 * Change the count associated with number of processes
136 * a given user is using.
137 */
138 int
139 chgproccnt(uid, diff)
140 uid_t uid;
141 int diff;
142 {
143 register struct uidinfo *uip;
144 register struct uihashhead *uipp;
145
146 uipp = UIHASH(uid);
147 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
148 if (uip->ui_uid == uid)
149 break;
150 if (uip) {
151 uip->ui_proccnt += diff;
152 if (uip->ui_proccnt > 0)
153 return (uip->ui_proccnt);
154 if (uip->ui_proccnt < 0)
155 panic("chgproccnt: procs < 0");
156 LIST_REMOVE(uip, ui_hash);
157 FREE_ZONE(uip, sizeof *uip, M_PROC);
158 return (0);
159 }
160 if (diff <= 0) {
161 if (diff == 0)
162 return(0);
163 panic("chgproccnt: lost user");
164 }
165 MALLOC_ZONE(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
166 if (uip == NULL)
167 panic("chgproccnt: M_PROC zone depleted");
168 LIST_INSERT_HEAD(uipp, uip, ui_hash);
169 uip->ui_uid = uid;
170 uip->ui_proccnt = diff;
171 return (diff);
172 }
173
174 /*
175 * Is p an inferior of the current process?
176 */
177 int
178 inferior(p)
179 register struct proc *p;
180 {
181
182 for (; p != current_proc(); p = p->p_pptr)
183 if (p->p_pid == 0)
184 return (0);
185 return (1);
186 }
187 /*
188 * Is p an inferior of t ?
189 */
190 int
191 isinferior(struct proc *p, struct proc *t)
192 {
193
194 /* if p==t they are not inferior */
195 if (p == t)
196 return(0);
197 for (; p != t; p = p->p_pptr)
198 if (p->p_pid == 0)
199 return (0);
200 return (1);
201 }
202
203 int
204 proc_isinferior(int pid1, int pid2)
205 {
206 proc_t p;
207 proc_t t;
208
209 if (((p = pfind(pid1)) != (struct proc *)0 ) && ((t = pfind(pid2)) != (struct proc *)0))
210 return (isinferior(p, t));
211 return(0);
212 }
213
214 proc_t
215 proc_find(int pid)
216 {
217 return(pfind(pid));
218 }
219
220 int
221 proc_rele(__unused proc_t p)
222 {
223 return(0);
224 }
225
226 proc_t
227 proc_self()
228 {
229 return(current_proc());
230 }
231
232 proc_t
233 proc_findref(int pid)
234 {
235 boolean_t funnel_state;
236 proc_t p;
237
238 funnel_state = thread_funnel_set(kernel_flock,TRUE);
239 p = pfind(pid);
240
241 if (p != proc_refinternal(p, 1))
242 p = PROC_NULL;
243
244 thread_funnel_set(kernel_flock, funnel_state);
245 return(p);
246 }
247
248 void
249 proc_dropref(proc_t p)
250 {
251
252 proc_dropinternal(p, 0);
253 }
254
255
256 proc_t
257 proc_refinternal(proc_t p, int funneled)
258 {
259
260 proc_t p1 = p;
261 boolean_t funnel_state = TRUE; /* need to init just to avoid warnings and build failure */
262
263 if (funneled == 0)
264 funnel_state = thread_funnel_set(kernel_flock,TRUE);
265
266 if ((p != PROC_NULL) &&(p->p_stat != SZOMB) && ((p->p_lflag & (P_LREFDRAINWAIT | P_LREFDRAIN | P_LREFDEAD)) == 0))
267 p->p_internalref++;
268 else
269 p1 = PROC_NULL;
270
271 if (funneled == 0)
272 thread_funnel_set(kernel_flock,funnel_state);
273 return(p1);
274 }
275
276 void
277 proc_dropinternal(proc_t p, int funneled)
278 {
279 boolean_t funnel_state = TRUE; /* need to init just to avoid warnings and build failure */
280
281 if (funneled == 0)
282 funnel_state = thread_funnel_set(kernel_flock,TRUE);
283
284 if (p->p_internalref > 0) {
285 p->p_internalref--;
286 if ((p->p_internalref == 0) && ((p->p_lflag & P_LREFDRAINWAIT) == P_LREFDRAINWAIT)) {
287 p->p_lflag &= ~P_LREFDRAINWAIT;
288 wakeup(&p->p_internalref);
289 }
290 } else
291 printf("proc_dropreg -ve ref\n");
292
293 if (funneled == 0)
294 thread_funnel_set(kernel_flock,funnel_state);
295 }
296
297
298 int
299 proc_pid(proc_t p)
300 {
301 return(p->p_pid);
302 }
303
304 int
305 proc_ppid(proc_t p)
306 {
307 if (p->p_pptr != (struct proc *)0)
308 return(p->p_pptr->p_pid);
309 return(0);
310 }
311
312 int
313 proc_selfpid(void)
314 {
315 struct proc *p = current_proc();
316 return(p->p_pid);
317 }
318
319
320 int
321 proc_selfppid(void)
322 {
323 struct proc *p = current_proc();
324 if (p->p_pptr)
325 return(p->p_pptr->p_pid);
326 else
327 return(0);
328 }
329
330 void
331 proc_name(int pid, char * buf, int size)
332 {
333 struct proc *p;
334
335 if ((p = pfind(pid))!= (struct proc *)0) {
336 strncpy(buf, &p->p_comm[0], size);
337 buf[size-1] = 0;
338 }
339 }
340
341 void
342 proc_selfname(char * buf, int size)
343 {
344 struct proc *p;
345
346 if ((p = current_proc())!= (struct proc *)0) {
347 strncpy(buf, &p->p_comm[0], size);
348 buf[size-1] = 0;
349 }
350 }
351
352 void
353 proc_signal(int pid, int signum)
354 {
355 proc_t p;
356
357 if ((p = pfind(pid))!= (struct proc *)0) {
358 psignal(p, signum);
359 }
360 }
361
362 int
363 proc_issignal(int pid, sigset_t mask)
364 {
365 proc_t p;
366
367 if ((p = pfind(pid))!= (struct proc *)0) {
368 return(proc_pendingsignals(p, mask));
369 }
370 return(0);
371 }
372
373 int
374 proc_noremotehang(proc_t p)
375 {
376 int retval = 0;
377
378 if (p)
379 retval = p->p_flag & P_NOREMOTEHANG;
380 return(retval? 1: 0);
381
382 }
383
384 int
385 proc_exiting(proc_t p)
386 {
387 int retval = 0;
388
389 if (p)
390 retval = p->p_flag & P_WEXIT;
391 return(retval? 1: 0);
392 }
393
394
395 int
396 proc_forcequota(proc_t p)
397 {
398 int retval = 0;
399
400 if (p)
401 retval = p->p_flag & P_FORCEQUOTA;
402 return(retval? 1: 0);
403
404 }
405
406 int
407 proc_tbe(proc_t p)
408 {
409 int retval = 0;
410
411 if (p)
412 retval = p->p_flag & P_TBE;
413 return(retval? 1: 0);
414
415 }
416
417 int
418 proc_suser(proc_t p)
419 {
420 return(suser(p->p_ucred, NULL));
421
422 }
423
424 kauth_cred_t
425 proc_ucred(proc_t p)
426 {
427 return(p->p_ucred);
428 }
429
430
431 int
432 proc_is64bit(proc_t p)
433 {
434 return(IS_64BIT_PROCESS(p));
435 }
436
437 /* LP64todo - figure out how to identify 64-bit processes if NULL procp */
438 int
439 IS_64BIT_PROCESS(proc_t p)
440 {
441 if (p && (p->p_flag & P_LP64))
442 return(1);
443 else
444 return(0);
445 }
446
447
448 /*
449 * Locate a process by number
450 */
451 struct proc *
452 pfind(pid)
453 register pid_t pid;
454 {
455 register struct proc *p;
456
457 if (!pid)
458 return (kernproc);
459
460 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
461 if (p->p_pid == pid)
462 return (p);
463 return (NULL);
464 }
465
466 /*
467 * Locate a zombie by PID
468 */
469 __private_extern__ struct proc *
470 pzfind(pid)
471 register pid_t pid;
472 {
473 register struct proc *p;
474
475 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
476 if (p->p_pid == pid)
477 return (p);
478 return (NULL);
479 }
480
481 /*
482 * Locate a process group by number
483 */
484 struct pgrp *
485 pgfind(pgid)
486 register pid_t pgid;
487 {
488 register struct pgrp *pgrp;
489
490 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
491 if (pgrp->pg_id == pgid)
492 return (pgrp);
493 return (NULL);
494 }
495
496
497 /*
498 * Move p to a new or existing process group (and session)
499 */
500 int
501 enterpgrp(p, pgid, mksess)
502 register struct proc *p;
503 pid_t pgid;
504 int mksess;
505 {
506 register struct pgrp *pgrp = pgfind(pgid);
507
508 #if DIAGNOSTIC
509 if (pgrp != NULL && mksess) /* firewalls */
510 panic("enterpgrp: setsid into non-empty pgrp");
511 if (SESS_LEADER(p))
512 panic("enterpgrp: session leader attempted setpgrp");
513 #endif
514 if (pgrp == NULL) {
515 pid_t savepid = p->p_pid;
516 struct proc *np;
517 /*
518 * new process group
519 */
520 #if DIAGNOSTIC
521 if (p->p_pid != pgid)
522 panic("enterpgrp: new pgrp and pid != pgid");
523 #endif
524 MALLOC_ZONE(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
525 M_WAITOK);
526 if (pgrp == NULL)
527 panic("enterpgrp: M_PGRP zone depleted");
528 if ((np = pfind(savepid)) == NULL || np != p) {
529 FREE_ZONE(pgrp, sizeof(struct pgrp), M_PGRP);
530 return (ESRCH);
531 }
532 if (mksess) {
533 register struct session *sess;
534
535 /*
536 * new session
537 */
538 MALLOC_ZONE(sess, struct session *,
539 sizeof(struct session), M_SESSION, M_WAITOK);
540 if (sess == NULL)
541 panic("enterpgrp: M_SESSION zone depleted");
542 sess->s_leader = p;
543 sess->s_sid = p->p_pid;
544 sess->s_count = 1;
545 sess->s_ttyvp = NULL;
546 sess->s_ttyp = NULL;
547 bcopy(p->p_session->s_login, sess->s_login,
548 sizeof(sess->s_login));
549 p->p_flag &= ~P_CONTROLT;
550 pgrp->pg_session = sess;
551 #if DIAGNOSTIC
552 if (p != current_proc())
553 panic("enterpgrp: mksession and p != curproc");
554 #endif
555 } else {
556 pgrp->pg_session = p->p_session;
557 pgrp->pg_session->s_count++;
558 }
559 pgrp->pg_id = pgid;
560 LIST_INIT(&pgrp->pg_members);
561 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
562 pgrp->pg_jobc = 0;
563 } else if (pgrp == p->p_pgrp)
564 return (0);
565
566 /*
567 * Adjust eligibility of affected pgrps to participate in job control.
568 * Increment eligibility counts before decrementing, otherwise we
569 * could reach 0 spuriously during the first call.
570 */
571 fixjobc(p, pgrp, 1);
572 fixjobc(p, p->p_pgrp, 0);
573
574 LIST_REMOVE(p, p_pglist);
575 if (p->p_pgrp->pg_members.lh_first == 0)
576 pgdelete(p->p_pgrp);
577 p->p_pgrp = pgrp;
578 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
579 return (0);
580 }
581
582 /*
583 * remove process from process group
584 */
585 int
586 leavepgrp(p)
587 register struct proc *p;
588 {
589
590 LIST_REMOVE(p, p_pglist);
591 if (p->p_pgrp->pg_members.lh_first == 0)
592 pgdelete(p->p_pgrp);
593 p->p_pgrp = 0;
594 return (0);
595 }
596
597 /*
598 * delete a process group
599 */
600 void
601 pgdelete(pgrp)
602 register struct pgrp *pgrp;
603 {
604 struct tty * ttyp;
605
606 ttyp = pgrp->pg_session->s_ttyp;
607 if (ttyp != NULL && pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
608 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
609 }
610 LIST_REMOVE(pgrp, pg_hash);
611 if (--pgrp->pg_session->s_count == 0) {
612 if (ttyp != NULL && (ttyp->t_session == pgrp->pg_session))
613 ttyp->t_session = 0;
614 FREE_ZONE(pgrp->pg_session, sizeof(struct session), M_SESSION);
615 }
616 FREE_ZONE(pgrp, sizeof *pgrp, M_PGRP);
617 }
618
619 void
620 sessrele(sess)
621 struct session *sess;
622 {
623 if (--sess->s_count == 0)
624 FREE_ZONE(sess, sizeof (struct session), M_SESSION);
625 }
626
627 /*
628 * Adjust pgrp jobc counters when specified process changes process group.
629 * We count the number of processes in each process group that "qualify"
630 * the group for terminal job control (those with a parent in a different
631 * process group of the same session). If that count reaches zero, the
632 * process group becomes orphaned. Check both the specified process'
633 * process group and that of its children.
634 * entering == 0 => p is leaving specified group.
635 * entering == 1 => p is entering specified group.
636 */
637 void
638 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
639 {
640 register struct pgrp *hispgrp;
641 register struct session *mysession = pgrp->pg_session;
642
643 /*
644 * Check p's parent to see whether p qualifies its own process
645 * group; if so, adjust count for p's process group.
646 */
647 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
648 hispgrp->pg_session == mysession) {
649 if (entering)
650 pgrp->pg_jobc++;
651 else if (--pgrp->pg_jobc == 0)
652 orphanpg(pgrp);
653 }
654
655 /*
656 * Check this process' children to see whether they qualify
657 * their process groups; if so, adjust counts for children's
658 * process groups.
659 */
660 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
661 if ((hispgrp = p->p_pgrp) != pgrp &&
662 hispgrp->pg_session == mysession &&
663 p->p_stat != SZOMB) {
664 if (entering)
665 hispgrp->pg_jobc++;
666 else if (--hispgrp->pg_jobc == 0)
667 orphanpg(hispgrp);
668 }
669 }
670
671 /*
672 * A process group has become orphaned;
673 * if there are any stopped processes in the group,
674 * hang-up all process in that group.
675 */
676 static void
677 orphanpg(struct pgrp *pg)
678 {
679 register struct proc *p;
680
681 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
682 if (p->p_stat == SSTOP) {
683 for (p = pg->pg_members.lh_first; p != 0;
684 p = p->p_pglist.le_next) {
685 pt_setrunnable(p);
686 psignal(p, SIGHUP);
687 psignal(p, SIGCONT);
688 }
689 return;
690 }
691 }
692 }
693
694 #ifdef DEBUG
695 void pgrpdump(void); /* forward declare here (called from debugger) */
696
697 void
698 pgrpdump(void)
699 {
700 struct pgrp *pgrp;
701 struct proc *p;
702 u_long i;
703
704 for (i = 0; i <= pgrphash; i++) {
705 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
706 printf("\tindx %d\n", i);
707 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
708 printf("\tpgrp 0x%08x, pgid %d, sess %p, sesscnt %d, mem %p\n",
709 pgrp, pgrp->pg_id, pgrp->pg_session,
710 pgrp->pg_session->s_count,
711 pgrp->pg_members.lh_first);
712 for (p = pgrp->pg_members.lh_first; p != 0;
713 p = p->p_pglist.le_next) {
714 printf("\t\tpid %d addr 0x%08x pgrp 0x%08x\n",
715 p->p_pid, p, p->p_pgrp);
716 }
717 }
718 }
719 }
720 }
721 #endif /* DEBUG */
722
723 /* XXX should be __private_extern__ */
724 int
725 proc_is_classic(struct proc *p)
726 {
727 return (p->p_flag & P_TRANSLATED) ? 1 : 0;
728 }
729
730 /* XXX Why does this function exist? Need to kill it off... */
731 struct proc *
732 current_proc_EXTERNAL(void)
733 {
734 return (current_proc());
735 }
736
737 /*
738 * proc_core_name(name, uid, pid)
739 * Expand the name described in corefilename, using name, uid, and pid.
740 * corefilename is a printf-like string, with three format specifiers:
741 * %N name of process ("name")
742 * %P process id (pid)
743 * %U user id (uid)
744 * For example, "%N.core" is the default; they can be disabled completely
745 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
746 * This is controlled by the sysctl variable kern.corefile (see above).
747 */
748 __private_extern__ char *
749 proc_core_name(const char *name, uid_t uid, pid_t pid)
750 {
751 const char *format, *appendstr;
752 char *temp;
753 char id_buf[11]; /* Buffer for pid/uid -- max 4B */
754 size_t i, l, n;
755
756 format = corefilename;
757 MALLOC(temp, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
758 if (temp == NULL)
759 return (NULL);
760 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
761 switch (format[i]) {
762 case '%': /* Format character */
763 i++;
764 switch (format[i]) {
765 case '%':
766 appendstr = "%";
767 break;
768 case 'N': /* process name */
769 appendstr = name;
770 break;
771 case 'P': /* process id */
772 sprintf(id_buf, "%u", pid);
773 appendstr = id_buf;
774 break;
775 case 'U': /* user id */
776 sprintf(id_buf, "%u", uid);
777 appendstr = id_buf;
778 break;
779 default:
780 appendstr = "";
781 log(LOG_ERR,
782 "Unknown format character %c in `%s'\n",
783 format[i], format);
784 }
785 l = strlen(appendstr);
786 if ((n + l) >= MAXPATHLEN)
787 goto toolong;
788 bcopy(appendstr, temp + n, l);
789 n += l;
790 break;
791 default:
792 temp[n++] = format[i];
793 }
794 }
795 if (format[i] != '\0')
796 goto toolong;
797 return (temp);
798 toolong:
799 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
800 (long)pid, name, (u_long)uid);
801 FREE(temp, M_TEMP);
802 return (NULL);
803 }