File: pagedresults.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (1050 lines) | stat: -rw-r--r-- 35,124 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2009 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "slap.h"

/* helper function to clean up one prp slot */
static void
_pr_cleanup_one_slot(PagedResults *prp)
{
    PRLock *prmutex = NULL;
    if (!prp) {
        return;
    }
    if (prp->pr_current_be && prp->pr_current_be->be_search_results_release) {
        /* sr is left; release it. */
        prp->pr_current_be->be_search_results_release(&(prp->pr_search_result_set));
    }
    /* clean up the slot */
    if (prp->pr_mutex) {
        /* pr_mutex is reused; back it up and reset it. */
        prmutex = prp->pr_mutex;
    }
    memset(prp, '\0', sizeof(PagedResults));
    prp->pr_mutex = prmutex;
}

/*
 * Parse the value from an LDAPv3 "Simple Paged Results" control.  They look
 * like this:
 *
 *   realSearchControlValue ::= SEQUENCE {
 *   size INTEGER (0..maxInt),
 *   -- requested page size from client
 *   -- result set size estimate from server
 *   cookie OCTET STRING
 *   -- index for the pagedresults array in the connection
 *   }
 *
 * Return an LDAP error code (LDAP_SUCCESS if all goes well).
 */
int
pagedresults_parse_control_value(Slapi_PBlock *pb,
                                 struct berval *psbvp,
                                 ber_int_t *pagesize,
                                 int *index,
                                 Slapi_Backend *be)
{
    int rc = LDAP_SUCCESS;
    struct berval cookie = {0};
    Connection *conn = NULL;
    Operation *op = NULL;
    BerElement *ber = NULL;
    PagedResults *prp = NULL;
    int i;
    int maxreqs = config_get_maxsimplepaged_per_conn();

    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_parse_control_value", "=>\n");
    if (NULL == conn || NULL == op || NULL == pagesize || NULL == index) {
        slapi_log_err(SLAPI_LOG_ERR,
                      "pagedresults_parse_control_value", "<= Error %d\n",
                      LDAP_OPERATIONS_ERROR);
        return LDAP_OPERATIONS_ERROR;
    }
    *index = -1;

    if (psbvp->bv_len == 0 || psbvp->bv_val == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                      "<= no control value\n");
        return LDAP_PROTOCOL_ERROR;
    }
    ber = ber_init(psbvp);
    if (ber == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                      "<= no control value\n");
        return LDAP_PROTOCOL_ERROR;
    }
    if (ber_scanf(ber, "{io}", pagesize, &cookie) == LBER_ERROR) {
        slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                      "<= corrupted control value\n");
        return LDAP_PROTOCOL_ERROR;
    }
    if (!maxreqs) {
        slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                      "Simple paged results requests per conn exceeded the limit: %d\n",
                      maxreqs);
        return LDAP_UNWILLING_TO_PERFORM;
    }

    pthread_mutex_lock(&(conn->c_mutex));
    /* the ber encoding is no longer needed */
    ber_free(ber, 1);
    if (cookie.bv_len <= 0) {
        /* first time? */
        int maxlen = conn->c_pagedresults.prl_maxlen;
        if (conn->c_pagedresults.prl_count == maxlen) {
            if (0 == maxlen) { /* first time */
                conn->c_pagedresults.prl_maxlen = 1;
                conn->c_pagedresults.prl_list = (PagedResults *)slapi_ch_calloc(1, sizeof(PagedResults));
            } else {
                /* new max length */
                conn->c_pagedresults.prl_maxlen *= 2;
                conn->c_pagedresults.prl_list = (PagedResults *)slapi_ch_realloc(
                    (char *)conn->c_pagedresults.prl_list,
                    sizeof(PagedResults) * conn->c_pagedresults.prl_maxlen);
                /* initialze newly allocated area */
                memset(conn->c_pagedresults.prl_list + maxlen, '\0', sizeof(PagedResults) * maxlen);
            }
            *index = maxlen; /* the first position in the new area */
            prp = conn->c_pagedresults.prl_list + *index;
            prp->pr_current_be = be;
        } else {
            prp = conn->c_pagedresults.prl_list;
            for (i = 0; i < conn->c_pagedresults.prl_maxlen; i++, prp++) {
                if (!prp->pr_current_be) { /* unused slot; take it */
                    _pr_cleanup_one_slot(prp);
                    prp->pr_current_be = be;
                    *index = i;
                    break;
                } else if (slapi_timespec_expire_check(&(prp->pr_timelimit_hr)) == TIMER_EXPIRED || /* timelimit exceeded */
                           (prp->pr_flags & CONN_FLAG_PAGEDRESULTS_ABANDONED) /* abandoned */) {
                    _pr_cleanup_one_slot(prp);
                    conn->c_pagedresults.prl_count--;
                    prp->pr_current_be = be;
                    *index = i;
                    break;
                }
            }
        }
        if ((maxreqs > 0) && (*index >= maxreqs)) {
            rc = LDAP_UNWILLING_TO_PERFORM;
            slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_parse_control_value",
                          "Simple paged results requests per conn exeeded the limit: %d\n",
                          maxreqs);
            goto bail;
        }

        if ((*index > -1) && (*index < conn->c_pagedresults.prl_maxlen) &&
            !conn->c_pagedresults.prl_list[*index].pr_mutex) {
            conn->c_pagedresults.prl_list[*index].pr_mutex = PR_NewLock();
        }
        conn->c_pagedresults.prl_count++;
    } else {
        /* Repeated paged results request.
         * PagedResults is already allocated. */
        char *ptr = slapi_ch_malloc(cookie.bv_len + 1);
        memcpy(ptr, cookie.bv_val, cookie.bv_len);
        *(ptr + cookie.bv_len) = '\0';
        *index = strtol(ptr, NULL, 10);
        slapi_ch_free_string(&ptr);
        if ((conn->c_pagedresults.prl_maxlen <= *index) || (*index < 0)) {
            rc = LDAP_PROTOCOL_ERROR;
            slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                          "Invalid cookie: %d\n", *index);
            *index = -1; /* index is invalid. reinitializing it. */
            goto bail;
        }
        prp = conn->c_pagedresults.prl_list + *index;
        if (!(prp->pr_search_result_set)) { /* freed and reused for the next backend. */
            conn->c_pagedresults.prl_count++;
        }
    }
    /* reset sizelimit */
    op->o_pagedresults_sizelimit = -1;

    if ((*index > -1) && (*index < conn->c_pagedresults.prl_maxlen)) {
        if (conn->c_pagedresults.prl_list[*index].pr_flags & CONN_FLAG_PAGEDRESULTS_ABANDONED) {
            /* repeated case? */
            prp = conn->c_pagedresults.prl_list + *index;
            _pr_cleanup_one_slot(prp);
            rc = LDAP_CANCELLED;
        } else {
            /* Need to keep the latest msgid to prepare for the abandon. */
            conn->c_pagedresults.prl_list[*index].pr_msgid = op->o_msgid;
        }
    } else {
        rc = LDAP_PROTOCOL_ERROR;
        slapi_log_err(SLAPI_LOG_ERR, "pagedresults_parse_control_value",
                      "Invalid cookie: %d\n", *index);
    }
