* BackendStartup -- start backend process * * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise. * * Note: if you change this code, also consider StartAutovacuumWorker. */
| 4730 | * Note: if you change this code, also consider StartAutovacuumWorker. |
| 4731 | */ |
| 4732 | static int |
| 4733 | BackendStartup(Port *port) |
| 4734 | { |
| 4735 | Backend *bn; /* for backend cleanup */ |
| 4736 | pid_t pid; |
| 4737 | |
| 4738 | /* |
| 4739 | * Create backend data structure. Better before the fork() so we can |
| 4740 | * handle failure cleanly. |
| 4741 | */ |
| 4742 | bn = (Backend *) malloc(sizeof(Backend)); |
| 4743 | if (!bn) |
| 4744 | { |
| 4745 | ereport(LOG, |
| 4746 | (errcode(ERRCODE_OUT_OF_MEMORY), |
| 4747 | errmsg("out of memory"))); |
| 4748 | return STATUS_ERROR; |
| 4749 | } |
| 4750 | |
| 4751 | /* |
| 4752 | * Compute the cancel key that will be assigned to this backend. The |
| 4753 | * backend will have its own copy in the forked-off process' value of |
| 4754 | * MyCancelKey, so that it can transmit the key to the frontend. |
| 4755 | */ |
| 4756 | if (!RandomCancelKey(&MyCancelKey)) |
| 4757 | { |
| 4758 | free(bn); |
| 4759 | ereport(LOG, |
| 4760 | (errcode(ERRCODE_INTERNAL_ERROR), |
| 4761 | errmsg("could not generate random cancel key"))); |
| 4762 | return STATUS_ERROR; |
| 4763 | } |
| 4764 | |
| 4765 | bn->cancel_key = MyCancelKey; |
| 4766 | |
| 4767 | /* Pass down canAcceptConnections state */ |
| 4768 | port->canAcceptConnections = canAcceptConnections(BACKEND_TYPE_NORMAL); |
| 4769 | bn->dead_end = (port->canAcceptConnections != CAC_OK && |
| 4770 | port->canAcceptConnections != CAC_SUPERUSER && |
| 4771 | port->canAcceptConnections != CAC_MIRROR_READY); |
| 4772 | |
| 4773 | /* |
| 4774 | * Unless it's a dead_end child, assign it a child slot number |
| 4775 | */ |
| 4776 | if (!bn->dead_end) |
| 4777 | bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot(); |
| 4778 | else |
| 4779 | bn->child_slot = 0; |
| 4780 | |
| 4781 | /* Hasn't asked to be notified about any bgworkers yet */ |
| 4782 | bn->bgworker_notify = false; |
| 4783 | |
| 4784 | #ifdef EXEC_BACKEND |
| 4785 | pid = backend_forkexec(port); |
| 4786 | #else /* !EXEC_BACKEND */ |
| 4787 | pid = fork_process(); |
| 4788 | if (pid == 0) /* child */ |
| 4789 | { |
no test coverage detected