bail:
    slapi_ch_free((void **)&cookie.bv_val);
    /* cleaning up the rest of the timedout or abandoned if any */
    prp = conn->c_pagedresults.prl_list;
    for (i = 0; i < conn->c_pagedresults.prl_maxlen; i++, prp++) {
        if (prp->pr_current_be &&
            (slapi_timespec_expire_check(&(prp->pr_timelimit_hr)) == TIMER_EXPIRED || /* timelimit exceeded */
             (prp->pr_flags & CONN_FLAG_PAGEDRESULTS_ABANDONED)) /* abandoned */) {
            _pr_cleanup_one_slot(prp);
            conn->c_pagedresults.prl_count--;
            if (i == *index) {
                /* registered slot is expired and cleaned up. return cancelled. */
                *index = -1;
                rc = LDAP_CANCELLED;
            }
        }
    }
    pthread_mutex_unlock(&(conn->c_mutex));

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_parse_control_value",
                  "<= idx %d\n", *index);
    return rc;
}

/*
 * controlType = LDAP_CONTROL_PAGEDRESULTS;
 * criticality = n/a;
 * controlValue:
 *   realSearchControlValue ::= SEQUENCE {
 *   size INTEGER (0..maxInt),
 *   -- requested page size from client
 *   -- result set size estimate from server
 *   cookie OCTET STRING
 *   }
 */
void
pagedresults_set_response_control(Slapi_PBlock *pb, int iscritical, ber_int_t estimate, int current_search_count, int index)
{
    LDAPControl **resultctrls = NULL;
    LDAPControl pr_respctrl;
    BerElement *ber = NULL;
    struct berval *berval = NULL;
    char *cookie_str = NULL;
    int found = 0;
    int i;
    int cookie = 0;

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_response_control",
                  "=> idx=%d\n", index);

    if ((ber = der_alloc()) == NULL) {
        goto bailout;
    }

    /* begin sequence, payload, end sequence */
    if (current_search_count < 0) {
        cookie = -1;
        cookie_str = slapi_ch_strdup("");
    } else {
        cookie = index;
        cookie_str = slapi_ch_smprintf("%d", index);
    }
    slapi_pblock_set(pb, SLAPI_PAGED_RESULTS_COOKIE, &cookie);
    ber_printf(ber, "{io}", estimate, cookie_str, strlen(cookie_str));
    if (ber_flatten(ber, &berval) != LDAP_SUCCESS) {
        goto bailout;
    }
    pr_respctrl.ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
    pr_respctrl.ldctl_iscritical = iscritical;
    pr_respctrl.ldctl_value.bv_val = berval->bv_val;
    pr_respctrl.ldctl_value.bv_len = berval->bv_len;

    slapi_pblock_get(pb, SLAPI_RESCONTROLS, &resultctrls);
    for (i = 0; resultctrls && resultctrls[i]; i++) {
        if (strcmp(resultctrls[i]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS) == 0) {
            /*
             * We get here if search returns more than one entry
             * and this is not the first entry.
             */
            ldap_control_free(resultctrls[i]);
            resultctrls[i] = slapi_dup_control(&pr_respctrl);
            found = 1;
            break;
        }
    }

    if (!found) {
        /* slapi_pblock_set() will dup the control */
        slapi_pblock_set(pb, SLAPI_ADD_RESCONTROL, &pr_respctrl);
    }

bailout:
    slapi_ch_free_string(&cookie_str);
    ber_free(ber, 1);   /* ber_free() checks for NULL param */
    ber_bvfree(berval); /* ber_bvfree() checks for NULL param */

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_response_control",
                  "<= idx=%d\n", index);
}

int
pagedresults_free_one(Connection *conn, Operation *op, int index)
{
    int rc = -1;

    if (!op_is_pagedresults(op)) {
        return 0; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_free_one",
                  "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (conn->c_pagedresults.prl_count <= 0) {
            slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_free_one",
                          "conn=%" PRIu64 " paged requests list count is %d\n",
                          conn->c_connid, conn->c_pagedresults.prl_count);
        } else if (index < conn->c_pagedresults.prl_maxlen) {
            PagedResults *prp = conn->c_pagedresults.prl_list + index;
            _pr_cleanup_one_slot(prp);
            conn->c_pagedresults.prl_count--;
            rc = 0;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_free_one", "<= %d\n", rc);
    return rc;
}

/*
 * Used for abandoning - conn->c_mutex is already locked in do_abandone.
 */
int
pagedresults_free_one_msgid_nolock(Connection *conn, ber_int_t msgid)
{
    int rc = -1;
    int i;

    if (conn && (msgid > -1)) {
        if (conn->c_pagedresults.prl_maxlen <= 0) {
            ; /* Not a paged result. */
        } else {
            slapi_log_err(SLAPI_LOG_TRACE,
                          "pagedresults_free_one_msgid_nolock", "=> msgid=%d\n", msgid);
            for (i = 0; i < conn->c_pagedresults.prl_maxlen; i++) {
                if (conn->c_pagedresults.prl_list[i].pr_msgid == msgid) {
                    PagedResults *prp = conn->c_pagedresults.prl_list + i;
                    if (prp->pr_current_be &&
                        prp->pr_current_be->be_search_results_release &&
                        prp->pr_search_result_set) {
                        prp->pr_current_be->be_search_results_release(&(prp->pr_search_result_set));
                    }
                    prp->pr_flags |= CONN_FLAG_PAGEDRESULTS_ABANDONED;
                    prp->pr_flags &= ~CONN_FLAG_PAGEDRESULTS_PROCESSING;
                    rc = 0;
                    break;
                }
            }
            slapi_log_err(SLAPI_LOG_TRACE,
                          "pagedresults_free_one_msgid_nolock", "<= %d\n", rc);
        }
    }

    return rc;
}

/* setters and getters for the connection */
Slapi_Backend *
pagedresults_get_current_be(Connection *conn, int index)
{
    Slapi_Backend *be = NULL;
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_current_be", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            be = conn->c_pagedresults.prl_list[index].pr_current_be;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_current_be", "<= %p\n", be);
    return be;
}

int
pagedresults_set_current_be(Connection *conn, Slapi_Backend *be, int index, int nolock)
{
    int rc = -1;
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_current_be", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        if (!nolock)
            pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_current_be = be;
        }
        rc = 0;
        if (!nolock)
            pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_current_be", "<= %d\n", rc);
    return rc;
}

void *
pagedresults_get_search_result(Connection *conn, Operation *op, int locked, int index)
{
    void *sr = NULL;
    if (!op_is_pagedresults(op)) {
        return sr; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result", "=> (%s) idx=%d\n",
                  locked ? "locked" : "not locked", index);
    if (conn && (index > -1)) {
        if (!locked) {
            pthread_mutex_lock(&(conn->c_mutex));
        }
        if (index < conn->c_pagedresults.prl_maxlen) {
            sr = conn->c_pagedresults.prl_list[index].pr_search_result_set;
        }
        if (!locked) {
            pthread_mutex_unlock(&(conn->c_mutex));
        }
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result", "<=%p\n", sr);
    return sr;
}

int
pagedresults_set_search_result(Connection *conn, Operation *op, void *sr, int locked, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return 0; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result", "=> idx=%d, sr=%p\n",
                  index, sr);
    if (conn && (index > -1)) {
        if (!locked)
            pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            PagedResults *prp = conn->c_pagedresults.prl_list + index;
            if (!(prp->pr_flags & CONN_FLAG_PAGEDRESULTS_ABANDONED) || !sr) {
                /* If abandoned, don't set the search result unless it is NULL */
                prp->pr_search_result_set = sr;
            }
            rc = 0;
        }
        if (!locked)
            pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result", "=> %d\n", rc);
    return rc;
}

int
pagedresults_get_search_result_count(Connection *conn, Operation *op, int index)
{
    int count = 0;
    if (!op_is_pagedresults(op)) {
        return count; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result_count", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            count = conn->c_pagedresults.prl_list[index].pr_search_result_count;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result_count", "<= %d\n", count);
    return count;
}

int
pagedresults_set_search_result_count(Connection *conn, Operation *op, int count, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result_count", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_search_result_count = count;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result_count", "<= %d\n", rc);
    return rc;
}

int
pagedresults_get_search_result_set_size_estimate(Connection *conn,
                                                 Operation *op,
                                                 int index)
{
    int count = 0;
    if (!op_is_pagedresults(op)) {
        return count; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result_set_size_estimate",
                  "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            count = conn->c_pagedresults.prl_list[index].pr_search_result_set_size_estimate;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_search_result_set_size_estimate", "<= %d\n",
                  count);
    return count;
}

int
pagedresults_set_search_result_set_size_estimate(Connection *conn,
                                                 Operation *op,
                                                 int count,
                                                 int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result_set_size_estimate",
                  "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_search_result_set_size_estimate = count;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "<-- pagedresults_set_search_result_set_size_estimate", "<= %d\n",
                  rc);
    return rc;
}

int
pagedresults_get_with_sort(Connection *conn, Operation *op, int index)
{
    int flags = 0;
    if (!op_is_pagedresults(op)) {
        return flags; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_with_sort", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            flags = conn->c_pagedresults.prl_list[index].pr_flags & CONN_FLAG_PAGEDRESULTS_WITH_SORT;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_with_sort", "<= %d\n", flags);
    return flags;
}

int
pagedresults_set_with_sort(Connection *conn, Operation *op, int flags, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_with_sort", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            if (flags & OP_FLAG_SERVER_SIDE_SORTING) {
                conn->c_pagedresults.prl_list[index].pr_flags |=
                    CONN_FLAG_PAGEDRESULTS_WITH_SORT;
            }
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_with_sort", "<= %d\n", rc);
    return rc;
}

int
pagedresults_get_unindexed(Connection *conn, Operation *op, int index)
{
    int flags = 0;
    if (!op_is_pagedresults(op)) {
        return flags; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_unindexed", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            flags = conn->c_pagedresults.prl_list[index].pr_flags & CONN_FLAG_PAGEDRESULTS_UNINDEXED;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_unindexed", "<= %d\n", flags);
    return flags;
}

int
pagedresults_set_unindexed(Connection *conn, Operation *op, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_unindexed", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_flags |=
                CONN_FLAG_PAGEDRESULTS_UNINDEXED;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_unindexed", "<= %d\n", rc);
    return rc;
}

int
pagedresults_get_sort_result_code(Connection *conn, Operation *op, int index)
{
    int code = LDAP_OPERATIONS_ERROR;
    if (!op_is_pagedresults(op)) {
        return code; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_sort_result_code", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            code = conn->c_pagedresults.prl_list[index].pr_sort_result_code;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_get_sort_result_code", "<= %d\n", code);
    return code;
}

int
pagedresults_set_sort_result_code(Connection *conn, Operation *op, int code, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_sort_result_code", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_sort_result_code = code;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_sort_result_code", "<= %d\n", rc);
    return rc;
}

int
pagedresults_set_timelimit(Connection *conn, Operation *op, time_t timelimit, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_timelimit", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            slapi_timespec_expire_at(timelimit, &(conn->c_pagedresults.prl_list[index].pr_timelimit_hr));
        }
        pthread_mutex_unlock(&(conn->c_mutex));
        rc = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_timelimit", "<= %d\n", rc);
    return rc;
}

int
pagedresults_set_sizelimit(Connection *conn __attribute__((unused)), Operation *op, int sizelimit, int index)
{
    int rc = -1;
    if (!op_is_pagedresults(op)) {
        return rc; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_sizelimit", "=> idx=%d\n", index);
    op->o_pagedresults_sizelimit = sizelimit;
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_set_sizelimit", "<= %d\n", rc);
    return rc;
}

int
pagedresults_get_sizelimit(Connection *conn __attribute__((unused)), Operation *op, int index)
{
    int sizelimit = -1;
    if (!op_is_pagedresults(op)) {
        return sizelimit; /* noop */
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_get_sizelimit", "=> idx=%d\n", index);
    sizelimit = op->o_pagedresults_sizelimit;
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_get_sizelimit", "<=\n");
    return sizelimit;
}

/*
 * pagedresults_cleanup cleans up the pagedresults list;
 * it does not free the list.
 * return values
 * 0: not a simple paged result connection
 * 1: simple paged result and successfully abandoned
 */
int
pagedresults_cleanup(Connection *conn, int needlock)
{
    int rc = 0;
    int i;
    PagedResults *prp = NULL;

    /* slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_cleanup", "=>\n"); */

    if (NULL == conn) {
        /* slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_cleanup", "<= Connection is NULL\n"); */
        return 0;
    }

    if (needlock) {
        pthread_mutex_lock(&(conn->c_mutex));
    }
    for (i = 0; conn->c_pagedresults.prl_list &&
                i < conn->c_pagedresults.prl_maxlen;
         i++) {
        prp = conn->c_pagedresults.prl_list + i;
        if (prp->pr_current_be && prp->pr_search_result_set &&
            prp->pr_current_be->be_search_results_release) {
            prp->pr_current_be->be_search_results_release(&(prp->pr_search_result_set));
            rc = 1;
        }
        prp->pr_current_be = NULL;
        if (prp->pr_mutex) {
            PR_DestroyLock(prp->pr_mutex);
        }
        memset(prp, '\0', sizeof(PagedResults));
    }
    conn->c_pagedresults.prl_count = 0;
    if (needlock) {
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    /* slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_cleanup", "<= %d\n", rc); */
    return rc;
}

/*
 * pagedresults_cleanup_all frees the list.
 * return values
 * 0: not a simple paged result connection
 * 1: simple paged result and successfully abandoned
 */
int
pagedresults_cleanup_all(Connection *conn, int needlock)
{
    int rc = 0;
    int i;
    PagedResults *prp = NULL;

    if (NULL == conn) {
        return 0;
    }

    if (needlock) {
        pthread_mutex_lock(&(conn->c_mutex));
    }
    for (i = 0; conn->c_pagedresults.prl_list &&
                i < conn->c_pagedresults.prl_maxlen;
         i++) {
        prp = conn->c_pagedresults.prl_list + i;
        if (prp->pr_mutex) {
            PR_DestroyLock(prp->pr_mutex);
        }
        if (prp->pr_current_be && prp->pr_search_result_set &&
            prp->pr_current_be->be_search_results_release) {
            prp->pr_current_be->be_search_results_release(&(prp->pr_search_result_set));
            rc = 1;
        }
        prp->pr_current_be = NULL;
    }
    slapi_ch_free((void **)&conn->c_pagedresults.prl_list);
    conn->c_pagedresults.prl_maxlen = 0;
    conn->c_pagedresults.prl_count = 0;
    if (needlock) {
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    return rc;
}

#if 0 /* Stopped using it (#47347) */
/*
 * check to see if this connection is currently processing
 * a pagedresults search - if it is, return True - if not,
 * mark that it is processing, and return False
 */
int
pagedresults_check_or_set_processing(Connection *conn, int index)
{
    int ret = 0;
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_check_or_set_processing", "=>\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            ret = (conn->c_pagedresults.prl_list[index].pr_flags &
                   CONN_FLAG_PAGEDRESULTS_PROCESSING);
            /* if ret is true, the following doesn't do anything */
            conn->c_pagedresults.prl_list[index].pr_flags |=
                                              CONN_FLAG_PAGEDRESULTS_PROCESSING;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_check_or_set_processing", "<= %d\n", ret);
    return ret;
}

/*
 * mark the connection as being done with pagedresults
 * processing - returns True if it was processing,
 * False otherwise
 */
int
pagedresults_reset_processing(Connection *conn, int index)
{
    int ret = 0;
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_reset_processing", "=> idx=%d\n", index);
    if (conn && (index > -1)) {
        pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            ret = (conn->c_pagedresults.prl_list[index].pr_flags &
                   CONN_FLAG_PAGEDRESULTS_PROCESSING);
            /* if ret is false, the following doesn't do anything */
            conn->c_pagedresults.prl_list[index].pr_flags &=
                                             ~CONN_FLAG_PAGEDRESULTS_PROCESSING;
        }
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_reset_processing", "<= %d\n", ret);
    return ret;
}
#endif

/*
 * This timedout is mainly for an end user leaves a commandline untouched
 * for a long time.  This should not affect a permanent connection which
 * manages multiple simple paged results requests over the connection.
 *
 * [rule]
 * If there is just one slot and it's timed out, we return it is timedout.
 * If there are multiple slots, the connection may be a permanent one.
 * Do not return timed out here.  But let the next request take care the
 * timedout slot(s).
 *
 * must be called within conn->c_mutex
 */
int
pagedresults_is_timedout_nolock(Connection *conn)
{
    PagedResults *prp = NULL;

    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_is_timedout", "=>\n");

    if (!conn || (0 == conn->c_pagedresults.prl_maxlen)) {
        slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_is_timedout", "<= false 1\n");
        return 0;
    }

    prp = conn->c_pagedresults.prl_list;
    if (prp && (1 == conn->c_pagedresults.prl_maxlen)) {
        if (slapi_timespec_expire_check(&(prp->pr_timelimit_hr)) == TIMER_EXPIRED) {
            slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_is_timedout", "<= true\n");
            return 1;
        }
    }
    slapi_log_err(SLAPI_LOG_TRACE, "<-- pagedresults_is_timedout", "<= false 2\n");
    return 0;
}

/*
 * reset all timeout
 * must be called within conn->c_mutex
 */
int
pagedresults_reset_timedout_nolock(Connection *conn)
{
    int i;
    PagedResults *prp = NULL;

    if (NULL == conn) {
        return 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_reset_timedout", "=>\n");

    for (i = 0; i < conn->c_pagedresults.prl_maxlen; i++) {
        prp = conn->c_pagedresults.prl_list + i;
        prp->pr_timelimit_hr.tv_sec = 0;
        prp->pr_timelimit_hr.tv_nsec = 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_reset_timedout", "<=\n");
    return 0;
}

/* paged results requests are in progress. */
int
pagedresults_in_use_nolock(Connection *conn)
{
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_in_use_nolock", "=>\n");
    if (NULL == conn) {
        slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_in_use_nolock", "<= Connection is NULL\n");
        return 0;
    }
    slapi_log_err(SLAPI_LOG_TRACE, "pagedresults_in_use_nolock", "<= %d\n",
                  conn->c_pagedresults.prl_count);
    return conn->c_pagedresults.prl_count;
}

int
op_is_pagedresults(Operation *op)
{
    if (NULL == op) {
        return 0;
    }
    return op->o_flags & OP_FLAG_PAGED_RESULTS;
}

void
op_set_pagedresults(Operation *op)
{
    if (NULL == op) {
        return;
    }
    op->o_flags |= OP_FLAG_PAGED_RESULTS;
}

/*
 * pagedresults_lock/unlock -- introduced to protect search results for the
 * asynchronous searches.
 */
void
pagedresults_lock(Connection *conn, int index)
{
    PagedResults *prp;
    if (!conn || (index < 0) || (index >= conn->c_pagedresults.prl_maxlen)) {
        return;
    }
    pthread_mutex_lock(&(conn->c_mutex));
    prp = conn->c_pagedresults.prl_list + index;
    pthread_mutex_unlock(&(conn->c_mutex));
    if (prp->pr_mutex) {
        PR_Lock(prp->pr_mutex);
    }
    return;
}

void
pagedresults_unlock(Connection *conn, int index)
{
    PagedResults *prp;
    if (!conn || (index < 0) || (index >= conn->c_pagedresults.prl_maxlen)) {
        return;
    }
    pthread_mutex_lock(&(conn->c_mutex));
    prp = conn->c_pagedresults.prl_list + index;
    pthread_mutex_unlock(&(conn->c_mutex));
    if (prp->pr_mutex) {
        PR_Unlock(prp->pr_mutex);
    }
    return;
}

int
pagedresults_is_abandoned_or_notavailable(Connection *conn, int locked, int index)
{
    PagedResults *prp;
    if (!conn || (index < 0) || (index >= conn->c_pagedresults.prl_maxlen)) {
        return 1; /* not abandoned, but do not want to proceed paged results op. */
    }
    if (!locked) {
        pthread_mutex_lock(&(conn->c_mutex));
    }
    prp = conn->c_pagedresults.prl_list + index;
    if (!locked) {
        pthread_mutex_unlock(&(conn->c_mutex));
    }
    return prp->pr_flags & CONN_FLAG_PAGEDRESULTS_ABANDONED;
}

int
pagedresults_set_search_result_pb(Slapi_PBlock *pb, void *sr, int locked)
{
    int rc = -1;
    Connection *conn = NULL;
    Operation *op = NULL;
    int index = -1;
    if (!pb) {
        return 0;
    }
    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    if (!op_is_pagedresults(op)) {
        return 0; /* noop */
    }
    slapi_pblock_get(pb, SLAPI_CONNECTION, &conn);
    slapi_pblock_get(pb, SLAPI_PAGED_RESULTS_INDEX, &index);
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result_pb", "=> idx=%d, sr=%p\n", index, sr);
    if (conn && (index > -1)) {
        if (!locked)
            pthread_mutex_lock(&(conn->c_mutex));
        if (index < conn->c_pagedresults.prl_maxlen) {
            conn->c_pagedresults.prl_list[index].pr_search_result_set = sr;
            rc = 0;
        }
        if (!locked) {
            pthread_mutex_unlock(&(conn->c_mutex));
        }
    }
    slapi_log_err(SLAPI_LOG_TRACE,
                  "pagedresults_set_search_result_pb", "<= %d\n", rc);
    return rc;
}